+ Reply to Thread
Results 1 to 20 of 20

VBA to check if user has pdf file open

Hybrid View

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

    Re: VBA to check if user has pdf file open

    Hello Rerock,

    Here is the 64 bit version of the macro.
    ' 64 Fit Windows API Calls.
    Private Declare PtrSafe Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
    
    Private Declare PtrSafe Function GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hwnd As LongPtr, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    
    Private Declare PtrSafe Function GetNextWindow Lib "user32.dll" Alias "GetWindow" (ByVal hwnd As LongPtr, ByVal wFlag As Long) As LongPtr
    
    Private Declare PtrSafe Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
    
    
    Sub ClosePDF(ByVal FileName As String)
    
        Dim Found   As Boolean
        Dim hwnd    As LongPtr
        Dim RefCell As Range
        Dim ret     As LongPtr
        Dim Title   As String
        Dim WndCls  As String
        
        Const GW_HWNDNEXT   As Long = 2
        Const WM_CLOSE      As Long = 16
        
           If FileName = "" Then Exit Sub
           
            hwnd = FindWindow(vbNullString, vbNullString)
    
            Do Until hwnd = 0
                WndCls = String(512, Chr(0))
                ret = GetClassName(hwnd, WndCls, 512)
                    If Left(WndCls, ret) = "AcrobatSDIWindow" Then
                        Title = String(512, Chr(0))
                        ret = GetWindowText(hwnd, Title, Len(Title))
                        If InStr(1, Title, FileName, vbTextCompare) Then
                            ret = SendMessage(hwnd, WM_CLOSE, 0, ByVal 0)
                            Found = True
                            Exit Do
                        End If
                    End If
                hwnd = GetWindow(hwnd, GW_HWNDNEXT)
            Loop
      
            If Not Found Then MsgBox "The Following PDF Document Was Not Found :" & vbCrLf & vbCrLf & FileName
            
    End Sub
    Last edited by Leith Ross; 10-09-2014 at 11:39 AM.
    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!)

+ 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. [SOLVED] Can I disallow the user to open a file if he disable the macro while the file is opening?
    By Robin in forum Excel Programming / VBA / Macros
    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. 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