VBA Code to close adobe reader

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I am looking for code to close Adobe Acrobat reader via Excel if open


I would appreciate any assistance to do this
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Re: VBA Code to close adobe readee

The following code will close if Test.pdf is already opened by Adobe Reader.

Rich (BB code):
Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
(ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long

Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassname As String, ByVal lpWindowName As String) As Long

Const WM_CLOSE = &H10

Sub Close_Adobe_Reader_PDF_Document()
    Dim Hwnd As Long
    PDF_Caption = "Test.pdf - Adobe Reader"
    Hwnd = FindWindow(vbNullString, PDF_Caption)
    If Hwnd Then
        PostMessage Hwnd, WM_CLOSE, 0, ByVal 0&
    Else
        MsgBox PDF_Caption & " is not running !"
    End If
End Sub
 
Upvote 0
Re: VBA Code to close adobe readee

Thanks Haluk

I am using Office 2016 64 bit


I amended your declarations to take 64 bit into account


However, I get a run time error "variable not defined"

Rich (BB code):
 PDF_Caption = "Test.pdf - Adobe Reader"




Rich (BB code):
 Option Explicit



#If  VBA7 And Win64 Then
    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, lParam As Any) As LongPtr
#Else 
    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
#End  If
Const WM_CLOSE = &H10

Sub Close_Adobe_Reader_PDF_Document()
    Dim hwnd As Long
    PDF_Caption = "Test.pdf - Adobe Reader"
    hwnd = FindWindow(vbNullString, PDF_Caption)
    If hwnd Then
        PostMessage hwnd, WM_CLOSE, 0, ByVal 0&
    Else
        MsgBox PDF_Caption & " is not running !"
    End If
End Sub


Kindly amend your code
 
Upvote 0
Re: VBA Code to close adobe readee

Hello,

In your code, remove the line Option Explicit and according to your API declarations, use SendMessage instead of PostMessage in line;

Rich (BB code):
PostMessage hwnd, WM_CLOSE, 0, ByVal 0&
 
Upvote 0
Re: VBA Code to close adobe readee

Thanks for the reply

I now get type mismatch and the following code is highlighted


Rich (BB code):
 hwnd = FindWindow(vbNullString, PDF_Caption)


Full code


Rich (BB code):
 #If  VBA7 And Win64 Then
    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, lParam As Any) As LongPtr
#Else 
    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
#End  If
Const WM_CLOSE = &H10

Sub Close_Adobe_Reader_PDF_Document()
    Dim hwnd As Long
    PDF_Caption = "Test.pdf - Adobe Reader"
    hwnd = FindWindow(vbNullString, PDF_Caption)
    If hwnd Then
        SendMessage hwnd, WM_CLOSE, 0, ByVal 0&
    Else
        MsgBox PDF_Caption & " is not running !"
    End If  
  End Sub


Kindly amend code and advise
 
Upvote 0
Re: VBA Code to close adobe readee

I am not sure if this helps but;

Remove the following line:

Code:
Dim hwnd As Long

and add the following line:

Code:
Dim PDF_Caption As String
 
Upvote 0
Re: VBA Code to close adobe readee

Thanks, Macro runs but does not close Adobe when it is open
 
Upvote 0
Re: VBA Code to close adobe readee

Is the name of the PDF file is Test.pdf ?

Check the caption of the PDF window and make sure to write it in the code line below:

Rich (BB code):
PDF_Caption = "Test.pdf - Adobe Reader"
 
Upvote 0
Re: VBA Code to close adobe readee

@Haluk
I think that the OP is trying to close the application rather than a file
 
Upvote 0
Re: VBA Code to close adobe readee

@Haluk
I think that the OP is trying to close the application rather than a file

Yes, you may be correct.

So, by getting the handle of Adobe rather then the opened PDF file; the below code will close Adobe application regardless of the file name.

Rich (BB code):
#If VBA7 And Win64 Then
    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, lParam As Any) As LongPtr
#Else 
    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
#End  If
Const WM_CLOSE = &H10

Sub Close_Adobe_Reader()
    Dim strClassName As String
    strClassName = "AcrobatSDIWindow"
    hwnd = FindWindow(strClassName, vbNullString)
    If hwnd Then
        SendMessage hwnd, WM_CLOSE, 0, ByVal 0&
    Else
        MsgBox "Adobe is not running !"
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,589
Messages
6,120,416
Members
448,960
Latest member
AKSMITH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top