greddin
asked on
Microsoft.Visualbasic DLL Error Message
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
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
ASKER
Here is the complete code:
Imports Word = Microsoft.Office.Interop.W ord
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.Applica tion")
oWord.Visible = True
oDoc = oWord.Documents.Add
'Insert a paragraph at the beginning of the document.
oPara1 = oDoc.Content.Paragraphs.Ad d
oPara1.Range.Text = "Heading 1"
oPara1.Range.Font.Bold = True
oPara1.Format.SpaceAfter = 24 '24 pt spacing after paragraph.
oPara1.Range.InsertParagra phAfter()
'Insert a paragraph at the end of the document.
'** \endofdoc is a predefined bookmark.
oPara2 = oDoc.Content.Paragraphs.Ad d(oDoc.Boo kmarks.Ite m("\endofd oc").Range )
oPara2.Range.Text = "Heading 2"
oPara2.Format.SpaceAfter = 6
oPara2.Range.InsertParagra phAfter()
'Insert another paragraph.
oPara3 = oDoc.Content.Paragraphs.Ad d(oDoc.Boo kmarks.Ite m("\endofd oc").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.InsertParagra phAfter()
'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.Bookm arks.Item( "\endofdoc ").Range, 3, 5)
oTable.Range.ParagraphForm at.SpaceAf ter = 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.Itali c = True
'Add some text after the table.
'oTable.Range.InsertParagr aphAfter()
oPara4 = oDoc.Content.Paragraphs.Ad d(oDoc.Boo kmarks.Ite m("\endofd oc").Range )
oPara4.Range.InsertParagra phBefore()
oPara4.Range.Text = "And here's another table:"
oPara4.Format.SpaceAfter = 24
oPara4.Range.InsertParagra phAfter()
'Insert a 5 x 2 table, fill it with data, and change the column widths.
oTable = oDoc.Tables.Add(oDoc.Bookm arks.Item( "\endofdoc ").Range, 5, 2)
oTable.Range.ParagraphForm at.SpaceAf ter = 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).Wid th = oWord.InchesToPoints(2) 'Change width of columns 1 & 2
oTable.Columns.Item(2).Wid th = 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("\endo fdoc").Ran ge.InsertP aragraphAf ter()
Do
oRng = oDoc.Bookmarks.Item("\endo fdoc").Ran ge
oRng.ParagraphFormat.Space After = 6
oRng.InsertAfter("A line of text")
oRng.InsertParagraphAfter( )
Loop While Pos >= oRng.Information(Word.WdIn formation. wdVertical PositionRe lativeToPa ge)
oRng.Collapse(Word.WdColla pseDirecti on.wdColla pseEnd)
oRng.InsertBreak(Word.WdBr eakType.wd PageBreak)
oRng.Collapse(Word.WdColla pseDirecti on.wdColla pseEnd)
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("\endo fdoc").Ran ge.InlineS hapes.AddO LEObject( _
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("\endo fdoc").Ran ge
oRng.InsertParagraphAfter( )
oRng.InsertAfter("THE END.")
'All done. Close this form.
Me.Close()
End Sub
Imports Word = Microsoft.Office.Interop.W
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.Applica
oWord.Visible = True
oDoc = oWord.Documents.Add
'Insert a paragraph at the beginning of the document.
oPara1 = oDoc.Content.Paragraphs.Ad
oPara1.Range.Text = "Heading 1"
oPara1.Range.Font.Bold = True
oPara1.Format.SpaceAfter = 24 '24 pt spacing after paragraph.
oPara1.Range.InsertParagra
'Insert a paragraph at the end of the document.
'** \endofdoc is a predefined bookmark.
oPara2 = oDoc.Content.Paragraphs.Ad
oPara2.Range.Text = "Heading 2"
oPara2.Format.SpaceAfter = 6
oPara2.Range.InsertParagra
'Insert another paragraph.
oPara3 = oDoc.Content.Paragraphs.Ad
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.InsertParagra
'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.Bookm
oTable.Range.ParagraphForm
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.
oTable.Rows.Item(1).Range.
'Add some text after the table.
'oTable.Range.InsertParagr
oPara4 = oDoc.Content.Paragraphs.Ad
oPara4.Range.InsertParagra
oPara4.Range.Text = "And here's another table:"
oPara4.Format.SpaceAfter = 24
oPara4.Range.InsertParagra
'Insert a 5 x 2 table, fill it with data, and change the column widths.
oTable = oDoc.Tables.Add(oDoc.Bookm
oTable.Range.ParagraphForm
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).Wid
oTable.Columns.Item(2).Wid
'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("\endo
Do
oRng = oDoc.Bookmarks.Item("\endo
oRng.ParagraphFormat.Space
oRng.InsertAfter("A line of text")
oRng.InsertParagraphAfter(
Loop While Pos >= oRng.Information(Word.WdIn
oRng.Collapse(Word.WdColla
oRng.InsertBreak(Word.WdBr
oRng.Collapse(Word.WdColla
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("\endo
ClassType:="MSGraph.Chart.
:="", 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("\endo
oRng.InsertParagraphAfter(
oRng.InsertAfter("THE END.")
'All done. Close this form.
Me.Close()
End Sub
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\{0 00209FF-00 00-0000-C0 00-0000000 00046}\Pro gID
HKEY_CLASSES_ROOT\CLSID\{0 00209FF-00 00-0000-C0 00-0000000 00046}\Ver sionIndepe ndentProgI D
HKEY_CLASSES_ROOT\CLSID\{0
HKEY_CLASSES_ROOT\CLSID\{0
ASKER
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.
For VersionIndependentProgID just says: Word.Application
I'm running Windows 2000 Professional with Office 2000 which is Word 9.0 I think.
Does this occur on the development or a production machine?
ASKER
I'm on my local machine. Development I guess. But if I can get this to work, I would eventually move to production.
AS TheLearnedOne told you use late binding
objWord = CreateObject("Word.Applica tion")
objDoc = CreateObject("Word.Documen t")
objWord.Visible = True
objWord.WindowState = WORD_STATE_MAXIMIZE
Try
objDoc = objWord.Documents.Open(m_s trFilePath & "\" & m_strFileName)
Catch
MessageBox.Show("Document couldn't be opened")
End Try
that will open a word doc
objWord = CreateObject("Word.Applica
objDoc = CreateObject("Word.Documen
objWord.Visible = True
objWord.WindowState = WORD_STATE_MAXIMIZE
Try
objDoc = objWord.Documents.Open(m_s
Catch
MessageBox.Show("Document couldn't be opened")
End Try
that will open a word doc
ASKER
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.Documen t")
Does this line need to be added to my code?
What is the reason to use late binding?
Thanks,
-Greg
objDoc = CreateObject("Word.Documen
Does this line need to be added to my code?
What is the reason to use late binding?
Thanks,
-Greg
ASKER
If I'm not mistaken, it looks like the MSDN code above is already doing late binding. Correct?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Option 1: Use late-binding. Option 2: Use early-binding in Debug mode, and late-binding in Release Mode.
Bob