Click here to Skip to main content
15,918,333 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when using this code from answer 2: https://www.codeproject.com/Answers/185610/Getting-currently-open-MS-word-insatnce-count-and-.aspx#answer2

C#
Microsoft.Office.Interop.Word.Application objWord;
objWord=(Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
for (int i = 0; i < objWord.Windows.Count; i++)
{
     object a = i+1;
     Window objWin = objWord.Windows.get_Item(ref a);
     MessageBox.Show(objWin.Caption);
}

I do get results displaying two word document windows, but the only problem is 4 exist and 4 word documents are open. It gives me the first 2, but nothing more. Am I doing something wrong?

What I have tried:

I've tried seeing if the count is wrong, but anything outside of 1 and 2 throw an error, even 0. It appears the code doesn't see but two of the docs, even though there are 4. They aren't an old session either. That doesn't matter, since I tested that too. Doesn't matter which two are first, only two are seen.
Posted
Updated 19-Jun-19 21:37pm

You shoul ask the person who posted that answer.
 
Share this answer
 
Comments
Maciej Los 20-Jun-19 4:30am    
Please, don't get me wrong, but i'm pretty sure that such of "answer" should be posted in a comment, Richard.
BTW: very good tip.
Richard MacCutchan 22-Jun-19 5:41am    
I agree; thank you for pointing that out.
There's a collection of Documents[^], you can step through...

C#
foreach(Document doc in WordApp.Documents)
{
   //your logic here
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900