none
Interop.Word.Application.Document do not return all open word documents RRS feed

  • Question

  • Hello,

    I'm trying to iterate through all open Word documents.

    Following is my code sample:

     

     

    Application wordApp = (Application) System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
    
        
    
     if(wordApp == null || wordApp .Documents.Count == 0)
     {
    
      return string.Empty;
    
     }
    
    
    foreach (Document document in wordApp .Documents)
    {
     Console.WriteLine(document.Name);
    }
    
    
    Marshal.ReleaseComObject(wordApp);
    

     

    My problem is that the enumeration returns only 2 of 3 open documents. And for some situations doesn't return anything at all.

    I've also noticed that whenever I have documents open from Sharepoint and from disk, only the Sharepoint documents appear in the enumeration.

    By the way, I'm using c#, .Net 4.0 and office 2010.

     

    Any ideas?

    Regards, Marina

    BTW. I have only one winword process running at the time of tests
    Wednesday, March 23, 2011 2:00 PM

Answers

  • Hi, Bruce

    I actually tested it on a different machine and it doesn't reproduce there also.

    I didn't figure out yet when exactly it happens, I'll try to reproduce and write down all the steps.

    Either way - > I get the wordApp just fine, it's not null, it's just returns wrong number of open documents

     

    Regards, Marina

    • Marked as answer by Bruce Song Wednesday, April 6, 2011 7:05 AM
    Sunday, March 27, 2011 1:35 PM

All replies

  • Hi Marina,

    Thank you for posting and we are glad to help with you.

    After reading your post, I tested with your code. However, I can't reproduce your issue. If I open three word documents, then I can get the actual number. Can you get WINWORD.EXE process? Did wordApp return null?

    Try to log off or switch to another user account to see whether it works well.

    Hope this can help you and just feel free to follow up.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, March 24, 2011 9:10 AM
  • Hi, Bruce

    I actually tested it on a different machine and it doesn't reproduce there also.

    I didn't figure out yet when exactly it happens, I'll try to reproduce and write down all the steps.

    Either way - > I get the wordApp just fine, it's not null, it's just returns wrong number of open documents

     

    Regards, Marina

    • Marked as answer by Bruce Song Wednesday, April 6, 2011 7:05 AM
    Sunday, March 27, 2011 1:35 PM
  • Hi Marina,

    If you still encounter the problem and find the reproduciable steps, just let use know. We are glad to help with you.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, March 31, 2011 8:25 AM