Close a word document that is already open

Tornado1981

Board Regular
Joined
Apr 1, 2010
Messages
248
Hi,

I'm trying to check whether a specific word document is open or not. If open, then close it and first then reopen again and if not then just open it.

The problem is that I always get a Bad file name 4160 error when it tries to close the document.


Would anyone plz tell me where exactly is the problem in my code.


Thank u in advance.

Code:
Dim wdApp As Object
Dim myDoc As Word.Document
             
Set wdApp = GetObject(, "Word.Application")
            
If IsFileOpen(FilePath & "TemporaryLetter.docx") Then
    wdApp.Documents(FilePath & "TemporaryLetter.docx").Close
End If
            
With wdApp
    .Visible = True
    .WindowState = 2
End With
                
Set myDoc = wdApp.Documents.Open(FilePath & "TemporaryLetter.docx")
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Where is the rest of the code?
 
Upvote 0
The rest of the code is not important. The problem is in this part of the code.
If u r asking about the FilePath .. it was declared previously, and the IsFileOpen is a function to check whether the file is open or not.
Any suggestion will be really appreciated.
Thank u.
 
Upvote 0
Hello,
try:
Code:
For Each myDoc In wdApp.Documents
If myDoc.Name Like "TemporaryLetter.docx" Then wdApp.Documents(FilePath & "TemporaryLetter.docx").Close
Next
 
Upvote 0
You wouldn't include the file path when using Documents.

By the way, why don't you us GetObject's first argument which is the path to the file you want to 'get'?
 
Upvote 0
Where/when are you giving FilePath a value?

How/where is it declared?

Does the code work if you hard-code the path?
 
Upvote 0
Not discounting Norie's suggestion, but instead of

Code:
If IsFileOpen(FilePath & "TemporaryLetter.docx") Then
    wdApp.Documents(FilePath & "TemporaryLetter.docx").Close
End If

... perhaps

Code:
On Error Resume Next
wdApp.Documents("TemporaryLetter.docx").Close
On Error Goto 0
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top