Cornelis
Cornelis

Reputation: 435

Open PDF file with VBA

I'd like to open a PDF file using an Excel VBA macro.

I have a list of names in the excel file. As soon as the commandbutton "Open PDF" is pressed i would like the macro to open a pdf file from a certain location.

filename corrosponds with activecell.value

thanks in advance

Sub Knop1_Klikken()

Dim a As String
Dim myShell As Object

a = ActiveCell.Value

Set myShell = CreateObject("WScript.Shell")
myShell.Run "Z:\simbeton - Solidworks\bp - betonplaten\bp07 - simvlak ZH Sport\PDF\" & "a" & ".pdf"

End Sub

The error: (my MS is in dutch):

Fout -2147024894 (80070002) tijden uitvoering: Methode Run van object IWshSHell3 is mislukt

Translated: Error -2147024894 (80070002) during execution: Methode Run of object IWshSHell3 has failed.

Upvotes: 1

Views: 14353

Answers (2)

Dave
Dave

Reputation: 11

Have you find out how it's done yet? If not, here 's the solution:

myShell.Run chr(34) & "C:\" & a & ".pdf" & chr(34)

chr(34) is a "

The difference is : your command sends C:\JouBetonInfo.pdf as an argument whereas my command sends "C:\JouBetonInfo.pdf" as an argument. Note the quotes that I send along. It works for me (Excel 2007).

Upvotes: 1

Koby Douek
Koby Douek

Reputation: 16693

You can simply use WScript.Shell like this:

a = ActiveCell.Value
Dim myShell As Object
Set myShell =  CreateObject("WScript.Shell")
myShell.Run "C:\" & a & ".pdf"

Upvotes: 1

Related Questions