"Bad file name" Error Trying to Close a Word Document

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,566
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I thought this should be relatively easy ...
The code in blue is supposed to close the open instance of Word document "HPE-FR.docx". When it reaches the line in red, it throws this error ...

"Bad file name."

What needs to be done to resolve this problem? The file name matches the open document.

Rich (BB code):
Private Sub tglb_hpe_fldr_Click()
    Set WordApp = CreateObject("word.Application")
    With Me
        With .tglb_hpe_fldr
            If .BackColor = RGB(102, 0, 204) And .Value = False Then
                MsgBox "File is open! Close it!"
                WordApp.Documents("HPE-FR.docx").Close
                WordApp.Quit
                Set WordApp = Nothing
            End If
            If .BackColor = RGB(0, 153, 211) Then
                If DocOpen("HPE-FR.docx") = False Then
                    
                    WordApp.Documents.Open path_name & "HPE-FR.docx"
                    WordApp.Visible = True
                    Set WordApp = Nothing
                    'WordApp.Activate
                    .Value = True
                End If
                .BackColor = RGB(102, 0, 204)
                Exit Sub
            Else
                If .Value = True Then
                    .tb_of_rpt.Value = .tb_of_rpt.Value + 1
                Else
                    .tb_of_rpt.Value = .tb_of_rpt.Value - 1
                End If
            End If
        End With
    End With
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
You create a new Word instance each time, so there is no document open in it.
 
Upvote 0
Hi Rory, thank you for pointing me in the right direction, but I'm still stumped.

I moved
Rich (BB code):
Set WordApp = CreateObject("word.Application")
to
Rich (BB code):
              If DocOpen("HPE-FR.docx") = False Then
                    Set WordApp = CreateObject("word.Application")
                    WordApp.Documents.Open path_name & "HPE-FR.docx"

But now receive "Object variable or With block variable not set" error. My guess is that WordApp hasn't been set. How do I do that?

If Set WordApp = CreateObject("word.Application") creates a new Word document, I figure I need something to replace CreateObject?
 
Upvote 0
It's not especially clear what your workflow is but you probably want to use GetObject and pass the path of the document. If it returns Nothing, then the document isn't already open.
 
Upvote 0
Thanks Rory, thank you for your patience. Your help is very appreciated! I didn't explain at all in my OP what I am trying to do. Basically, close a Word document if it is open.

This is my adaptation, but clearly I;m not using GetObject properly. I get the same results whether the document is open or not. It does not recognize the document as being open when it is.

Rich (BB code):
If GetObject(path_name & "HPE-FR.docx") = True Then
    MsgBox "HPE_FR is open and needs to be closed"
    'close HPE_FR here
Else
    MsgBox "HPE_FR is closed."
End if
 
Last edited:
Upvote 0
You need to test if it's Nothing, not True/False.

Code:
If Not GetObject(path_name & "HPE-FR.docx") Is Nothing Then
    MsgBox "HPE_FR is open and needs to be closed"
    'close HPE_FR here
Else
    MsgBox "HPE_FR is closed."
End if

but you might as well just close it without the message?
 
Upvote 0
Ahhh ... thanks Rory. Just what I was hoping for.
The messages were just for testing purposes. I don't need them.
 
Upvote 0

Forum statistics

Threads
1,223,794
Messages
6,174,641
Members
452,575
Latest member
Fstick546

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