none
Problem with ActiveDocument RRS feed

  • Question

  • I am facing a strange problem with my MS Word Addin. When I open an .ODT document in a network location and that document is already opened by some other application (Open Office or some other application). It throws an exception with ActiveDocument and as a result the Addin is not loaded as you can see with image the exception that is coming and the whole addin gets disabled.

    The problem seems to lie with this section of code in ActiveDocument. Although the document is getting loaded its while getting the active document this problem ariese

    void oWord_DocumentChange()
    {
          Extension = string.Empty;
          try
          {
               if (oWord.Documents.Count >= 1)
                    File = oWord.ActiveDocument.Path.ToString() + @"\" + oWord.ActiveDocument.Name.ToString();
                    
                Extension = Path.GetExtension(File);
                ribbon.InvalidateControl("ODTConvertor");
           }
           catch (System.Exception Exp)
           {
                MessageBox.Show(Exp.Message);
           }
    }


    Adeel

    Monday, August 6, 2012 1:13 PM

Answers

  • It is not read only it is opened from network location once opened Word Displays that notification message that I've mentioned above although this problem is eliminated once I disable these 2 options.


    Adeel

    Thursday, August 9, 2012 5:29 AM

All replies

  • I don't know why the event's firing when there's no ActiveDocument. Possibly someone else will have an idea, or possibly it's just one of those things. I would have thought you could avoid the error, however, by changing the line

    if (oWord.Documents.Count >= 1)
    to
    if ((oWord.Documents.Count >= 1) && (oWord.ActiveDocument != null))
    Please let us know if that at least gets rid of the symptom (the error).
    Monday, August 6, 2012 1:38 PM
  • I don't know why the event's firing when there's no ActiveDocument. Possibly someone else will have an idea, or possibly it's just one of those things. I would have thought you could avoid the error, however, by changing the line

    if (oWord.Documents.Count >= 1)
    to
    if ((oWord.Documents.Count >= 1) && (oWord.ActiveDocument != null))
    Please let us know if that at least gets rid of the symptom (the error).

    Even that doesn't work because even this Active Document Null condition throws this exception. And there is an active document it is that when i load this document which resides in a network location. The document is indeed loaded as you can see in the screen shot. But still it generates this exception


    Adeel

    Tuesday, August 7, 2012 4:48 AM
  • I tried some more files. Usually when I open files from the network at that time this ActiveDocument throws this exception. Although the document is loaded but it throws an exception, Thus disabling the addin.

    Another problem that I've noticed is whenever i open a document in MS Word 2010 from network location I receive this message on the top of the pane

    Which might be creating problem with ActiveDocument. But is there a way that the Addin atleast gets loaded even if there is some exception handled? The addin doesn't get loaded sometimes is something that concerns me the most

    Tuesday, August 7, 2012 5:19 AM
  • What we really need to do is stop that error being thrown.

    It's a good thought that it may be the same thing that creates that yellow 'Enable Editing' strip at the top.

    So, just for testing, what happens if you use the test:

    if ((oWord.Documents.Count >= 1) && (oWord.Documents[1].ReadOnly == true))

    Tuesday, August 7, 2012 7:26 AM
  • It works. But then I am unable to extract the extension of the currently loaded documet because activedocument will still throw error.

    void oWord_DocumentChange()
    {
          Extension = string.Empty;
          try
          {
                //string strName = oWord.ActiveDocument.Name;
                if ((oWord.Documents.Count >= 1) && (oWord.Documents[1].ReadOnly))
                    File = oWord.ActiveDocument.Path.ToString() + @"\" + oWord.ActiveDocument.Name.ToString();
           }
           catch (System.Exception Exp)
           {
               MessageBox.Show(Exp.Message);
           }
    
           Extension = Path.GetExtension(File);
           ribbon.InvalidateControl("ODTConvertor");
    }

    Because in this condition it doesn't fullfill that condition because none of the document that I've opened is read only. But I am opening an .odt document therefore extension name is required

    More importantly this is an ActiveDocument exception. So in worse case scenerio it should'nt be able to get the document details. But that exception doesn't let that addin button to be displayed in Backstage UI is something big concern


    Adeel




    Tuesday, August 7, 2012 7:40 AM
  • I think you need to use a different event. It seems that the 'ActiveDocument', by definition, is one that is editable (i.e. not read only). If you handle the DocumentOpen event, you will be passed a pointer to the just opened document, from which you can extract the filepath.

    By the way, in your DocumentChange handler, the code I showed was just for testing a theory....if you have other documents open you may not need to test whether document[1] is read only.

    Tuesday, August 7, 2012 8:30 AM
  • "It seems that the 'ActiveDocument', by definition, is one that is editable (i.e. not read only)"

    What kind of read-only.  If it is the recommended read-only (and you respond yes), then it shows as Read Only on the title bar, but it IS editable, and the document count = 1.  Are we talking about a Windows file system read-only?


    Word MVP


    • Edited by fumei2 Thursday, August 9, 2012 4:45 AM
    Thursday, August 9, 2012 4:44 AM
  • It is not read only it is opened from network location once opened Word Displays that notification message that I've mentioned above although this problem is eliminated once I disable these 2 options.


    Adeel

    Thursday, August 9, 2012 5:29 AM