Close open PDF file after opening it with FollowHyperlink
I am opening a pdf using the method FollowHyperlink
shown here:
Sub Sample()
ActiveWorkbook.FollowHyperlink "C:\MyFile.pdf"
End Sub
which was found in this thread .
My question is how to close the PDF?
source to share
Here are the two options I'm using.
Option1: I use this option to kill all open internet browsers when they are not visible (otherwise I messed up). There might be a way to lay out the file this way, but I'm not entirely sure if this is possible without an API call as @Jeeped mentioned. I'll list the second API call.
To find out what type of Adobe you are using. Open Windows Task Manager> Processes and look for the .exe with the description for Adobe Reader.
Sub Kill_All_PDFs()
'***ErrorHandler***
On Error Resume Next
'***Define Variables***
Dim objectWMI As Object
Dim objectProcess As Object
Dim objectProcesses As Object
'***Set Objects***
Set objectWMI = GetObject("winmgmts://.")
Set objectProcesses = objectWMI.ExecQuery( _
"SELECT * FROM Win32_Process WHERE Name = 'AcroRd32.exe'") '< Change if you need be
'***Terminate all Open PDFs***
For Each objectProcess In objectProcesses
Call objectProcess.Terminate
Next
'***Clean Up***
Set objectProcesses = Nothing
Set objectWMI = Nothing
End Sub
Option2 API call method:
Here you can find your PDF file by name. I modified the code to find Adobe, but the source is listed below if you'd like to continue reading about how it works. Just add the title as it appears at the top of your PDF file.
Source: http://support.microsoft.com/kb/168204
Private Declare Function FindWindow _
Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) _
As Long
Private Declare Function SendMessage _
Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Long) _
As Long
Private Sub Close_AdobeReader()
Dim lpClassName As String
Dim lpCaption As String
Dim Handle As Long
Const NILL = 0&
Const WM_SYSCOMMAND = &H112
Const SC_CLOSE = &HF060&
lpClassName = "AcrobatSDIWindow"
lpCaption = "e.g.name - Adobe Reader" '< add Title Here
'* Determine the handle to the Calculator window.
Handle = FindWindow(lpClassName$, lpCaption$)
'* Post a message to Calc to end its existence.
Handle = SendMessage(Handle, WM_SYSCOMMAND, SC_CLOSE, NILL)
End Sub
Hope this helps you on your way!
source to share