+ Reply to Thread
Results 1 to 20 of 20

VBA to check if user has pdf file open

  1. #1
    Forum Contributor
    Join Date
    07-22-2009
    Location
    Minneapolis, MN
    MS-Off Ver
    2016
    Posts
    220

    VBA to check if user has pdf file open

    Hi,
    The following code works great as long as you have entered a manual file path.

    Please Login or Register  to view this content.
    How can this be amended to work with "ThisWorkbook.Path", "ActiveWorkbook.Path", "Range("YOUR_NAMED_RANGE").Value", or some other type of variable?

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA to check if user has pdf file open

    Hello Rerock,

    This macro will search all open PDF documents. If the given document is found then it is closed.

    The document name matching is not case sensitive. You may use either the full document name or part of the name to match an open document.

    Copy this code into a VBA Module in your workbook.

    Macor Code to Close an Opened PDF File
    Please Login or Register  to view this content.
    Example of Using the Macro
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    07-22-2009
    Location
    Minneapolis, MN
    MS-Off Ver
    2016
    Posts
    220

    Re: VBA to check if user has pdf file open

    Hi,
    Wow; that's incredible!

    How do I modify it for 64 Windows?

    All of the Private declarations are red in text...

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA to check if user has pdf file open

    Hello Rerock,

    Here is the 64 bit version of the macro.
    Please Login or Register  to view this content.
    Last edited by Leith Ross; 10-09-2014 at 11:39 AM.

  5. #5
    Forum Contributor
    Join Date
    07-22-2009
    Location
    Minneapolis, MN
    MS-Off Ver
    2016
    Posts
    220

    Re: VBA to check if user has pdf file open

    Hi,
    Thank you! I pasted the code in to a new Module, and tried to assign the macro to a button. For some reason, the macro doesn't appear in the list as an option to select. So, I tried to manually step through the macro (F8), and Windows just makes a dinging noise and does nothing; no debug error, nothing.

    Any ideas?

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA to check if user has pdf file open

    HEllo Rerock,

    VBA will ding if Excel is in Edit Mode. Check that the cursor is not in the Formula Bar.

  7. #7
    Forum Contributor
    Join Date
    07-22-2009
    Location
    Minneapolis, MN
    MS-Off Ver
    2016
    Posts
    220

    Re: VBA to check if user has pdf file open

    Hi, Attached is an image of what I'm seeing. When I try to assign the macro to a button, I'm not seeing the macro available. When I click on a cell (B25 in the screenshot) and try to manually step through the macro, Excel just dings.
    Untitled.jpg

    Perhaps there is an Excel and/ or VBA setting I need to adjust in order for it to work?

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA to check if user has pdf file open

    Hello Rerock,

    Any Sub or Function that takes one or arguments will not show up the available Macros List. But, it is there. Just use the name in your code.

    There seems to have been a problem with the picture uploading. It will not display due to an error. Try uploading it again.

    You will need to View Code for the button and add a call to the macro manually.

    For example:
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    07-22-2009
    Location
    Minneapolis, MN
    MS-Off Ver
    2016
    Posts
    220

    Re: VBA to check if user has pdf file open

    Hi,
    The image upload is now irrelevant due to your last post... Not sure why it didn't work; it was just a jpg screenshot.

    At any rate, I added the 'buttoon1_click' macro, and that seems to work just fine. However, I got a "Compile Error: Type Mismatch" error on this line of the code. "ret" was highlighted. Any idea what may be causing that error?

    Please Login or Register  to view this content.
    Thanks for all of your help; I really appreciate it!

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA to check if user has pdf file open

    Hello Rerock,

    Change the Dim statement for ret to this...
    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    07-22-2009
    Location
    Minneapolis, MN
    MS-Off Ver
    2016
    Posts
    220

    Re: VBA to check if user has pdf file open

    HI,
    It looks like that solved that issue, now there is a new one.

    Please Login or Register  to view this content.
    GetWindowText is highlighted, and I'm getting "Compile Error: Sub or Function not defined".

  12. #12
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA to check if user has pdf file open

    Hello Rerock,

    Okay, I need two ret variables. One for Long data types and a second for LongPtr data types.

    Recised Macro Code
    Please Login or Register  to view this content.
    EDIT: The wrong API call was posted GetWindowNext should be GetWindowText. The change has been made.
    Last edited by Leith Ross; 10-09-2014 at 03:22 PM.

  13. #13
    Forum Contributor
    Join Date
    07-22-2009
    Location
    Minneapolis, MN
    MS-Off Ver
    2016
    Posts
    220

    Re: VBA to check if user has pdf file open

    I really wish I knew how I could help you out with this; I feel bad.

    Please Login or Register  to view this content.
    "Compile Error: Type Mismatch" for FindWindow.

  14. #14
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA to check if user has pdf file open

    Hello Rerock,

    Did you see the correction I made to Post #12?

  15. #15
    Forum Contributor
    Join Date
    07-22-2009
    Location
    Minneapolis, MN
    MS-Off Ver
    2016
    Posts
    220

    Re: VBA to check if user has pdf file open

    Hi,
    Yes, it looks like you edited the code at 2:22, and I copied the code a couple of minutes before I posted at 2:37. I presume that I copied/ tested the corrected code?

  16. #16
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA to check if user has pdf file open

    Hello Rerock,

    The code you copied should work. I don't know why you would get "Type Mismatch" for FindWindow. The return value is a Long Pointer and hWnd is dimensioned as a LongPtr data type.

  17. #17
    Forum Contributor
    Join Date
    07-22-2009
    Location
    Minneapolis, MN
    MS-Off Ver
    2016
    Posts
    220

    Re: VBA to check if user has pdf file open

    Yes, I am running 64 bit Office 2010 on 64 bit Win 7 Enterprise.

    I have an XLSM inside of a folder on our network path. In the folder with the XLSM is a PDF named "Signed Agreement".

    I wish I could help troubleshoot it, but I have no idea what this means:
    Quote Originally Posted by Leith Ross View Post
    The return value is a Long Pointer and hWnd is dimensioned as a LongPtr data type.
    Last edited by Rerock; 10-09-2014 at 04:53 PM. Reason: Grammar

  18. #18
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: VBA to check if user has pdf file open

    Leith's approach lets you have good control.

    For what you posted initially, just poking a value into a cell and getting it worked fine. I put one more routine in it that I use for text files sometimes.

    What else did you need?

    Please Login or Register  to view this content.

  19. #19
    Forum Contributor
    Join Date
    07-22-2009
    Location
    Minneapolis, MN
    MS-Off Ver
    2016
    Posts
    220

    Re: VBA to check if user has pdf file open

    Hi,
    Thanks for the reply, I appreciate you taking the time!

    I tried the code you posted, and the msgbox always says "File is not open" regardless if the file is actually open or not.




    I'm simply trying to get the macro to check and see if - ThisWorkbook.Path & "\Signed Agreement.pdf" - is currently open or not.

    If it is open, it'd be cool if it could close it (no need to save), but a msgbox stating it's open works too.
    If it's not open, then do nothing.

    The code in my first post is what I found googling, and it works fine as long as the file path is embedded in the code; which won't work for this purpose.

  20. #20
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: VBA to check if user has pdf file open

    It is left to the user to change the value of strPDFPath if you are not going to use it the way I built it based on your first post. IF you had checked the value of A1 or observed the way it was built in the code, you would have seen that. Obviously, if want to check that file, you can do it like this.

    Please Login or Register  to view this content.

+ 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. 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
  2. Replies: 12
    Last Post: 06-07-2011, 04:17 AM
  3. Check if file is open and open if closed
    By Jockster in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-22-2010, 08:18 AM
  4. Check to see if file is open, and if it is open, activate it
    By karen000 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-15-2009, 03:59 AM
  5. [SOLVED] vba to check presence of file and open/not open
    By fLiPMoD£ in forum Excel General
    Replies: 2
    Last Post: 04-28-2005, 04:06 PM

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