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

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

shg

MrExcel MVP
Joined
May 7, 2008
Messages
21,836
Office Version
  1. 2010
Platform
  1. Windows
Where is the rest of the code?
 
Upvote 0

Tornado1981

Board Regular
Joined
Apr 1, 2010
Messages
248
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

andrewkard

Active Member
Joined
Apr 6, 2012
Messages
455
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

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,367
Office Version
  1. 365
Platform
  1. Windows
ADVERTISEMENT
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

Tornado1981

Board Regular
Joined
Apr 1, 2010
Messages
248
ADVERTISEMENT
Same result when I tried all ur suggestions .. Error 4160 :(
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,367
Office Version
  1. 365
Platform
  1. Windows
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

shg

MrExcel MVP
Joined
May 7, 2008
Messages
21,836
Office Version
  1. 2010
Platform
  1. Windows
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,195,672
Messages
6,011,083
Members
441,581
Latest member
rp4717

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
Top