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
PDF_Caption = "Test.pdf - Adobe Reader"
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
PostMessage hwnd, WM_CLOSE, 0, ByVal 0&
hwnd = FindWindow(vbNullString, PDF_Caption)
#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
@Haluk
I think that the OP is trying to close the application rather than a file
#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