Karthik P B
Karthik P B

Reputation: 237

How to Close the Manually opened word document using Excel VBA

Before running the excel vba code, I have opened the Word document manually, is it possible to close that particular Word Document which is opened through Excel VBA

Upvotes: 0

Views: 715

Answers (1)

SierraOscar
SierraOscar

Reputation: 17647

Assuming you don't already have a hook on the Word app, something like this should do the trick:

Set wordApp = GetObject(, "Word.Application")

    For Each wdDoc In wordApp.Documents
        If wdDoc.Name = "close_me.docx" Then '// rename to your doc
            wdDoc.Close SaveChange:=False '// change to true if required
            DoEvents
            Exit For
        End If
    Next

Upvotes: 2

Related Questions