+ Reply to Thread
Results 1 to 7 of 7

How Do I Close Any Open File

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Question How Do I Close Any Open File

    I want to be able to be able to close any open file. How do I do this?

    Idea #1 = I've been puzzling over the Help article for Lock and Unlock statements. It would appear that it is possible to close any file in theory? I cant get it to work.

    Idea #2 = If I could be certain that the current window is displaying the open filename (or if I could force this window to be the active window), I could use SendKeys to send Alt + F4 to close the file? (Not the best solution but if Idea #1 doesn't work...)



    Below is what I've been playing around with.

    Please Login or Register  to view this content.
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: How Do I Close Any Open File

    Kills a process based on it's title...
    Please Login or Register  to view this content.
    Title needs to be an Exact match - there are alternatives to search for a Window based on a partial title, but just took the easiest option (I had the code already).

    This kills a process, not just close the application - there will be no option to Save or any other dialogs that may be displayed by the application, before it is terminated.
    Last edited by cytop; 12-18-2014 at 06:05 AM.

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: How Do I Close Any Open File

    Quote Originally Posted by cytop View Post
    Kills a process based on it's title...
    Interesting. +1

    (I am looking to close an open file, not kill the whole program however I am open to alternative solutions on this thread)


    Quote Originally Posted by cytop View Post
    Title needs to be an Exact match - there are alternatives to search for a Window based on a partial title, but just took the easiest option (I had the code already).
    An exact match may be a problem. I am primarily interested in solving the close file concept for PDFs. And depending on the system I am using, the default PDF software is Sumatra, FoxIt or Adobe Reader. Now the first one uses the filename as the window title. But the latter twos title is "[FileName] - [ProgramName]".


    Quote Originally Posted by cytop View Post
    This kills a process, not just close the application - there will be no option to Save or any other dialogs that may be displayed by the application, before it is terminated.
    Understood. If I do end up going down the Kill Process route (instead of Close File) then I don't care about saving files before killing. Thanks for pointing that out though.
    Last edited by mc84excel; 12-18-2014 at 06:12 PM.

  4. #4
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: How Do I Close Any Open File

    You can use WMI also for a brute force approach:
    Please Login or Register  to view this content.
    For a document specific approach, you'd want to loop through the open windows, get the caption and then probably send a WM_CLOSE message, or similar. Or if you know the full file name, perhaps simply GetObject.
    Remember what the dormouse said
    Feed your head

  5. #5
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: How Do I Close Any Open File

    Quote Originally Posted by romperstomper View Post
    You can use WMI also for a brute force approach:
    Please Login or Register  to view this content.
    For a document specific approach, you'd want to loop through the open windows, get the caption and then probably send a WM_CLOSE message, or similar. Or if you know the full file name, perhaps simply GetObject.
    Thanks for the suggestions Rory.

    I don't see how the first two will work for me (1. requires the program process name - which I cant be programatically sure of unless I get stuck into registry detection of the process exe. 2. requires the name of the window title - which I cant be sure of - unless I assume that the open file will always contain the filename somewhere in the window title? and then loop through the titles looking for wildcard filename wildcard?)

    I am looking into suggestion 3 now. This sounds like the best solution, I just need to solve the error messages on setting the object from the fullname. (Error 424 on trying to set object on open PDF) (UPDATE: I'm not the only one - http://www.vbaexpress.com/forum/show...ct-not-working )
    Last edited by mc84excel; 12-18-2014 at 06:42 PM.

  6. #6
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: How Do I Close Any Open File

    Why do you need your code to close a PDF it didn't open?

  7. #7
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: How Do I Close Any Open File

    Quote Originally Posted by romperstomper View Post
    Why do you need your code to close a PDF it didn't open?
    Hmmm...Good question.

    I initially thought that if the PDF was already open, I would need to close it and reopen it in order to ensure that the code had full control and window focus before continuing.

    However upon testing this morning, it seems that even if the file is already open, the rest of the code works just fine. In terms of window focus that is. I want to close the PDF once the code has finished - not essential I know.


    UPDATE:
    It may help if you can see what I want to use this close file code for? See attached. (And yes the attached is working for me but it assumes that the PDF software is Foxit. So it will fail on the other systems I work on)
    Attached Files Attached Files
    Last edited by mc84excel; 12-18-2014 at 07:03 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Problem With Open File and Save File With Saveas and then close the File
    By John Vieren in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-27-2013, 12:53 AM
  2. Open 2nd file(CSV) from cell reference, copy columns to main file & close 2nd file
    By Langchop in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-31-2013, 05:09 AM
  3. Check if a Word File is Open and If Open Close it with Excel VBA.
    By e4excel in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-10-2012, 11:14 AM
  4. [SOLVED] VB to open a file, input on a file and close again, with NO macros/vb saved to file
    By unreal_event_horizon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-05-2012, 11:41 AM
  5. How to: Open file, format data, save file, close file and repeat.
    By thexeber in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-11-2010, 12:56 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1