Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Years ago I wrote an application in VB.NET that opens Word documents to make certain corrections to them, and it has been running smoothly until now.
The code is as follows:
VB.NET
Imports Microsoft.Office.Interop.Word
...
Friend WithEvents oWord As Word.Application
Friend WithEvents oWord As Word.Document
...
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Open(Trim(Main.docName.Text), varMissing, varMissing,
       varMissing, varMissing, varMissing, varMissing, varMissing,
       varMissing, varMissing, varMissing, varMissing, varMissing,
       varMissing, varMissing, varMissing)
...

(Of course, I added Microsoft.Office.Interop.Word.dll reference)

Days ago, the version of Word was automatically updated to 16 (previously 15) and from that moment the error occurs:
"You cannot cast the COM object from type Microsoft.Office.Interop.Word.ApplicationClass to the interface type Microsoft.Office.Interop.Word._Application. An operation error occurred because the QueryInterface call on the COM component with IID {00020970-0000-0000-C000-0000000000046} generated the following error: The item was not found. (Exception from HRESULT: 0x002802B (TYPE_E_ELEMENTNOTFOUND))"

in the line of code
VB
oWord.Visible=True

I have verified in the Task Manager that before the error a "Microsoft Word" task has been created.

Does anyone have any idea why?

What I have tried:

The "RegisteredApps" section of the registry lists "WordApplication.16", "WordApplication.14", and "Word.Application.8".
I tried replacing CreateObject("Word.Application") with
CreateObject("Word.Application.16") with no result.
Hours of internet searching have not offered me any solution either.
Posted
Updated 8-Aug-21 0:33am
v3

Looking at the variables, you seem to have defined oWord twice and oDoc is not defined.

So instead of
VB
Friend WithEvents oWord As Word.Application
Friend WithEvents oWord As Word.Document

try
VB
Friend WithEvents oWord As Word.Application
Friend WithEvents oDoc As Word.Document

Also it's probably a good idea to ensure that Option Explicit is on, see Option Explicit Statement - Visual Basic | Microsoft Docs[^]
 
Share this answer
 
Comments
Member 2237472 30-Jul-21 7:38am    
I am sorry, I made a translation error. The correct statements in the draft, as you suggest, are:

Public WithEvents oWord As Word.Application
Public WithEvents oDoc As Document

Thank you for your response.
This is not a solution, but...

I have proceeded to change the declarations of the variables, as follows:

Before:

<pre>Friend WithEvents oWord As Word.Application
Friend WithEvents oDoc As Document


Now:

Friend WithEvents oWord As Object
Friend WithEvents oDoc As Object


In the same way, I've modified the word and document application mapping code, as follows:

Before:

oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Open(Trim(Main.docName.Text), varMissing, varMissing,
       varMissing, varMissing, varMissing, varMissing, varMissing,
       varMissing, varMissing, varMissing, varMissing, varMissing,
       varMissing, varMissing, varMissing)


Now:

oword = ctype(createobject("word.application"), Microsoft.office.interop.word.application)
oDoc = CType(oWord.Documents.Open(Main.docName.Text, varMissing, varMissing,
      varMissing, varMissing, varMissing, varMissing, varMissing,
      varMissing, varMissing, varMissing, varMissing, varMissing,
      varMissing, varMissing, varMissing), Microsoft.Office.Interop.Word.Document)


with the result that it works. But with the change I lost the ability to handle the "DocumentBeforeClose" event, which prevented the document from being closed outside the control of the application.

Additionally, the following code, (which previously did not produce any error)

rngParagraphs = oWord.ActiveDocument.Range(Start:=oWord.ActiveDocument.Paragraphs(1). Range.Start, End:=oWord.ActiveDocument.Paragraphs(oWord.ActiveDocument.Paragraphs.Count). Range.End)


causes the "Count is read-only" error.

But replacing it with:

Dim nParg As Single = oWord.ActiveDocument.Paragraphs.Count
rngParagraphs = oWord.ActiveDocument.Range(Start:=oWord.ActiveDocument.Paragraphs(1). Range.Start, End:=oWord.ActiveDocument.Paragraphs(nParg). Range.End)

or what is the same, substituting .Activedocument.Paragraphs.Count for a variable in which the content has previously been loaded, works perfectly, which perplexes me.

He said that this is not a solution, just a palliative with problems. But I wanted to close the consultation with some more information.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900