Excel VBA Open pdf (struggling)

dwilson38550m

Board Regular
Joined
Nov 21, 2005
Messages
89
Hi,

I have created a macro code to create a .pdf file (Review and Review History tabs) but I would also like to create a code to open this file up. Unfortunately, I have had little success opening the pdf file up. Could anyone suggest anything, please? If possible I would like to open the .pdf file with the location noted on cell S1 of Sheet1.

Thanks in advance.

VBA Code:
Sub convertpdf()
'
' convertpdf Macro
'

    Dim future As String
    Dim future5 As String
   

Sheets("Review").Activate
Sheets(Array("Review", "Review History")).Select

    ActiveWindow.ActivateNext

    Sheets("Data Sheet").Select
    Range("S1").Select
    future5 = ActiveCell
continue:      ActiveWindow.ActivateNext
    Application.DisplayAlerts = False
    Rem ActiveWorkbook.Close SaveChanges:=FALSE
   
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=future5
   
       
    Application.DisplayAlerts = True

    ActiveWorkbook.Close SaveChanges:=False
    Sheets("data sheet").Select
    Range("A1").Select

'
End Sub


Sub openpdf()
'
' openpdf Macro

'
    Dim strFilename As String

    strFilename = "P:\Co3\04\Contract Review - 427078.pdf"

    Call Shell(strFilename, vbNormalFocus)


'
End Sub
 
Last edited by a moderator:

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Add this to the module:
VBA Code:
Public Sub Open_PDF(PDFfullName As String)
    CreateObject("Shell.Application").Namespace(Left(PDFfullName, InStrRev(PDFfullName, "\"))).Items.Item(Mid(PDFfullName, InStrRev(PDFfullName, "\") + 1)).InvokeVerb "Open"
End Sub
and call it like this:
VBA Code:
    Open_PDF Worksheets("Data Sheet").Range("S1").Value
 
Upvote 0
Add this to the module:
VBA Code:
Public Sub Open_PDF(PDFfullName As String)
    CreateObject("Shell.Application").Namespace(Left(PDFfullName, InStrRev(PDFfullName, "\"))).Items.Item(Mid(PDFfullName, InStrRev(PDFfullName, "\") + 1)).InvokeVerb "Open"
End Sub
and call it like this:
VBA Code:
    Open_PDF Worksheets("Data Sheet").Range("S1").Value
 
Upvote 0
For something like this I usually record a macro whilst printing the active sheet and incorporate the generated code.

The macro recorder generates:
VBA Code:
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
and the PrintOut method can also be applied to a sheet:
VBA Code:
    ActiveSheet.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
 
Upvote 0
Hi John W,

I used the code below to try to print the active pdf file however instead this only prints the Excel file that holds the macro - can you think of anything else I can do to print the active pdf then close this down. The full code I am using is below. Sorry this is driving me a bit crazy but I feel I am close to resolving this. Thanks in advance.

-----------------------------------------------------------------------------------

Public Sub Open_PDF(PDFfullName As String)
CreateObject("Shell.Application").Namespace(Left(PDFfullName, InStrRev(PDFfullName, "\"))).Items.Item(Mid(PDFfullName, InStrRev(PDFfullName, "\") + 1)).InvokeVerb "Open"
End Sub
Sub openpdf()
'
' openpdf Macro

'

Open_PDF Worksheets("Data Sheet").Range("S1").Value



ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False

'
End Sub
------------------------------------------------------------------------------------------------------------
 
Upvote 0
The PrintOut method can only print the workbook or sheets. In the same way that InvokeVerb "Open" opens the PDF, InvokeVerb "Print" prints it. However, all methods to print the PDF leaves the PDF application (Acrobat/Adobe Reader) open, therefore we must close it using Windows API functions.

Put this code in a new module:
VBA Code:
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function SetForegroundWindow Lib "user32.dll" (ByVal hWnd As LongPtr) As Long
    Private Declare PtrSafe Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
    Private Declare PtrSafe Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
    Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
#Else
    Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hWnd As Long) As Long
    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds 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
#End If


Public Sub Print_and_Close_PDF(PDFfullName As String)

    Print_PDF PDFfullName
    Find_and_Close_PDF_Window
    
End Sub


Private Sub Print_PDF(PDFfullName As String)
    CreateObject("Shell.Application").Namespace(Left(PDFfullName, InStrRev(PDFfullName, "\"))).Items.Item(Mid(PDFfullName, InStrRev(PDFfullName, "\") + 1)).InvokeVerb "Print"
End Sub


Private Sub Find_and_Close_PDF_Window(Optional PDFwindowTitle As String)

    #If VBA7 Then
        Dim hWnd As LongPtr
    #Else
        Dim hWnd As Long
    #End If
    Dim timeOutTime As Date
    
    Const WM_CLOSE As Long = &H10

    timeOutTime = DateAdd("s", 3, Now)
    Do
        DoEvents
        hWnd = FindWindow("AcrobatSDIWindow", PDFwindowTitle)
        Sleep 100
    Loop Until hWnd <> 0 Or Now > timeOutTime
    
    If hWnd <> 0 Then
        'Close the PDF window
        Sleep 1000
        SendMessage hWnd, WM_CLOSE, 0&, 0&
    Else
        AppActivate Application.Caption
        MsgBox "Could not find Acrobat window" & IIf(PDFwindowTitle <> "", " with title '" & PDFwindowTitle & "'", "")
    End If

End Sub
To print and close your PDF add this to your macro:
VBA Code:
    Print_and_Close_PDF Worksheets("Data Sheet").Range("S1").Value
 
Upvote 0

Forum statistics

Threads
1,214,894
Messages
6,122,124
Members
449,066
Latest member
Andyg666

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