1

I created a function to open any valid file now I want to close the opened file. Input file could be of any valid file type: .docx, .bmp, xlsx, etc.

I was trying to make WScript object and trying to close file using below code, but it's not working.

Public Function openfile(filepath)
    Set ws = CreateObject("WScript.Shell")
    ws.Run filepath 
    'taking the screen shot of opened file 
End function

Public Function closefile(filepath)
    Set ws = CreateObject("WScript.Shell")
    ws.AppActivate (filepath)
    ws.Terminate
End function

However, I am unable to close the opened file and getting error. File could be any valid file type which I will get to know only at run time. We already have a function to update the opened file for .txt and Excel.

Can anybody help me to find out how I can close already opened file irrespective of file type?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Gimmet
  • 97
  • 7
  • Take a look at this [solution](https://stackoverflow.com/a/39278494/2784816). You can search Win32_Process based on the CommandLine field where you can provide the filepath. – Pankaj Jaju Oct 18 '18 at 16:11
  • thanks for the solution. i am able to close the file. however still facing issue to close pdf file type. – Gimmet Oct 23 '18 at 06:25

0 Answers0