Programmer
Programmer

Reputation: 159

If word.ActiveDocument exists close it [VBA]

Here's my code:

    If Not (word.ActiveDocument Is Nothing) Then
          word.ActiveDocument.Close SaveChanges:=wdSaveChanges
    End If

I get an error if there is no active document. How can I prevent this?

Upvotes: 0

Views: 982

Answers (1)

Erik A
Erik A

Reputation: 32682

You can check if there are open documents in the following way:

If word.Documents.Count Then '0 is falsy, all other values are truthy
      word.ActiveDocument.Close SaveChanges:=wdSaveChanges
End If

Upvotes: 2

Related Questions