none
Word Application

    Question

  • Hello,

    I am trying to create an object of

    WordApp =

    new Microsoft.Office.Interop.Word.Application();

    but it throws following exception:

    '((Microsoft.Office.Interop.Word.ApplicationClass)(WordApp)).ActiveDocument' threw an exception of type 'System.Runtime.InteropServices.COMException'

     

    How to get rid of this?

    Thanks in advance


    -Thanks & Regards,
    Wednesday, August 25, 2010 12:44 PM

Answers

  • Hi Sweety,

    Try this 

    First remove all the assemblies whatever you added (libraries) from the web.config except default assemblies or remove all the references from the project.

     

    right click on the solution explorer - Add reference - go to the browse tab - go to the c:\Program Files\Microsoft visual studio 9.0\visual studio tools for office\PIA\Office11\- select microsoft.office.interop.word.dll. 

    • Marked as answer by sweety1112 Friday, August 27, 2010 8:45 AM
    Thursday, August 26, 2010 1:24 PM

All replies

  • Hi sweety

    Please show a larger segment of code, including the code that's triggering the error message and your declaration of the object WordApp. The code you show us won't trigger an error concerning ActiveDocument.

    Also, which version of Word is invoved?


    Cindy Meister, VSTO/Word MVP
    Wednesday, August 25, 2010 4:02 PM
  • Hi,

     Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application(); ;     
          
    
          oWord.Visible = false;
         // oWord.ShowWindowsInTaskbar = false;
         Microsoft.Office.Interop.Word._Document oDoc = oWord.Documents.Add(ref emptyItem, ref emptyItem,
          ref emptyItem, ref oFalse);
          oDoc.Words.First.InsertBefore(words);
          object nothing = Type.Missing;
          object objLanguage = Microsoft.Office.Interop.Word.WdLanguageID.wdEnglishUS;
          //ask MS Word to spell check the given word 
          
          bool spelledright = oWord.CheckSpelling( word, ref nothing, ref oIgnoreUpperCase, ref objLanguage, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing ); 
          if (spelledright) return null;
          //if word is spelled wrong, ask MS Word to suggest 
          //other similar words.
          ArrayList wordList = new ArrayList();
          
    //Exception is generated by this statement.
          Microsoft.Office.Interop.Word.SpellingSuggestions suggestions = oWord.GetSpellingSuggestions( word, ref nothing, ref nothing, ref objLanguage, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing ); 
         
         
    

    Also when I add an watch over oWord it shows me the same error and so all the properties are filled with exception.

    I have referenced the Microsft office 12.0 Object Library and I have Word 2007 installed.


    -Thanks & Regards,
    Thursday, August 26, 2010 4:52 AM
  • Hi Sweety,

    Try this 

    First remove all the assemblies whatever you added (libraries) from the web.config except default assemblies or remove all the references from the project.

     

    right click on the solution explorer - Add reference - go to the browse tab - go to the c:\Program Files\Microsoft visual studio 9.0\visual studio tools for office\PIA\Office11\- select microsoft.office.interop.word.dll. 

    • Marked as answer by sweety1112 Friday, August 27, 2010 8:45 AM
    Thursday, August 26, 2010 1:24 PM
  • Hi Sweety

    FWIW I haven't been able to get GetSpellingSuggestions to work at all in Word 2007, not even with Word's native VBA. The following test, for example, throws the error

    "Cannot find proofing tools for" followed by three unicode symbols, the first of which is an upwards pointing arrow, the other two undefinable.

    Sub SpellingSuggestions()
        Dim suggestions As word.SpellingSuggestions
        Dim word As String
       
        word = "casee"
        Set suggestions = Application.GetSpellingSuggestions(word)
        Debug.Print suggestions.Count
    End Sub

    It doesn't matter which parameters I set (or don't). As far as I can tell, it's broken...


    Cindy Meister, VSTO/Word MVP
    Thursday, August 26, 2010 2:08 PM
  • Cindy, your code does work for me, in Word 2007 (English US only) on Windows XP. Tonight I can try it in Word 2007 on Windows 7 Ultimate and post back. I wonder, though, whether the problem might be related to having more than one language enabled, which I don't have.

    BTW, adding the line

        If suggestions.Count Then Debug.Print suggestions(1).Name

    prints the word 'case'.

    Cindy Meister wrote:

    Hi Sweety

    FWIW I haven't been able to get GetSpellingSuggestions to work at all
    in Word 2007, not even with Word's native VBA. The following test,
    for example, throws the error

    "Cannot find proofing tools for" followed by three unicode symbols,
    the first of which is an upwards pointing arrow, the other two
    undefinable.

    Sub SpellingSuggestions()
    Dim suggestions As word.SpellingSuggestions
    Dim word As String

    word = "casee"
    Set suggestions = Application.GetSpellingSuggestions(word)
    Debug.Print suggestions.Count
    End Sub

    It doesn't matter which parameters I set (or don't). As far as I can
    tell, it's broken...


    Jay Freedman
    MS Word MVP  FAQ: http://word.mvps.org
    Thursday, August 26, 2010 2:24 PM
  • <<Cindy, your code does work for me, in Word 2007 (English US only) on Windows XP. Tonight I can try it in Word 2007 on Windows 7 Ultimate and post back. I wonder, though, whether the problem might be related to having more than one language enabled, which I don't have.>>

    that's very interesting...

    I tested this already a couple of months ago, Win7 & Office 10, all German. Got the same error when I specified a dicationary. It worked (then) asking for just the suggestions, without giving any parameters. But not so today.

    My test environment today was Windows Vista and Office 2007, the US English versions. But as you know, I do have multiple languages activated.

    I know this used to work, because I worked through it with someone a couple of years ago in one of the office.developer newsgroups. And I kept an extract of the final solution. But seeing those weird (unicode, I assume) characters in the error message... <GAGH>


    Cindy Meister, VSTO/Word MVP
    Thursday, August 26, 2010 3:11 PM
  • It's OK here on Word 2007+Vista (32 bit) with my normal primary editing language (English UK), French and Spanish (which I think are the three languages provided with proofing tools as standard for this edition. The other languages I've tried, including German (Germany) will cause the same error that Cindy is seeing.

    BTW, if you feel compelled to mess around with your editing languages, don't forget to save your normal.dotm as Word will create a new one.

     


    Peter Jamieson
    Thursday, August 26, 2010 3:19 PM
  • I got the same error that sweety reported in the following line of my code:

          _word = new Microsoft.Office.Interop.Word.Application();

    Even though the exception is not caught i can see inside _word that an exception was thrown ...this exception makes my AddIn to not load anymore since i can see the registry key LoadBehavior = 2 ...any ideas? Tried to clean the references and add them again but nothing, I am working on Word 2007 in a 64 bits Vista pc. Any help would be helpful          

    private void CreateNewDoc()

            {

                try

                {

                    _word = new Microsoft.Office.Interop.Word.Application();

                    _word.Visible = false;

                    _doc = _word.Documents.Add(ref _missing, ref _missing,

                        ref _missing, ref _missing);

                }

                catch (COMException ex)

                {

                    Log.WriteEntry("XPM", ex.StackTrace + " " + ex.Message, 3);

     

                }

     

            }

    Wednesday, October 20, 2010 7:51 PM