VBA to open non-office document

B

ben M

I'm attempting, without much luck, to create a menu in word that will open
frequently used documents/templates for my users.

Getting word to create a new document based on a specific template was easy.

The hard part, the part I'm stonewalled against, is writing the code to open
a pdf file using a macro or VBA. Any ideas?
 
J

Jean-Guy Marcil

ben M was telling us:
ben M nous racontait que :
I'm attempting, without much luck, to create a menu in word that will
open frequently used documents/templates for my users.

Getting word to create a new document based on a specific template
was easy.

The hard part, the part I'm stonewalled against, is writing the code
to open a pdf file using a macro or VBA. Any ideas?

An easy way is to use hyperklinks:

ActiveDocument.FollowHyperlink "C:\MyPDF.pdf"

But, depending on the Word version, it may trigger a warning dialog
regarding the untrustworthiness of hyperlink targets.

To avoid that, use the following:

'Place this before the first procedure, at the top of the module
Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

'This is the function that does the work
Public Sub OpenAcrobatFile(strFile As String)
ShellExecute 0, "open", strFile, vbNullString, vbNullString, 9
End Sub

'In a regular sub, use this to call the function to open the PDF file
without any warnings:

OpenAcrobatFile "C:\MyPath\FileName.pdf"

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
T

Tony Jollans

But bear in mind that there is a bug in Acrobat 7 which stops hyperlinks to
PDFs working properly. It is fixed in version 7.0.8 so you may need to
upgrade.
 
B

ben M

Thanks to you and Tony!

I've been racking my brain for days trying to figure this out.
 
P

PumaMan

This helped me as well. Thanks!!!

Jean-Guy Marcil said:
ben M was telling us:
ben M nous racontait que :


An easy way is to use hyperklinks:

ActiveDocument.FollowHyperlink "C:\MyPDF.pdf"

But, depending on the Word version, it may trigger a warning dialog
regarding the untrustworthiness of hyperlink targets.

To avoid that, use the following:

'Place this before the first procedure, at the top of the module
Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

'This is the function that does the work
Public Sub OpenAcrobatFile(strFile As String)
ShellExecute 0, "open", strFile, vbNullString, vbNullString, 9
End Sub

'In a regular sub, use this to call the function to open the PDF file
without any warnings:

OpenAcrobatFile "C:\MyPath\FileName.pdf"

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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