Results 1 to 10 of 10
  1. #1
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    1,560
    Thanks
    37
    Thanked 1 Time in 1 Post

    Launch .pdf file from Word (2000/9.0.3821 SR1)

    I've read several messages about how to "convert" a PDF file into something MS-Word can use. I tried doing the screen shot thing with very unsatisfactory results. The resulting printed page looked as though I'd photographed it through a lens covered with petroleum jelly--blurry to the point of being illegible. I know I can copy the text with the selection tool in Acrobat Reader, but I'd like to retain the look of the original.

    Here's what I'm dealing with: In our learning labs, we have "handouts" to accompany the courses we offer. One such handout includes a one-page Word document and a four-page PDF document. Of course, they're separate files, and sometimes we print the Word document and forget to print the PDF. Isn't there some way to use VBA to help out here? Picture this: You start Word, then click on File > Open (or CTRL+O), find and open the Word document, and right away Acrobat Reader opens with the rest of the package ready to print. Any way to automate this process?

    Here's another thought: I've got all our courses cataloged in an Access database. The Courses table includes a hyperlink field for the course numbers, and I've dreamed of using that field with an Access form to handle the printing chores. I know I'm getting into territory that belongs to another forum, but I'd like opinions as to whether it might be more feasible to use this MS-Access approach to achieve what I want here. Ideas, anybody...?

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Launch .pdf file from Word (2000/9.0.3821 SR1)

    >>Isn't there some way to use VBA to help out here?

    Yes.

    This Windows API call is what you are looking for to do the printing of PDFs (or any other file type
    with an assocation set up in Windows):

    Public 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

    Call it with "Print" as the operation and set lpFile to your pdf.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  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 .pdf file from Word (2000/9.0.3821 SR1)

    > Picture this: You start Word, then click on File > Open (or CTRL+O), find and open the Word document,
    > and right away Acrobat Reader opens with the rest of the package ready to print. Any way to automate this process?

    You can, as you know, "replace" Word commands at any level (global, attached template, document) by creating a macro of the same name. Thus, if you create a macro called

    Sub FileOpen()
    ...
    End Sub

    it will run in place of the built-in FileOpen command. If you store the path to the Acrobat file in, say, a Custom Document Property, you could have your FileOpen look for that property and, if found, use the Shell method to open that document in Acrobat.

    Printing is a bit trickier. There have been solutions posted on this board or the VBA board on how to print a PDF file from a command line, but if the file is already open, those method might generate a file-in-use error. I haven't tested either way myself.

    As for MS Access, MS Access VBA can run the Shell method just as well as Word can, so if it's more convenient to start from MS Access, it should work equally well, I would think. And this would keep your document "clean" of unnecessary data and your template free of sometimes confusing command overrides.

    Hope this helps.

  4. #4
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    1,560
    Thanks
    37
    Thanked 1 Time in 1 Post

    Re: Launch .pdf file from Word (2000/9.0.3821 SR1)

    Kevin: I confess to being a novice at this kind of thing, so I'm a little bit lost. So far, I've copied and pasted the code you furnished into a module in the VB editor. (I launched my Word document, then clicked on ToolsMacro...>Visual Basic Editor; then I inserted a blank module and pasted the code into that module.)

    If this sounds like I know what I'm doing, don't let me fool you--I'm almost clueless as to what to do next. Tell me if I'm right: (1) I need to change "lpOperation_" to read "lpPrint_". (2) I need to change lpFile to read the name of the file I want to print...? Exactly how would this read? And what if the file name includes spaces? The file in question reads something like this: "227 Course worksheets (PRINT WITH HANDOUT).pdf" (we put the words in all caps as a reminder to print this file along with the handout for course number 227). I suppose I need to put brackets [ ] around the file name? And I suppose I need to include the file path, inasmuch as the file resides on a shared (network) directory.

    Thanks!

  5. #5
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    1,560
    Thanks
    37
    Thanked 1 Time in 1 Post

    Re: Launch .pdf file from Word (2000/9.0.3821 SR1)

    Thanks for the prompt reply. As I mentioned in my response to Kevin, I'm in a learning mode--thanks for your patience.

    Actually, no, I did not know I can replace Word commands by creating a macro. Obviously, I can't create a macro such as you describe with the macro recorder--I must use the VB editor. So, in the editor I create my macro and put the file path and name inside those parentheses...right? How can I be sure this macro will run just with this one file? I certainly don't want to launch this PDF file every time I click FileOpen in some other document.

    Where and how do I create this "Custom Document Property"? What is "the Shell method"? (I sought out the VB Help file before I asked this question. Under Contents, I found a listing for Visual Basic Language Reference > Methods. A-ha! said I, and I eagerly expanded the Methods listing. Boy, was I ever disappointed to find no reference to the Shell method!)

    Thanks again...both of you!

  6. #6
    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 .pdf file from Word (2000/9.0.3821 SR1)

    To limit a customized FileOpen macro to running in the "right" context, you either can insert it into each individual document, which is not recommended because each document will trigger a macro warning, or into a custom template for the relevant documents. But even if it runs all the time, you can write it in such a way that it always does the right thing. On that point, I am an optimist. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    Here's an example:

    (1) In your document, choose File|Properties..., click the Custom tab, then in Name type PDFpath and in the Value field enter a sample path to a PDF file, such as c:testingsample.pdf. The Add button should become enabled. Click Add, and your new custom property will appear in the list below.

    (2) Fire up the VBE (Alt+F11) and use the Project tree to open one of the modules for the Normal template. Later, you can move this to a custom template. Create a new macro as follows:

    <pre>Sub FileOpen()
    ' Display FileOpen dialog and take no further action if user cancels
    If Dialogs(wdDialogFileOpen).Show <> -1 Then Exit Sub
    ' Check for a PDFpath in the document and, if found, open the PDF
    Dim docProp As DocumentProperty, custProps As DocumentProperties
    Set custProps = ActiveDocument.CustomDocumentProperties
    For Each docProp In custProps
    If docProp.Name = "PDFpath" Then
    ' The property exists! Try to open the associated PDF file
    Shell <font color=red>"C:Program FilesAdobeAcrobat 4.0AcrobatAcrobat.exe """</font color=red> & _
    docProp.Value & """"
    ' Stop checking custom document properties
    Exit For
    End If
    Next
    If Not docProp Is Nothing Then
    Set docProp = Nothing
    End If
    Set custProps = Nothing
    End Sub</pre>

    Now... you will note that I hardcoded in the path to the Acrobat program that I have. In fact, your users might have either Acrobat.exe or Acrobat Reader (AcroRd.exe?). To account for this requires some additional gymnastics: specifically, looking up in the registry which program handles PDF files. With that change the code would be as follows:
    <pre>Sub FileOpen()
    ' Display FileOpen dialog and take no further action if user cancels
    If Dialogs(wdDialogFileOpen).Show <> -1 Then Exit Sub
    ' Check for a PDFpath in the document and, if found, open the PDF
    Dim docProp As DocumentProperty, custProps As DocumentProperties
    Set custProps = ActiveDocument.CustomDocumentProperties
    For Each docProp In custProps
    If docProp.Name = "PDFpath" Then
    ' The property exists! Look up the handler for PDF files
    Dim strAcroExecutable As String
    strAcroExecutable = System.PrivateProfileString("", "HKEY_CLASSES_ROOT" & _
    System.PrivateProfileString("", "HKEY_CLASSES_ROOT.pdf", "") & _
    "shellopencommand", "")
    ' Replace the %1 parameter with the file name
    strAcroExecutable = Replace(strAcroExecutable, "%1", docProp.Value)
    ' Run the command string
    Shell strAcroExecutable, vbNormalFocus
    ' Stop checking custom document properties
    Exit For
    End If
    Next
    If Not docProp Is Nothing Then
    Set docProp = Nothing
    End If
    Set custProps = Nothing
    End Sub</pre>

    I think that should do it. Does it work for you?

  7. #7
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    1,560
    Thanks
    37
    Thanked 1 Time in 1 Post

    Re: Launch .pdf file from Word (2000/9.0.3821 SR1)

    Jefferson: That picture of you on the forum--that is you, I suppose--is that what one looks like after one learns how to write code like this? <img src=/S/duck.gif border=0 alt=duck width=23 height=23> (Just kidding!)

    I'm not at work right now, and maybe it's just as well. Here in the quiet of my home office, I can sit and admire this example and wonder at the knowledge and talent it takes to figure this stuff out. You better believe I'll give it a whirl tomorrow. Thank you very much!

  8. #8
    Lounger
    Join Date
    Mar 2001
    Location
    NSW, Australia
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Launch .pdf file from Word (2000/9.0.3821 SR1)

    Thanks jscher for this code - it works a treat 4 me. One thing though - is it possible to add extra code to automatically print the .pdf file to default printer and then close Acrobat Reader ?

    Many thanks.

  9. #9
    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 .pdf file from Word (2000/9.0.3821 SR1)

    Kevin has posted the command line for that, showing the switch that forces Acrobat to "print" a document. I assume you could edit the Shell statement in my macro, adding the necessary switch, and do exactly what you want. (Kevin's post might be here, or VBA, or General Office Solutions... I can't recall.)

  10. #10
    Lounger
    Join Date
    Mar 2001
    Location
    NSW, Australia
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Launch .pdf file from Word (2000/9.0.3821 SR1)

    Thanks for that Jefferson ... I've found the post from Kevin in VBA, subject "More PDF fun" ... so I'm off to give it a go. <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23>

Posting Permissions

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