470,854 Members | 2,579 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,854 developers. It's quick & easy.

Word COM integration

I am noticing a problem when interfacing with Word through my C# code.
What I am trying to do is enumerate through the documents currently
open in Word. I have noticed issues with this on both methods I can
think to try.

We are using COM objects, because we need compatibility with Word 2000.
I am testing this against Word 2003 however.

For simplicity sake, I have two documents open, 1.doc and 2.doc.

Method 1:
If I do
foreach(Word.Document doc in wordHandle.Documents) {}
then the first time this is run, I get the correct documents. If I try
to enumerate through a second time, it only runs through once, even
though wordHandle.Documents.Count is 2.

This doesn't work, so let's try with for...

Method 2:
If I do
for(int i=1; i < wordHandle.Documents.Count+1; i++)
Object oI = i;
Word.Document doc = wordHandle.Documents.Item(ref oI);
then the first time this is run, I get 2 documents, but it is the same
two documents. I get 1.doc and 1.doc, both times. After the first
run, I get the correct two documents, 1.doc and 2.doc.

So, it seems that to get the correct documents every time, I would want
to do the foreach the first time, then use for each time after. This
seems like a serious pain, especially since I don't know what will
happen if a third document is opened after we already enumerated

Has anyone run into this issue and found a good solution?


Nov 16 '05 #1
0 1361

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by David Thielen | last post: by
1 post views Thread by insureallrisk | last post: by
8 posts views Thread by scorpion53061 | last post: by
3 posts views Thread by Jesse Johnson | last post: by
reply views Thread by Bill Partridge | last post: by
10 posts views Thread by Antoine De Groote | last post: by
2 posts views Thread by Derek Hart | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.