Results 1 to 6 of 6
  1. #1
    2 Star Lounger
    Join Date
    Mar 2005
    Location
    Kent, England
    Posts
    138
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Opening PDF Document via a Word Toolbar Button (Office 2003)

    I have a toolbar in Word and wish to open a PDF file when a specific button is clicked. I was under the impression it would be fairly simple eg Shell ("C:Program FilesGuidelinesGuidelines.pdf"), but this does not appear to work. Is this more complex than I originally thought?

    I have looked at a number of queries logged through Woody's on this but cannot seem to find the answer and am unsure whether additonal code is necessary to open the file.

    Thanks
    Jack

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Opening PDF Document via a Word Toolbar Button (Office 2003)

    Shell starts executables, not documents, so you'd have to specify the application used to view PDF files. This is not very attractive since there are different viewers, different installation paths etc. Better to use the FollowHyperlink method or the ShellExecute API function. For example:

    Sub ShowPDF()
    ActiveDocument.FollowHyperlink "C:Program FilesGuidelinesGuidelines.pdf"
    End Sub

  3. #3
    2 Star Lounger
    Join Date
    Mar 2005
    Location
    Kent, England
    Posts
    138
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Opening PDF Document via a Word Toolbar Button (Office 2003)

    Thanks Hans. That makes sense as I was able to run the Calculator ("calc.exe") but not open the file.

    Regards
    Jack

  4. #4
    2 Star Lounger
    Join Date
    Mar 2005
    Location
    Kent, England
    Posts
    138
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Opening PDF Document via a Word Toolbar Button (Office 2003)

    Hans

    Apologies, I should have mentioned, I do not have an Active Document open. Can this be run if no document is open?

    Cheers
    Jack

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Opening PDF Document via a Word Toolbar Button (Office 2003)

    No, FollowHyperlink is a method of the Document object, so it will work only if a document is open. Try using ShellExecute instead:

    Put the following declarations at the beginning of a module:
    <code>
    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

    Private Const SW_SHOWNORMAL = 1
    </code>
    and change the macro attached to the toolbar button to
    <code>
    Public Sub ShowPDF()
    Dim lngResult As Long
    lngResult = ShellExecute(0&, "Open", "C:Program FilesGuidelinesGuidelines.pdf", _
    0&, 0&, SW_SHOWNORMAL)
    If lngResult <= 32 Then
    MsgBox "Something went wrong.", vbExclamation
    End If
    End Sub
    </code>
    This should work whether a document is open or not.

  6. #6
    2 Star Lounger
    Join Date
    Mar 2005
    Location
    Kent, England
    Posts
    138
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Opening PDF Document via a Word Toolbar Button (Office 2003)

    Thank you as always Hans.
    Jack

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •