question

FranciscoCastillo-7558 avatar image
0 Votes"
FranciscoCastillo-7558 asked FranciscoCastillo-7558 published

Word automation fails

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:

Imports Microsoft.Office.Interop.Word
...
Friend WithEvents oWord As Word.Application
Friend WithEvents oWord As Word.Document
Dim varMissing As Object = Type.Missing
...
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 "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?

office-vba-dev
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

rectification.
The fourth line of code is:
Friend WithEvents oDoc As Word.Document
I'am sorry.

0 Votes 0 ·

Did you try some usual things, such as installing Windows and Office updates, restarting machine, removing and re-adding the references to Office assemblies, rebuilding the program?

(By the way, you can also try New Word.Application instead of CreateObject(“Word.Application”)).

0 Votes 0 ·

Although I did not say this for not lengthening the question, I have performed a clean installation of Windows 10, reinstalling Visual Studio and Office 365 again, but this has not solved the problem.
"oWord = New Word.Application (previously tested by me) produces the same error message.
Thank you for your reply.

0 Votes 0 ·

1 Answer

FranciscoCastillo-7558 avatar image
0 Votes"
FranciscoCastillo-7558 answered FranciscoCastillo-7558 published

This is not a solution, but...

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

Before:

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.