locked
Creating new Word object - COMexception error RRS feed

  • Question

  • Am a complete newcomer to VS2005. I have a reasonable knowledge of Access Visual Basic.
    Am trying to develop a very simple Windows application for use on local machines (not on the server).
    I have a simple form with the following coding:

    Imports System.IO
    Imports Microsoft.Office.Interop.Word
    Public Class Form1

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Label1.Text = "Printing Letters to Schools"
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            'Create new document based on template
            Dim strTemplateFile As System.String = "C:\Web Design\LIFEnet\LifeMergeTemplate.dot"
            Dim WordtemplateFile As New _
               System.IO.FileInfo(strTemplateFile)

            ' See if the file specified exists
            If (WordtemplateFile.Exists) Then
                Dim wordApplication As New Microsoft.Office.Interop.Word.Application

                ' For improved performance and to prevent screen flashing set visibility to false
                wordApplication.Visible = False

                ' Add the template to the new word application
                Dim wordTemplate As Microsoft.Office.Interop.Word.Document = wordApplication.Documents.Add( _
                 CType(strTemplateFile, Object), , , False)

                wordApplication.Visible = True

                ' Activate the new wordTemplate document
                wordTemplate.Activate()
            End If


        End Sub
    End Class

    The problem is when I run the application with Debug.Start Debugging and click on the button I get a "Com Exception was unhandled" error (80040154) on the line:

    Dim wordApplication As New Microsoft.Office.Interop.Word.Application

    Have added a  reference to the Microsoft Word 11.0 Object Library and there is one to Microsoft Office 11.0 Object Library.

    Can anyone tell me what is wrong? There are no errors shown on the Error List.
    Am a complete newcomer to VS2005. I have a reasonable knowledge of Access Visual Basic.
    Am trying to develop a very simple Windows application for use on local machines (not on the server).
    I have a simple form with the following coding:

    Imports System.IO
    Imports Microsoft.Office.Interop.Word
    Public Class Form1

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Label1.Text = "Printing Letters to Schools"
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            'Create new document based on template
            Dim strTemplateFile As System.String = "C:\Web Design\LIFEnet\LifeMergeTemplate.dot"
            Dim WordtemplateFile As New _
               System.IO.FileInfo(strTemplateFile)

            ' See if the file specified exists
            If (WordtemplateFile.Exists) Then
                Dim wordApplication As New Microsoft.Office.Interop.Word.Application

                ' For improved performance and to prevent screen flashing set visibility to false
                wordApplication.Visible = False

                ' Add the template to the new word application
                Dim wordTemplate As Microsoft.Office.Interop.Word.Document = wordApplication.Documents.Add( _
                 CType(strTemplateFile, Object), , , False)

                wordApplication.Visible = True

                ' Activate the new wordTemplate document
                wordTemplate.Activate()
            End If


        End Sub
    End Class

    The problem is when I run the application with Debug.Start Debugging and click on the button I get a "Com Exception was unhandled" error (80040154) on the line:

    Dim wordApplication As New Microsoft.Office.Interop.Word.Application

    Have added a  reference to the Microsoft Word 11.0 Object Library and there is one to Microsoft Office 11.0 Object Library.

    Can anyone tell me what is wrong? There are no errors shown on the Error List.
    Friday, March 30, 2007 8:45 PM

Answers

  • Hi Brian,

     

    when you run this vbs snippet and it doesn't work it means you have no Microsoft Word installed.

    It has nothing todo with .Net or VisualStudio or VSTO.

    I would recommend to reinstall Microsoft Office - or use the repair function of Add/Remove Software.

     

    Or maybe it's an Antivirus Programm that prevents you to invoke this Method - but you said you disabled it.

    Also try to post in a Office/Word Forum and try to get an answer.

     

    Greets, Helmut

     

    Sunday, April 1, 2007 4:11 PM
    Answerer

All replies

  • Hello bogorman,

     

    you can try this - it will get the wordinstance by latebound:

     

    Dim wordApp As Microsoft.Office.Interop.Word.Application

    wordApp = Activator.CreateInstance(Type.GetTypeFromProgID("Word.Application"))

    wordApp.Visible = True

     

    And if this is not working, you may have norton Antivirus installed.

    Maybe it prevents you from automating word.

     

    Hope this helps,

    greets, Helmut

    Saturday, March 31, 2007 5:02 PM
    Answerer
  • Hi Helmut,

    Thanks for your help. Unfortunately I STILL get the error.

    In fact I have Kaspersky AV installed. Have "paused" it but that makes no difference.

    Is there anything else you can suggest - it's driving me mad!!

    I did read somewhere about registering a DLL with Regsvr32. Is this relevant? (am probably talking nonsense)

     

    The whole error message reads:

    Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80040154.

    Would be grateful for any more help you can give me.

    Regards

    Brian

     

    Saturday, March 31, 2007 6:06 PM
  • Hi Brian,

     

    could you paste this into to notepad and save it as .vbs file

    then doubleclick the .vbs file and see if word pops up.

     

    Dim word

    set word = createObject("Word.Application")

    word.Visible = true

    set word = nothing

     

    If this is working, maybe reinstall the Office PIA's

    Another question, do you compile and run this code directly from visualstudio or do you copy the application to another directory ?

    Maybe you don't have permission to call COM objects from there.

     

    Greets, Helmut

    Saturday, March 31, 2007 6:35 PM
    Answerer
  • Hi Helmut,

    When I open the file, I get an error message on line 2:

    ActiveX component can't create object: 'Word.Application'

     

    Re your other question. I normally run the code directly from VS using Debug/Start Debugging.

     

    Does the above error give you any other ideas?

    Do I need to set a permission from the directory your code is in? If so, how do I do that?

    Regards

    Brian

     

    Sunday, April 1, 2007 3:38 PM
  • Hi Brian,

     

    when you run this vbs snippet and it doesn't work it means you have no Microsoft Word installed.

    It has nothing todo with .Net or VisualStudio or VSTO.

    I would recommend to reinstall Microsoft Office - or use the repair function of Add/Remove Software.

     

    Or maybe it's an Antivirus Programm that prevents you to invoke this Method - but you said you disabled it.

    Also try to post in a Office/Word Forum and try to get an answer.

     

    Greets, Helmut

     

    Sunday, April 1, 2007 4:11 PM
    Answerer
  • Hi Helmut,

    Wonderful - you've solved it!!

    It was corruption in Office. Have repaired it and it works fine now.

    Thanks, so much. I was really getting desperate!!

    All my thanks, again.

    Regards

    Brian

     

    Sunday, April 1, 2007 5:39 PM