Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    2 Star Lounger
    Join Date
    May 2002
    Location
    Atlanta, Georgia, USA
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Launch a PDF from Word (Office 2000)

    Is there a simple VBA command to launch a PDF file?

    I tried using:

    Shell pdfname.pdf

    But that doesn't seem to work. Do I have to know where Acrobat resides (c:acrobat....)?

    Thanks,
    Bob
    Writing Consultant / Word Template Designer
    Words Connect

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

    Re: Launch a PDF from Word (Office 2000)

    Try

    ActiveDocument.FollowHyperlink "Filename.pdf"

    You don't have to know where the Acrobat reader is installed for this. Windows knows how to open a file with extension .pdf.

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Launch a PDF from Word (Office 2000)

    The VBA Shell is not very smart. If you want a Shell that emulates the Start>Run... dialog, you can use the Windows Script Host's wshShell object. That's significantly more work and overhead than the method Hans suggested, but you can keep it in mind for future reference for cases where you don't have the FollowHyperlink method available.

  4. #4
    2 Star Lounger
    Join Date
    May 2002
    Location
    Atlanta, Georgia, USA
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Launch a PDF from Word (Office 2000)

    Hi Hans -

    That worked, but not as elegantly as I hoped. The PDF opens fine, but the title bar says "Embedded File" instead of the file name. Apparently it is opened more in web browser mode (makes sense given the command), rather than a "standalone" instance of Acrobat. It seems to open a copy of the PDF and not the real file.

    In my environment, the users will need to edit the PDF (change file properties, edit bookmarks, and such). Any changes like that in this embedded file mode are lost when you close Acrobat. I didn't even get a prompt to save changes! I'll either have to find a way to launch Acrobat itself or just make them do it by hand.

    Thanks!
    Bob
    Writing Consultant / Word Template Designer
    Words Connect

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

    Re: Launch a PDF from Word (Office 2000)

    I'm sorry, I don't have the Acrobat Writer, or Distiller, or whatever it is you use, only the Reader. So I can't help you with this.

  6. #6
    2 Star Lounger
    Join Date
    May 2002
    Location
    Atlanta, Georgia, USA
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Launch a PDF from Word (Office 2000)

    Jefferson -

    I really wanted to take advantage of the association for the pdf file type, so the FollowHyperlink command was close to what I wanted. (See my earlier post for the problems I had with that method.)

    I'm not familiar with Windows Script Host--is that something available through VBA?

    Thanks,
    Bob
    Writing Consultant / Word Template Designer
    Words Connect

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

    Re: Launch a PDF from Word (Office 2000)

    If you would like to use FollowHyperlink, you would have to change the default action for PDF's. But that would only work on your own PC, you can't expect other users to do this just for your application. And you would need to know the path to the Acrobat application you are using. In that case, you might use Shell as well.

  8. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Launch a PDF from Word (Office 2000)

    It can be accessed through VBA if installed. Here are two ways, the first uses "late binding" and you don't have to set a Reference in your project, the second uses early binding which is preferred for performance, but means you have to remember to set the reference whenever you re-use the code.

    Sub ShowPDF1()
    Dim shl As Object
    ' Next line requires that the Windows Script Host be installed
    Set shl = CreateObject("WScript.Shell")
    shl.Run """C:testingProduct Sheet for resellers.pdf"""
    Set shl = Nothing
    End Sub

    Sub ShowPDF2()
    ' Requires a reference to the Windows Script Host Object Library
    Dim shl As IWshRuntimeLibrary.WshShell
    ' Next line requires that the Windows Script Host be installed
    Set shl = New WshShell
    shl.Run """C:testingProduct Sheet for resellers.pdf"""
    Set shl = Nothing
    End Sub

  9. #9
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Launch a PDF from Word (Office 2000)

    I'd recommend using the Windows API ShellExecute function, which is a lot more useful than the simplistic VBA Shell function. Sample code:

    Option Explicit

    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
    Private Const SW_SHOWMINIMIZED = 2
    Private Const SW_SHOWMAXIMIZED = 3
    Private Const SW_SHOW = 5
    Private Const SW_MINIMIZE = 6
    Private Const SW_SHOWMINNOACTIVE = 7
    Private Const SW_SHOWNA = 8
    Private Const SW_RESTORE = 9
    Private Const SW_SHOWDEFAULT = 10

    Sub TestOpenPDFFile(ByRef strPath As String, ByRef strFile As String)

    ' Windows API ShellExecute function - args:
    ' hwnd (Long) - handle to window (form or app hwnd)
    ' lp = "Long Pointer" (Windows uses pointers to strings)
    ' lpOperation (String) - "Open" or "Print" (use vbNullString for default Open)
    ' lpFile (String) - name of program or document file (strFile)
    ' lpParameters (String) - if file is executable, parameters, otherwise vbNullString
    ' lpDirectory (String) - full path of default directory (strPath)
    ' nShowCmd (Long) - a ShowWindow constant specifying how to display (maximize, etc)

    ShellExecute Application.hWndAccessApp, "Open", strPath & "" & strFile, vbNullString, strPath, SW_SHOWMAXIMIZED

    End Sub

    Sample use:

    TestOpenPDFFile "Cocuments and SettingsMARK DMy Documents", "Test Doc.pdf"

    Executing this instruction from the Immediate window launched Adobe Acrobat and opened the specified .PDF file in a maximized window. This example assumes the default directory will be same as path to file. For first argument, you would specify either a form or application window handle (hwnd). This is necessary because some applications need to display a msgbox before creating its own main window. How you get this value depends on where you're running code from. In this example I used Access application hwnd. With Acrobat, you can probably get away with specifying zero for this parameter:

    ShellExecute &O0, "Open", strPath & "" & strFile, vbNullString, strPath, SW_SHOWMAXIMIZED

    Note that ShellExecute depends on Windows Registry file associations to "know" which app to launch for specified file. If your users all have Adobe Acrobat installed (which I assume is case) then it is probably safe to assume that .PDF extension is associated with Acrobat; nevertheless actual use of function should include error handling, etc. Also note, you can specify "Print" vice "Open" for the lpOperation parameter, which can be useful if you need to print a file associated with a particular application (Word doc, XL file, etc).

    HTH

  10. #10
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Launch a PDF from Word (Office 2000)

    PS: Realized this is running from Word - If you know the Windows class name for an application, you can use the FindWindow API function to get the hwnd for the main application window. Modified example:

    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal strClassName As String, ByVal lpWindowName As Any) As Long

    Sub TestOpenPDFFile(ByRef strPath As String, ByRef strFile As String)

    Dim hwnd As Long

    ' OpusApp = Windows class name for Word:
    hwnd = FindWindow("OpusApp", vbNullString)

    ShellExecute hwnd, "Open", strPath & "" & strFile, vbNullString, strPath, SW_SHOWMAXIMIZED

    End Sub

    The Windows class name for Word is "OpusApp" - don't ask me why, because I don't know - these class names are somewhat arbitrary, something the development team dreams up. This may be related to the oddly-named "WholeStory" method that applies to a Word Range or Selection object.... (see attd pic, Word's synonyms for "Opus")...

    HTH

  11. #11
    2 Star Lounger
    Join Date
    May 2002
    Location
    Atlanta, Georgia, USA
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Launch a PDF from Word (Office 2000)

    That works!

    I'm pulling the name for the PDF from a custom property, so it took me a minute to figure out how to specify PDF file name as a variable. I always have a hard time with the proper syntax for strings that involve quotation marks. Here's what I ended up with:

    strFileName = """" & ActiveDocument.Path & "" & ActiveDocument.CustomDocumentProperties("Document Title") & ".pdf" & """"
    shl.Run strFileName

    Thanks for your help!
    Bob
    Writing Consultant / Word Template Designer
    Words Connect

  12. #12
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Launch a PDF from Word (Office 2000)

    > The Windows class name for Word is "OpusApp" - don't ask me why, because I don't know

    Opus was, I believe, the development code name for Word for Windows. The inspiration for the name is strongly suggested by your signature. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  13. #13
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Launch a PDF from Word (Office 2000)

    That didn't occur to me till later (UserPic notwithstanding), but I think you're right.... and apparently, after an extended absence, the original "Opus" is returning:

    Opus Returns

  14. #14
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Launch a PDF from Word (Office 2000)

    You can download the Acrobat SDK from Adobe's web site.
    It will tell you how to automate Acrobat from VB/VBA.

  15. #15
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Launch a PDF from Word (Office 2000)

    Don't use WSH for apps you are going to distribute.
    WSH tends to ge tauntie virus software upset.

Page 1 of 2 12 LastLast

Posting Permissions

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