Reputation: 1
I'm trying to print text with 6 textboxes to word But the program stops and gives me this message enter image description here System.InvalidCastException: 'Unable to cast COM object of type 'Microsoft.Office.Interop.Word.ApplicationClass' to interface type 'Microsoft.Office.Interop.Word._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00020970-0000-0000-C000-000000000046}' failed due to the following error: Element not found. (Exception from HRESULT: 0x8002802B (TYPE_E_ELEMENTNOTFOUND)).'
What is the solution to this message please
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Create Word Application
Dim oWord As Word.Application = CreateObject("Word.Application")
' Open existing word document
Dim oDoc As Word.Document = oWord.Documents.Open("D:\myfile.doc")
Dim oTable As Word.Table
oWord.Visible = True
'Insert a table with 1 row x 5 columns and fill it with five TextBox.Text
Dim r As Integer, c As Integer
oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 3, 6)
oTable.Range.ParagraphFormat.SpaceAfter = 6
oTable.Cell(1, 1).Range.Text = TxtBook.Text
oTable.Cell(1, 2).Range.Text = TxtAuthor.Text
oTable.Cell(1, 3).Range.Text = TxtPublisher.Text
oTable.Cell(1, 4).Range.Text = TxtPublish.Text
oTable.Cell(1, 5).Range.Text = txtPage.Text
oTable.Cell(1, 6).Range.Text = TxtNote.Text
' Save this word document
oDoc.SaveAs("D:\myfile.doc", True)
oDoc.Close()
oWord.Application.Quit()
End Sub
Upvotes: 0
Views: 2669
Reputation: 927
oWord.Documents is a collection of documents, not a document itself. try
Dim oDoc As Word.Document = oWord.Documents.Add("D:\myfile.doc")
EDIT:
Although this first answer is necessary, it is not sufficient because you have a problem of incorrect COM Interop settings in the Windows registry :
to check COM installation have a look to this registry key (64bit Office:) :
HKEY_CLASSES_ROOT\WOW6432Node\Interface\[00020970-0000-0000-C000-000000000046]\TypeLib
(on my computer I see this)
default Value = {00020905-0000-0000-C000-000000000046}
Version Value = 8.5
then browse to the registry key
HKEY_CLASSES_ROOT\WOW6432Node\TypeLib\{00020905-0000-0000-C000-000000000046}`
you must find a subkey with same name than last version value ("8.5" in my case) with this content :
default Value = "Microsoft Word 14.0 Object Library"
PrimaryInteropAssemblyName = "Microsoft.Office.Interop.Word, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"
I made a screen image to help you :
if you don't find this keys or find multiple keys 8.5, 8.6 etc..., you must repair office installation or reinstall it.
Upvotes: 0