Open a PDF from Excel

Michael Pettinicchi

Board Regular
Joined
Apr 11, 2002
Messages
53
I posted this before but didn't get any answers so I'm trying it again just in case it was missed.

I need to open a PDF file from an Excel workbook.
It seems I need to create an Object like:
Set appWD = CreateObject("Word.Application")
but I don't know what it should be for a PDF.
Thanks,
Michael
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hi,

I find it difficult to use Adobe Reader´s typelibrary so I use following procedure:

<pre>
Sub Open_PDF()
Dim dbRetValue As Double
Dim stAdobeExe As String, stFileName As String

stAdobeExe = "C:ProgramAdobeAcrobat 5.0AcrobatAcrobat.exe"
stFileName = "e:Datahantering.pdf"

dbRetValue = Shell(stAdobeExe & " " & stFileName, vbMaximizedFocus)

End Sub
</pre>

Juan, do you open PDF with Adobe Acrobat TypeLibrary?

Kind regards,
Dennis
 
Upvote 0
Dennis, I dislike PDF a lot too !

And, I have used the method that Ivan explains on his site of "Open any program" or any file, because it uses the associated program of the file.

I have used a method to merge Pdf files, that has worked ok, I don't know if that's the one I posted a while ago, but here it is, "pure" programatic access to Acrobat:<Pre>Public Sub mergePDFFiles(inDirectory As String, outMergeFile As String, fileList() As String)
Dim AcroApp As CAcroApp
Dim PDDoc As CAcroPDDoc
Dim InsertPDDoc As CAcroPDDoc
Dim iNumberOfPagesToInsert As Integer, iLastPage As Integer

'On Error GoTo ErrorHandler

Set AcroApp = CreateObject("AcroExch.App")
Set PDDoc = CreateObject("AcroExch.PDDoc")
Set InsertPDDoc = CreateObject("AcroExch.PDDoc")

'/ Is there a trailing backslash on the inDirectory?
If Right(inDirectory, 1) <> "" Then
inDirectory = inDirectory & ""
End If

'/ Hide the Acrobat window
AcroApp.Hide

'/ Create a Blank PDDoc (The Merge File) - Alterations courtesy of KDA.
If PDDoc.Create = False Then
MsgBox "Creation not successful"
End
End If

'/PDDoc.Save &H1, inDirectory & outMergeFile

For goRound = LBound(fileList()) To UBound(fileList())

' Get the total pages less one for the last page num [zero based]
iLastPage = PDDoc.GetNumPages - 1
'/If (iLastPage = -1) Then iLastPage = 0

If fileList(goRound) = "" Then
Exit For
End If

'/ Using the fileList Array access the first index member
If InsertPDDoc.Open(fileList(goRound)) = False Then
writeError inDirectory & "errpdfMerge.log", "Error Opening The File To Insert - " & fileList(goRound)
'/ MsgBox "Error Opening The File To Insert - " & fileList(goRound), , "MergePDFs - Command Line"
Exit Sub
End If

'/ Get the number of pages to insert from the Current InsertPDDoc object
iNumberOfPagesToInsert = InsertPDDoc.GetNumPages

strOutput = "Go Round Counter for fileList array: " & goRound & vbCrLf & _
"File List Array Item: " & fileList(goRound) & vbCrLf & _
"Insert Document: " & InsertPDDoc.GetFileName & vbCrLf & _
"Last Page Number: " & iLastPage & vbCrLf & _
"Number of Pages to Insert: " & iNumberOfPagesToInsert & vbCrLf & _
"File Name of PDDoc: " & PDDoc.GetFileName & vbCrLf & _
"Directory of In Files: " & inDirectory & vbCrLf & _
"Title from Insert Doc: " & InsertPDDoc.GetInfo("Title") & vbCrLf & _
"Number of Files in Array: " & cntFiles

'/MsgBox strOutput

'/ Insert the pages
If PDDoc.InsertPages(iLastPage, InsertPDDoc, 0, iNumberOfPagesToInsert, True) = False Then
'writeError inDirectory & "errpdfMerge.log", "Error Inserting Pages - " & inDirectory & fileList(goRound)
'/MsgBox "Error Inserting Pages - " & inDirectory & fileList(goRound), , "MergePDFs - Command Line"
Exit Sub
End If

' Close the document without saving
If InsertPDDoc.Close() = False Then
'writeError inDirectory & "errpdfMerge.log", "Error Closing Insert Document - " & fileList(goRound)
'/MsgBox "Error Closing Insert Document - " & fileList(goRound), , "MergePDFs - Command Line"
Exit For
End If

writeLog inDirectory & "pdfMerge.log", inDirectory & fileList(goRound), iNumberOfPagesToInsert
Next
'/ Save the File Optimized
If (PDDoc.Save(&H5, outMergeFile)) = False Then
' writeError inDirectory & "errpdfMerge.log", "Error Saving The Merged Document - " & outMergeFile
MsgBox "Error Saving The Merged Document - " & outMergeFile, , "MergePDFs - Command Line"
Exit Sub
End If

' Close the PDDoc
If PDDoc.Close() = False Then
'writeError inDirectory & "errpdfMerge.log", "Error Closing The Merged Document - " & fileList(goRound)
MsgBox "Error Closing The Merged Document - " & fileList(goRound), , "MergePDFs"
Exit Sub
End If
' Close Acrobat Exchange
AcroApp.Exit

Set AcroApp = Nothing
Set PDDoc = Nothing
Set InsertPDDoc = Nothing

'/MsgBox "Merge Completed", vbOKOnly, "MergePDF's - Command Line Version"
'writeLogFooter inDirectory & "pdfMerge.log"
Exit Sub

ErrorHandler:
MsgBox Err.Description & " dave", , "MergePDFs"
Err.Clear
Exit Sub
End Sub

Public Sub writeLog(logFileName As String, fileBeingInserted As String, numberOfPagesInserted As Integer)

Open logFileName For Append As #3
Print #3, fileBeingInserted & "," & numberOfPagesInserted
'Print #1, "--------------- end of session: " & Now()
Close #3

End Sub</pre>
 
Upvote 0
Hi,

Thanks for the code Juan (and Ivan) :)

The structure of the objects in Adobe reminds me about Lotus Notes (which LotusScript I also dislike...)

Nevertheless, the below procedure will open a PDF-file and a reference must be made to Adobe Acrobat x.0 Type Library:

<PRE>


<FONT color=blue>Sub </FONT>Open_PDF()

<FONT color=blue>Dim </FONT>AcroApp<FONT color=blue> As</FONT> Acrobat.CAcroApp

<FONT color=blue>Dim </FONT>PDDoc<FONT color=blue> As</FONT> Acrobat.CAcroPDDoc

<FONT color=blue>Dim </FONT>avDoc<FONT color=blue> As</FONT> Acrobat.CAcroAVDoc



<FONT color=blue>Set </FONT>AcroApp = CreateObject("AcroExch.App")

<FONT color=blue>Set </FONT>PDDoc = CreateObject("AcroExch.PDDoc")



<FONT color=blue>If </FONT>PDDoc.Open("e:Datahantering.pdf") Then

AcroApp.Show

<FONT color=blue>Set </FONT>avDoc = PDDoc.OpenAVDoc("")

<FONT color=blue>Else</FONT>

MsgBox "Unable to open the PDF-file", vbInformation

<FONT color=blue>End If</FONT>



<FONT color=blue>Set </FONT>avDoc =<FONT color=blue> Nothing</FONT>

<FONT color=blue>Set </FONT>PDDoc =<FONT color=blue> Nothing</FONT>

<FONT color=blue>Set </FONT>AcroApp =<FONT color=blue> Nothing</FONT>

<FONT color=blue>End Sub</FONT>
</PRE>

In order to execute it successfully the total package of Acrobat Reader must be installed (it is not sufficient to have teh client-version only).

Kind regards,
Dennis
 
Upvote 0
Tom Ogilvy, a Microsoft MVP, posted the following and advised it worked for him using Win2K/XL2000.

<pre>
Sub Tester2()
Shell "C:Program FilesAdobeAcrobat 5.0ReaderAcroRd32.exe" & _
" c:toc1p708_2.pdf"
End Sub

</pre>

Mr. Ogilvy advises note the space in front of the file path.

I was unable to get the code to work in Excel 97-SR2. The code opens the Adobe reader but fails to open the file (the file path was treble checked etc).

Regards,


Mike
 
Upvote 0
Ekim,

I must honestly question You contribution in this thread.

What´s the benefit of the answer Your provided compared with my first answer?

...and beside that my second reply is even better :wink:

Kind regards,
Dennis
 
Upvote 0
On 2002-11-28 19:12, Ekim wrote:
Tom Ogilvy, a Microsoft MVP, posted the following and advised it worked for him using Win2K/XL2000.

<pre>
Sub Tester2()
Shell "C:Program FilesAdobeAcrobat 5.0ReaderAcroRd32.exe" & _
" c:toc1p708_2.pdf"
End Sub

</pre>

Mr. Ogilvy advises note the space in front of the file path.

I was unable to get the code to work in Excel 97-SR2. The code opens the Adobe reader but fails to open the file (the file path was treble checked etc).

Regards,


Mike

Mike
Worked for me ??
Shelling out this way is OK, untill you want to do more with the Application...

<pre/>
Sub Tester2()
Dim AppPath As String
Dim strDoc As String

AppPath = "C:Program FilesAdobeAcrobat 5.0ReaderAcroRd32.exe"
strDoc = " C:Program FilesAdobeAcrobat 5.0HelpENUDocBox.pdf"

Shell AppPath & strDoc, vbMaximizedFocus

End Sub
</pre>
 
Upvote 0
Thanks everyone. This is great stuff.

In reply to Juan's question, I wanted to OPEN the PDF file and not control Acrobat.

I used XL-Dennis' solution which works fine - EXCELlent!

I will be looking into Ivan's Open Any File in his web site - great web site.

Thanks again to all,
Michael
 
Upvote 0
Well Dennis,

You question my contribution to this thread? I think that's the prerogative of the OP.

I could not get your solution to work for me. So, I assumed (wrongly) that it did not work period. On the other hand, I had a solution from a Microsoft MVP who confirmed that it worked for him. Therefore, to assist the OP, the solution was thrown into the mix for consideration.

None of the solutions provided in this thread worked for me. It could be something to do with my computer configuration (Win 95/Excel 97-SR2) or that I have Acrobat 4.0. On the other hand, it could be me.

Regards,

Mike
 
Upvote 0

Forum statistics

Threads
1,215,636
Messages
6,125,952
Members
449,276
Latest member
surendra75

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