How to close an open file by name

TrainingExcellence

New Member
Joined
Apr 6, 2017
Messages
10
Office Version
  1. 365
I've searched for quite some time for a solution to this problem. All the answers I see appear more complicated than they should be, but perhaps that is simply the nature of the task.

I am trying to figure out the code necessary to close a file (i.e. PDF or Word document) by name. I figured out how to open a file using its filepath, but is there a way to do the reverse?

Thanks!
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Trebor76

Well-known Member
Joined
Jul 23, 2007
Messages
4,735
Hi TrainingExcellence,

See how this goes:

VBA Code:
Option Explicit
Sub CloseFile()

    'https://www.mrexcel.com/board/threads/how-to-close-an-open-file-by-name.1167672
    
    'This macro will loop through each item in the Taskbar to determine if 'strFileName' is open.
    'If 'strFileName' is open it will close it and its associated program.
    'An instance to Word is needed as the Task object used in the code resides within it.
    
    Dim objWordApp As Object
    Dim objTask As Object
    Dim strFileName As String
    
    Application.ScreenUpdating = False
    
    strFileName = "My PDF File.pdf" 'Filename to be closed. Change to suit.
    
    On Error Resume Next
        Set objWordApp = GetObject(, "Word.Application")
        If Err.Number <> 0 Then
            Set objWordApp = CreateObject("Word.Application")
        End If
    On Error GoTo 0

    For Each objTask In objWordApp.Tasks
        'If the task name has the required file name as well as the program that opened it (just closing the file name will leave its associated program open), then...
        If InStr(objTask.Name, strFileName) > 0 And Len(objTask.Name) > Len(strFileName) Then
            '...close it and exit the loop.
            objTask.Close
            Set objTask = Nothing
            Exit For
        End If
    Next objTask
  
  Set objWordApp = Nothing
  
  Application.ScreenUpdating = True

End Sub

Regards,

Robert
 

TrainingExcellence

New Member
Joined
Apr 6, 2017
Messages
10
Office Version
  1. 365
Thanks for the code, @Trevor76, but unfortunately it didn't work. I debug.printed each of the objTask.name variables and none of them listed the file (in this case, a Word document called "Doc1.docx").

The code worked when I ran it for a PDF, and interestingly, the Immediate window showed far fewer items for the objTask.name variable when I ran the PDF than when I ran the Word doc.

Most of the time, I will be working with PDFs anyway, but I would like the code to be dynamic enough to handle other formats, such as Word documents.

Thanks!
 

Trebor76

Well-known Member
Joined
Jul 23, 2007
Messages
4,735
If "Doc1" is just a blank document that has not yet been saved you need to set the strFileName like so (i.e. no extension):

strFileName = "Doc1"

The code works for me :confused:
 

jsb1921

Board Regular
Joined
Aug 14, 2020
Messages
70
Office Version
  1. 2007
Platform
  1. Windows
  2. Web
TrainingExcellence, pl post ur code u used to open the file and any dim stt. then some one will try to post the code for closing. the code for closing depends on the code for open. there are atleast half a dozen ways to open a file in vba
 

Trebor76

Well-known Member
Joined
Jul 23, 2007
Messages
4,735
the code for closing depends on the code for open

Not sure about that. Consider the following that will open a text file called "Just a test.txt" from the C drive (in fact you can open file this way):

VBA Code:
Option Explicit
Sub OpenFile()

    CreateObject("Shell.Application").ShellExecute "C:\Just a test.txt"

End Sub

How can that one line help when trying to close it?
 

Watch MrExcel Video

Forum statistics

Threads
1,130,169
Messages
5,640,547
Members
417,151
Latest member
ChickenTenderer

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