none
Word addin ThisAddin_Startup ActiveDocument is null? RRS feed

  • Question

  • Hi,

    I've created a Word 2010 addin. The addin works fine with local documents (opened from explorer).

    But if I open documents via my .net Application using Interop, the addin throws an error: "The command is not available because no document is open".

    The "command" it is refering to is probably Word.ActiveDocument:

    private void ThisAddIn_Startup(object sender, EventArgs e)
    {
        WordApp = this.Application;
    
        if (WordApp.ActiveDocument.Variables.Count > 0)
                GetMetadata();          //Gets Document Variables

    Globals.ThisAddIn.Application.DocumentOpen += (Application_DocumentOpen);
    }

    void Application_DocumentOpen(Microsoft.Office.Interop.Word.Document Doc)
    {
    GetMetadata();
    }
     
    private  void GetMetadata()
    {
    if (WordApp.ActiveDocument.Variables.Count > 0) { .... get data }

     


    This is how I am opening Word documents in my .net Application (I need to insert some custom Variables to the document):

     

    _wordApp = new Word.Application { Visible = false };
    
    Word.Document wordDoc = _wordApp.Documents.Open(filename, ReadOnly: false, Visible: true);
    wordDoc.Variables.Add("FileId", "555");   //Example<br/>
     _wordApp.Visible = true;
    wordDoc.Activate();
    _wordApp.Activate();
                    
    

    As soon as it hits the first line, I get the error: "The command is not available because no document is open". Probably because the addin is loading faster that the document, or before the _wordApp.Documents.Open.

    What am I doing wrong here? How can I make sure that the ActiveDocument is not null in ThisAddIn_Startup when opening documents
    via another .net application? I don't understand why it does not fire the
    Application_DocumentOpen event......

    btw: If I use Process.Start(path); in my application, it works fine. 

     

    Thanks in advance!

     





    Friday, September 9, 2011 2:02 PM

Answers

  • Hi Cindy,

    Yeah, it's realy weird how this works... Here is the code that opens the Word document:
     

    _wordApp = new Word.Application { Visible = false };
    
    object isVisible = false;
    object isReadOnly = readOnly;
    
    Word.Documents wordDocs = _wordApp.Documents;
    Word.Document wordDoc = wordDocs.Open(ref filename, ref _varMissing, ref isReadOnly, ref _varMissing, ref _varMissing, ref _varMissing, ref _varMissing, ref _varMissing, ref _varMissing, ref _varMissing, ref _varMissing, ref isVisible, ref _varMissing, ref _varMissing,  ref _varMissing, ref _varMissing);
    
    
    if (wordDoc.Variables.Count == 0)
    {
        if (SelectedFile != null)
            wordDoc.Variables.Add("FileId", SelectedFile.Id.ToString());
    
        if (SelectedDocument != null)
        {
            wordDoc.Variables.Add("DocumentId", SelectedDocument.Id.ToString());
            wordDoc.Variables.Add("DocumentNr", SelectedDocument.DocumentNr != null ? SelectedDocument.DocumentNr.ToString() : "0");
        }
    
        if (_caseNr != 0)
            wordDoc.Variables.Add("CaseNr", _caseNr.ToString());
    }
    else if (wordDoc.Variables.Count == 4)
    {
           wordDoc.Variables.get_Item("FileId").Value = SelectedFile.Id.ToString();
           wordDoc.Variables.get_Item("DocumentId").Value = SelectedDocument.Id.ToString();
           wordDoc.Variables.get_Item("DocumentNr").Value = SelectedDocument.DocumentNr.ToString();
           wordDoc.Variables.get_Item("CaseNr").Value = _caseNr.ToString();
    }
    
    wordDoc.Save();
    
    _wordApp.Visible = true;
    wordDoc.Activate();
    _wordApp.Activate();
    

    The variables get saved to the document, but are not visible in the Addin. I even put a breakpoint after wrodDoc.Save(); and copied the document to desktop.  Later I just opened the copied document, and it loded properly, with variables.

    I have made a temporary solution using method 2. I only changed how documents are opened in my .net application:
     

    wordDoc.Save();
    
    wordDoc.Close();
    _wordApp.Quit();
    
    Process.Start(filename.ToString());
    

    Not the best solution, but it does the job :) 

    Monday, September 12, 2011 9:33 AM

All replies

  • <<What am I doing wrong here? How can I make sure that the ActiveDocument is not null in ThisAddIn_Startup when opening documents>>

    Two things you can try:

    1. Put a loop in ThisAddin_Startup that checks the Documents.Count property and doesn't continue until it's >0

    2. Add a DocumentOpen event to your project and put the code to check the variables, etc. in that. In that case, you wouldn't use ActiveDocument. Instead, you'd use the Doc parameter provided by the event.


    Cindy Meister, VSTO/Word MVP
    Friday, September 9, 2011 2:15 PM
    Moderator
  • <<What am I doing wrong here? How can I make sure that the ActiveDocument is not null in ThisAddIn_Startup when opening documents>>

    Two things you can try:

    1. Put a loop in ThisAddin_Startup that checks the Documents.Count property and doesn't continue until it's >0

    2. Add a DocumentOpen event to your project and put the code to check the variables, etc. in that. In that case, you wouldn't use ActiveDocument. Instead, you'd use the Doc parameter provided by the event.


    Cindy Meister, VSTO/Word MVP

    Hi, 

    1. I tried this method, I placed a While loop to check (WordApp.Documents.Count > 0) if it is above 0, it will get Metadata and then break;
    Worked nicely in debug, but it won't work via my .net application. it seems like it just skips the entire loop... weird... it doesn't even hang.

    2. This method did not work. Creating a new instance of Word, does not fire the DocumentOpen event. It only fires when opening a document while Word is allready running, via the same instance.

    Thanks... 


    Sunday, September 11, 2011 6:54 PM
  • Just an update on method 2:
    I've just implemented log4net in the addin (logging). 

    I found out that the Application_DocumentOpen event IS fired when opening documents from my .net application. BUT! The Document.Variables.Count is allways 0 (zero), it dosen't cary any of the variables I set on the document in my application.

     

    Sunday, September 11, 2011 10:19 PM
  • Hi Nedim

    In that case, the question is whether the variables are being set and saved in the document at all. I know of nothing in Word, itself, that would remove document Variables.


    Cindy Meister, VSTO/Word MVP
    Monday, September 12, 2011 8:29 AM
    Moderator
  • Hi Cindy,

    Yeah, it's realy weird how this works... Here is the code that opens the Word document:
     

    _wordApp = new Word.Application { Visible = false };
    
    object isVisible = false;
    object isReadOnly = readOnly;
    
    Word.Documents wordDocs = _wordApp.Documents;
    Word.Document wordDoc = wordDocs.Open(ref filename, ref _varMissing, ref isReadOnly, ref _varMissing, ref _varMissing, ref _varMissing, ref _varMissing, ref _varMissing, ref _varMissing, ref _varMissing, ref _varMissing, ref isVisible, ref _varMissing, ref _varMissing,  ref _varMissing, ref _varMissing);
    
    
    if (wordDoc.Variables.Count == 0)
    {
        if (SelectedFile != null)
            wordDoc.Variables.Add("FileId", SelectedFile.Id.ToString());
    
        if (SelectedDocument != null)
        {
            wordDoc.Variables.Add("DocumentId", SelectedDocument.Id.ToString());
            wordDoc.Variables.Add("DocumentNr", SelectedDocument.DocumentNr != null ? SelectedDocument.DocumentNr.ToString() : "0");
        }
    
        if (_caseNr != 0)
            wordDoc.Variables.Add("CaseNr", _caseNr.ToString());
    }
    else if (wordDoc.Variables.Count == 4)
    {
           wordDoc.Variables.get_Item("FileId").Value = SelectedFile.Id.ToString();
           wordDoc.Variables.get_Item("DocumentId").Value = SelectedDocument.Id.ToString();
           wordDoc.Variables.get_Item("DocumentNr").Value = SelectedDocument.DocumentNr.ToString();
           wordDoc.Variables.get_Item("CaseNr").Value = _caseNr.ToString();
    }
    
    wordDoc.Save();
    
    _wordApp.Visible = true;
    wordDoc.Activate();
    _wordApp.Activate();
    

    The variables get saved to the document, but are not visible in the Addin. I even put a breakpoint after wrodDoc.Save(); and copied the document to desktop.  Later I just opened the copied document, and it loded properly, with variables.

    I have made a temporary solution using method 2. I only changed how documents are opened in my .net application:
     

    wordDoc.Save();
    
    wordDoc.Close();
    _wordApp.Quit();
    
    Process.Start(filename.ToString());
    

    Not the best solution, but it does the job :) 

    Monday, September 12, 2011 9:33 AM