Solved

Microsoft.Visualbasic DLL Error Message

Posted on 2004-03-30
10
550 Views
Last Modified: 2007-12-19
I am trying to follow a Microsoft "How to" document entitled "HOW TO: Automate Word from Visual Basic .NET to Create a New Document".  The code they provide is at the following url: http://support.microsoft.com/default.aspx?scid=kb;EN-US;316383

When I try to run this I get the following error message:

An unhandled exception of type 'System.Exception' occurred in microsoft.visualbasic.dll
Additional information: Cannot create ActiveX component.

The "How to" document wants me to add a reference to the "Microsoft Word 10.0 Object Library" but all I have is "Microsoft Word 9.0 Object Library".

I'm looking for something to help rid me of this error message.

-Greg
0
Comment
Question by:greddin
  • 5
  • 4
10 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10716610
Some code would help.  

Option 1:  Use late-binding.  Option 2:  Use early-binding in Debug mode, and late-binding in Release Mode.

Bob
0
 

Author Comment

by:greddin
ID: 10716631
Here is the complete code:

Imports Word = Microsoft.Office.Interop.Word
Private Sub Button1_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Button1.Click

        Dim oWord As Word.Application
        Dim oDoc As Word.Document
        Dim oTable As Word.Table
        Dim oPara1 As Word.Paragraph, oPara2 As Word.Paragraph
        Dim oPara3 As Word.Paragraph, oPara4 As Word.Paragraph
        Dim oRng As Word.Range
        Dim oShape As Word.InlineShape
        Dim oChart As Object
        Dim Pos As Double

        'Start Word and open the document template.
        oWord = CreateObject("Word.Application")
        oWord.Visible = True
        oDoc = oWord.Documents.Add

        'Insert a paragraph at the beginning of the document.
        oPara1 = oDoc.Content.Paragraphs.Add
        oPara1.Range.Text = "Heading 1"
        oPara1.Range.Font.Bold = True
        oPara1.Format.SpaceAfter = 24    '24 pt spacing after paragraph.
        oPara1.Range.InsertParagraphAfter()

        'Insert a paragraph at the end of the document.
        '** \endofdoc is a predefined bookmark.
        oPara2 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
        oPara2.Range.Text = "Heading 2"
        oPara2.Format.SpaceAfter = 6
        oPara2.Range.InsertParagraphAfter()

        'Insert another paragraph.
        oPara3 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
        oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:"
        oPara3.Range.Font.Bold = False
        oPara3.Format.SpaceAfter = 24
        oPara3.Range.InsertParagraphAfter()

        'Insert a 3 x 5 table, fill it with data, and make the first row
        'bold and italic.
        Dim r As Integer, c As Integer
        oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 3, 5)
        oTable.Range.ParagraphFormat.SpaceAfter = 6
        For r = 1 To 3
            For c = 1 To 5
                oTable.Cell(r, c).Range.Text = "r" & r & "c" & c
            Next
        Next
        oTable.Rows.Item(1).Range.Font.Bold = True
        oTable.Rows.Item(1).Range.Font.Italic = True

        'Add some text after the table.
        'oTable.Range.InsertParagraphAfter()
        oPara4 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
        oPara4.Range.InsertParagraphBefore()
        oPara4.Range.Text = "And here's another table:"
        oPara4.Format.SpaceAfter = 24
        oPara4.Range.InsertParagraphAfter()

        'Insert a 5 x 2 table, fill it with data, and change the column widths.
        oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 5, 2)
        oTable.Range.ParagraphFormat.SpaceAfter = 6
        For r = 1 To 5
            For c = 1 To 2
                oTable.Cell(r, c).Range.Text = "r" & r & "c" & c
            Next
        Next
        oTable.Columns.Item(1).Width = oWord.InchesToPoints(2)   'Change width of columns 1 & 2
        oTable.Columns.Item(2).Width = oWord.InchesToPoints(3)

        'Keep inserting text. When you get to 7 inches from top of the
        'document, insert a hard page break.
        Pos = oWord.InchesToPoints(7)
        oDoc.Bookmarks.Item("\endofdoc").Range.InsertParagraphAfter()
        Do
            oRng = oDoc.Bookmarks.Item("\endofdoc").Range
            oRng.ParagraphFormat.SpaceAfter = 6
            oRng.InsertAfter("A line of text")
            oRng.InsertParagraphAfter()
        Loop While Pos >= oRng.Information(Word.WdInformation.wdVerticalPositionRelativeToPage)
        oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
        oRng.InsertBreak(Word.WdBreakType.wdPageBreak)
        oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
        oRng.InsertAfter("We're now on page 2. Here's my chart:")
        oRng.InsertParagraphAfter()

        'Insert a chart and change the chart.
        oShape = oDoc.Bookmarks.Item("\endofdoc").Range.InlineShapes.AddOLEObject( _
            ClassType:="MSGraph.Chart.8", FileName _
            :="", LinkToFile:=False, DisplayAsIcon:=False)
        oChart = oShape.OLEFormat.Object
        oChart.charttype = 4 'xlLine = 4
        oChart.Application.Update()
        oChart.Application.Quit()
        'If desired, you can proceed from here using the Microsoft Graph
        'Object model on the oChart object to make additional changes to the
        'chart.
        oShape.Width = oWord.InchesToPoints(6.25)
        oShape.Height = oWord.InchesToPoints(3.57)

        'Add text after the chart.
        oRng = oDoc.Bookmarks.Item("\endofdoc").Range
        oRng.InsertParagraphAfter()
        oRng.InsertAfter("THE END.")

        'All done. Close this form.
        Me.Close()

    End Sub
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10716885
Look through the Windows registry for Word.Application.  You should find a version ProgID and a version-independent ProgID.  I have Word XP, so it found Word.Application.10, and Word.Application for the version-independent ProgID.  If you don't have the ProgID correct for that machine, then you will get the ActiveX error.

HKEY_CLASSES_ROOT\CLSID\{000209FF-0000-0000-C000-000000000046}\ProgID
HKEY_CLASSES_ROOT\CLSID\{000209FF-0000-0000-C000-000000000046}\VersionIndependentProgID
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:greddin
ID: 10716977
For ProgID mine says: Word.Application.9
For VersionIndependentProgID just says: Word.Application

I'm running Windows 2000 Professional with Office 2000 which is Word 9.0 I think.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10717042
Does this occur on the development or a production machine?
0
 

Author Comment

by:greddin
ID: 10717097
I'm on my local machine. Development I guess. But if I can get this to work, I would eventually move to production.
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 10717153
AS TheLearnedOne told you use late binding

objWord = CreateObject("Word.Application")
objDoc = CreateObject("Word.Document")
objWord.Visible = True
objWord.WindowState = WORD_STATE_MAXIMIZE

Try
    objDoc = objWord.Documents.Open(m_strFilePath & "\" & m_strFileName)
Catch
    MessageBox.Show("Document couldn't be opened")
End Try

that will open a word doc
0
 

Author Comment

by:greddin
ID: 10717218
I will have to learn exactly what late binding is. All I was trying to do was follow the code sample from MSDN. It said nothing about late binding. In your code above your have the line:

objDoc = CreateObject("Word.Document")

Does this line need to be added to my code?

What is the reason to use late binding?

Thanks,
-Greg
0
 

Author Comment

by:greddin
ID: 10717360
If I'm not mistaken, it looks like the MSDN code above is already doing late binding. Correct?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 10718113
Yes, the code is using late-binding, but it appears that there is a mix between early and late binding.  With early binding, you need a reference to the Word library, but it is version specific reference to a type library with a particular GUID.  Early binding gives you the ability for AutoCompletion and Intellisense.  The compiler knows about properties and methods, so it can trap code errors.  Late-binding makes it easier to access type libraries that have the same ProgID, but different GUIDs (like Word 2000 and Word XP).  The compiler can't predetermine properties and method signatures, so it can only trap errors at run-time.  

The error 'ActiveX cannot create object' is broad.  I can mean that the ProgID is incorrect, or the type-library is not registered correctly.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Help 27 67
Function after success of Jquery/AJAX action 9 42
SSIS On fail action 5 48
How to use Visual Studio's AddArgument method found in the PowerShell Namespace 2 30
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question