Results 1 to 3 of 3
  1. #1
    Thomasso is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Nov 2016
    Location
    Czech Republic
    Posts
    277

    How to close an opened PDF using VBA?

    Hello,



    I am trying to figure out how to close a specific PDF file using VBA in MS Access.

    The full path of the file is as follows:
    Code:
    \\server1\TEST$\XXX.pdf
    I am already able to check if the file is currently opened. If it is, I want to close it.

    Any ideas? Thank you.

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    ChatGPT suggests
    Code:
    Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _
        ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
    
    
    Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" ( _
        ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, _
        ByVal lParam As LongPtr) As LongPtr
    
    
    Private Const WM_CLOSE = &H10
    
    
    Sub ClosePDFWindow()
        Dim hWnd As LongPtr
        Dim windowTitle As String
        
        windowTitle = "YourFileName.pdf - Adobe Acrobat Reader" ' Adjust as needed
        hWnd = FindWindow(vbNullString, windowTitle)
        
        If hWnd <> 0 Then
            Call SendMessage(hWnd, WM_CLOSE, 0, 0)
        Else
            MsgBox "Window not found."
        End If
    End Sub
    That is 64bit code, so you will need to modify for 32bit.
    https://learn.microsoft.com/en-us/of...tions-overview

    Would work for any window as well, so could be an Excel file, Browser window, whatever, as long as you know it's name.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Thomasso is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Nov 2016
    Location
    Czech Republic
    Posts
    277
    Quote Originally Posted by Welshgasman View Post
    ChatGPT suggests
    Code:
    Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _
        ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
    
    
    Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" ( _
        ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, _
        ByVal lParam As LongPtr) As LongPtr
    
    
    Private Const WM_CLOSE = &H10
    
    
    Sub ClosePDFWindow()
        Dim hWnd As LongPtr
        Dim windowTitle As String
        
        windowTitle = "YourFileName.pdf - Adobe Acrobat Reader" ' Adjust as needed
        hWnd = FindWindow(vbNullString, windowTitle)
        
        If hWnd <> 0 Then
            Call SendMessage(hWnd, WM_CLOSE, 0, 0)
        Else
            MsgBox "Window not found."
        End If
    End Sub
    That is 64bit code, so you will need to modify for 32bit.
    https://learn.microsoft.com/en-us/of...tions-overview

    Would work for any window as well, so could be an Excel file, Browser window, whatever, as long as you know it's name.
    Thank you, works!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 07-28-2020, 05:03 PM
  2. Replies: 2
    Last Post: 05-01-2020, 01:14 PM
  3. Replies: 5
    Last Post: 03-27-2017, 07:48 PM
  4. Close and Save Excel file already opened in Access
    By DB2010MN26 in forum Programming
    Replies: 7
    Last Post: 01-19-2012, 06:50 PM
  5. Can not close pop up form after second pop was opened
    By snoopy2003 in forum Programming
    Replies: 2
    Last Post: 03-09-2011, 02:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums