3

I am opening a pdf file using the FollowHyperlink method, shown here:

Sub Sample()
    ActiveWorkbook.FollowHyperlink "C:\MyFile.pdf"
End Sub

which was found at this thread.

My question is, how do I close the pdf?

Community
  • 1
  • 1
brietsparks
  • 4,776
  • 8
  • 35
  • 69
  • 1
    You would have to import some of the WinAPI's User32 functions and enumerate the top level windows looking for a window title that matched a string constructed using the name of the PDF. Seems easier just to click the × in the top-right corner of the PDF reader. –  Jan 11 '15 at 02:05
  • Sounds complicated... I just used send key alt f4 – brietsparks Jan 11 '15 at 02:16
  • That's probably a bad idea @bsapaka. Send Keys is unreliable at best. You could end up unintentionally closing a different program. – RubberDuck Jan 13 '15 at 21:43

1 Answers1

1

Here are two options i use.

Option1: This option I use it to kill all open internet browsers when they are not visible(aka I messed up). There could be a method to single the file out this way but i am not entirely sure it is possible without an API call as @Jeeped mentioned. I will list the API Call second.

To find out which Adobe type you are running. Open Windows Task Manager > Processes and find the .exe with the description 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 will be able to find your PDF file by title. I modified the code to find Adobe but the source is listed below if you would like further reading on 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!

Ricky
  • 98
  • 4