Opening a pdf file with Adobe Reader

O

Otto Moehrbach

Excel XP & Win XP
I'm trying to get the code for opening a pdf file with my Adobe Reader. I
can easily open the file by just double-clicking the file name, but not by
code, so the reader is operating correctly.
What is the proper code syntax to open, say, "TheFile.pdf", in the path
"ThePath"? Thanks for your time. Otto
 
T

Tom Ogilvy

Private Sub CommandButton1_Click()
Shell "Start.exe ""D:\My Documents\sm569_Jan2001\tute6.pdf"""

End Sub
 
O

Otto Moehrbach

Tom
I think I am misreading what you wrote. Here is what I have written
from what you said. I get an error that the file is not found.
Sub OttoMacroC1()
OpenExcelFilepdf ("C1")
End Sub

Sub OpenExcelFilepdf(WhichFile As String)
ThePath = "C:\LettersForms\Ops Manuals\"
TheFile = WhichFile & " Ops Manual.pdf"
Shell "Start.exe" & ThePath & TheFile
End Sub

There is a file named "C1 Ops Manual.pdf" in the cited path.
Thanks for your help with this. Otto
 
T

Tom Ogilvy

Yes, it appears you did misread it:

Sub OpenExcelFilepdf(WhichFile As String)
ThePath = "C:\LettersForms\Ops Manuals\"
TheFile = WhichFile & " Ops Manual.pdf"
Shell "Start.exe """ & ThePath & TheFile & """"
End Sub

Note the space after Start.exe and the additional quotes.

Demo'd from the immediate window:

ThePath = "C:\LettersForms\Ops Manuals\"
Whichfile = "C1"
TheFile = WhichFile & " Ops Manual.pdf"
? "Start.exe """ & ThePath & TheFile & """"
Start.exe "C:\LettersForms\Ops Manuals\C1 Ops Manual.pdf"

shows you want the argument passed to Shell looks like.

If you still have problems, you might need to put single quotes in so it
ends up being

Start.exe "'C:\LettersForms\Ops Manuals\C1 Ops Manual.pdf'"

because of the spaces in the names. However, the example I posted has
spaces in the path and it worked, so I think yours will work without single
quotes as well.

--
Regards,
Tom Ogilvy


--
Regards,
Tom Ogilvy
 
C

Chip Pearson

Shell "Start.exe ""D:\My Documents\sm569_Jan2001\tute6.pdf"""

Start.exe isn't available on all platforms -- at least that has been my
experience on Windows XP Pro and Vista. For example, I don't have it here
on my laptop with Windows Vista Ultimate (Shell errors with "53 - File Not
Found").

Instead, you can use FindExecutable, which will work on any platform, to
find the exe file associated with the file and Shell to that exe passing the
file name to the exe. Note that the file passed in to FindExecutable must
exist. If it doesn't FindExecutable will fail.

A result >= 32 from FindExecutable indicates success. Change the value of
PDFFileName to the fully-qualified name of your PDF file.

Public Declare Function FindExecutable Lib "shell32.dll" _
Alias "FindExecutableA" ( _
ByVal lpFile As String, _
ByVal lpDirectory As String, _
ByVal lpResult As String) As Long

Const C_MIN_FINDEXE_SUCCESS_VALUE = 32
Const MAX_PATH = 260

Sub ShellToExe()
''''''''''''''''''''''''''''''''''''
' Shells to the exe associated with
' PDFFileName and opens PDFFileName.
''''''''''''''''''''''''''''''''''''
Dim ExeName As String
Dim Pos As Integer
Dim PDFFileName As String
Dim Res As Long

' allocate space in ExeName variable.
ExeName = String$(MAX_PATH, vbNullChar)

'>>>>> CHANGE FILE NAME
PDFFileName = "C:\Whatever\FileName.pdf" '<<< File MUST exist

If Dir(PDFFileName, vbNormal) = vbNullString Then
MsgBox "File: " & PDFFileName & " does not exist." & vbCrLf & _
"'FindExecutable' requires an existing file name."
Exit Sub
End If

' Get the exe name associated with "pdf".
Res = FindExecutable(PDFFileName, vbNullString, ExeName)

If Res >= C_MIN_FINDEXE_SUCCESS_VALUE Then
' trim off trailing vbNullChar characters
Pos = InStr(1, ExeName, vbNullChar, vbBinaryCompare)
If Pos Then
ExeName = Left(ExeName, Pos - 1)
End If
Shell Chr(34) & ExeName & Chr(34) & Chr(32) & _
Chr(34) & PDFFileName & Chr(34),vbMaximizedFocus
Else
MsgBox "Error From FindExecutable: " & CStr(Res)
End If
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email on the web site)
 
O

Otto Moehrbach

Tom
Thanks for your help but it's not working. I ran the following macro to
make sure my path and file name are correct. I get "True":
Sub TestFileExists()
Dim bFileExists As Boolean
Dim rsFullPath As String
rsFullPath = "C:\LettersForms/Ops Manuals\C1 Ops Manual.pdf"
bFileExists = Len(Dir$(rsFullPath))
MsgBox bFileExists
End Sub

I am running the following and I get a "File not found" error. Your help is
greatly appreciated. Otto
Sub OttoMacroC1()
OpenExcelFilepdf ("C1")
End Sub
Sub OpenExcelFilepdf(WhichFile As String)
ThePath = "C:\LettersForms\Ops Manuals\"
TheFile = WhichFile & " Ops Manual.pdf"
Shell "Start.exe """ & ThePath & TheFile & """"
End Sub
 
T

Tom Ogilvy

This also worked, but assumes Acrobat reader is installed.

Sub efg()
Set myShell = CreateObject("WScript.Shell")
Filename = "C:\Army_Ex_300_Gde_11-03.pdf"
myShell.Run ("AcroRd32.exe " & Filename)
End Sub
 
O

Otto Moehrbach

Chip
This code is way over my head and it works fine. Thank you very much
for your time. Otto
 
O

Otto Moehrbach

Thanks for your help Tom. Otto
Tom Ogilvy said:
This also worked, but assumes Acrobat reader is installed.

Sub efg()
Set myShell = CreateObject("WScript.Shell")
Filename = "C:\Army_Ex_300_Gde_11-03.pdf"
myShell.Run ("AcroRd32.exe " & Filename)
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top