I want to get the amount of comments in a word document. When the word documents only have text and comments my code works.
But when I run the (excel vba) code over a word document which includes tables then I get the runtime error 91 "Object variable or With block variable not set".
I don't know if this information is needed: When there is a table in the word document, then there are also comments to text in the table...
How can I change my code that it runs for every word document?
Thanks for your help in advance!
Now my code:
Dim appWord As Word.Application
Dim document As Word.Document
ThisWorkbook.Worksheets("Tabelle4").Activate
Set appWord = CreateObject("Word.Application")
' This word document just contains text and comments and with this document there is no runtime error 91
Set document = appWord.Documents.Open( _
ThisWorkbook.Path & "\Testfile_JustTextAndComments.docx")
' This word document contains tables, text and comments and with this document there arises a runtime error 91
'Set document = appWord.Documents.Open( _
'ThisWorkbook.Path & "\Testfile_TableTextAndComments.docx")
' Show the amount of comments in the word document
Range("A1") = document.Comments.Count ' when I click the debug-button of the runtime error 91 this line of code is marked (with Testfile_TableTextAndComments.docx)
document.Close wdDoNotSaveChanges
appWord.Quit
Set document = Nothing
Set appWord = Nothing
CodePudding user response:
Because of the runtime error 91 Object variable or With block variable not set it was suggested to me to check if the variable document
is empty (so thanks to all commenters for their contribution).
' check if word document is loaded on document
If document Is Nothing Then
MsgBox ("document not opened")
End If
And when I run in the error 91 then the document
was empty.
Here in Extract comments from multiple word docs into Excel I found a solution that works for me, so that the runtime error 91 was gone.
When opening the word file, I just have to add some arguments
Set dokument = appWord.Documents.Open(Filename:=ThisWorkbook.Path & "\Testfile_TableTextAndComments.docx", AddToRecentFiles:=False, ReadOnly:=True, Visible:=False)
Here you can find some additional hints and explanation to the Documents.Open method (Word)
And now the whole code:
Dim appWord As Word.Application
Dim document As Word.Document
ThisWorkbook.Worksheets("Tabelle4").Activate
Set appWord = CreateObject("Word.Application")
' This word document contains tables, text and comments and with the additional arguments the runtime error 91 doesn't appear anymore
Set document = appWord.Documents.Open( _
ThisWorkbook.Path & "\Testfile_TableTextAndComments.docx", _
AddToRecentFiles:=False, ReadOnly:=True, Visible:=False)
' check if word document is loaded on document - but this code snippet isn't needed anymore
If document Is Nothing Then
MsgBox ("document not opened")
End If
' Show the amount of comments in the word document
Range("A1") = document.Comments.Count '
document.Close wdDoNotSaveChanges
appWord.Quit
Set document = Nothing
Set appWord = Nothing