Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

Use vba to Print Word document to pdf

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
WC Justice - 24 Sep 2006 14:58 GMT
I use an application that uses Automation to create a Word document from
within Access.  After checking and possibly correcting the Word document, we
then print the Word document to a pdf file.  Some of our computers have
Acrobat Professional
7.0, while others have PrimoPDF.  This process is acceptable except that we
have to go through so many manual steps.

I would like to be able to generate the pdf from within Access using vba, or
at least from within Word.  I get close, but don't quite get it.  We make
extensive use of Automation, so I am familiar with the basics.  I have no
trouble opening the specific document in hidden mode.  I even get the file
to save as a pdf, but it about 10 x larger than it should be and is
apparently not really a pdf file.

As a recap, I would like to be able to use vba to go direct from Word to pdf
with no manual intervention (the filename and location do not change, only
the extension.).  Even better would be to accomplish this through Access, as
we use Access to create the initial Word document.

Searching the internet makes it clear that it is not as easy as I thought it
would be.  I've seen references to Acrobat Distiller and code modifying the
Windows registery for every print.  Can you help me simplify this?
Doug Robbins - Word MVP - 24 Sep 2006 18:57 GMT
If you need to check the document, you may as well just create the .pdf by
printing it with either the Adobe Printer or the Primo PDF Printer.

       Dim printer As String
       'Get the name of the default printer
       With Dialogs(wdDialogFilePrintSetup)
           printer = .printer
           'Set the Adobe PDF or PrimoPDF as the printer
           .printer = "Adobe PDF" 'or "PrimoPDF"
           .Execute
       End With
       'Print the document
        [DocumentObject].PrintOut Background:=False
        'Restore the default printer
        With Dialogs(wdDialogFilePrintSetup)
             .printer = printer
             .Execute
        End With

To be able to tell which one is available, see the article "Getting names of
available printers" at:

http://www.word.mvps.org/FAQs/MacrosVBA/AvailablePrinters.htm

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

>I use an application that uses Automation to create a Word document from
> within Access.  After checking and possibly correcting the Word document,
[quoted text clipped - 25 lines]
> the
> Windows registery for every print.  Can you help me simplify this?
WC Justice - 24 Sep 2006 23:10 GMT
Is there any way to include the path and file name in the code so that no
human intervention is necessary?

> If you need to check the document, you may as well just create the .pdf by
> printing it with either the Adobe Printer or the Primo PDF Printer.
[quoted text clipped - 49 lines]
> > the
> > Windows registery for every print.  Can you help me simplify this?
Doug Robbins - Word MVP - 25 Sep 2006 04:40 GMT
In the Printer Properties dialog for both the Adobe and Primo PDF printers,
there is a box to check that controls whether or not the Filename to save as
dialog is presented to the user and whether the result is displayed.  If the
filename dialog is not presented, the .pdf will be saved with the default
name which is the existing filename with .pdf substituted for the existing
extension.

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Is there any way to include the path and file name in the code so that no
> human intervention is necessary?
[quoted text clipped - 64 lines]
>> > the
>> > Windows registery for every print.  Can you help me simplify this?
WC Justice - 26 Sep 2006 02:16 GMT
Thank you and others who have contributed.  I have made much progress based
on your suggestions, but am still experiencing  a problem.

I have Acrobat installed on twocomputers, one running Windows Server 2003
(Exchange, file and print server), the other running XP Pro.  The one
running Server 2003 is working perfectly.  The XP computer works, but it
truncates the file name.  The file names begin with a number with the format
being similar to the following:

1234567890.001 - Some File Name.doc

The XP computer saves the .pdf file as 1234567890.pdf.  Both computers have
the same copy of Acrobat installed.

I have checked the settings and they all seem to be the same, but I'm
obviously missing something.  Any ideas?

> In the Printer Properties dialog for both the Adobe and Primo PDF printers,
> there is a box to check that controls whether or not the Filename to save as
[quoted text clipped - 56 lines]
> >> >
> >> > As a recap, I would like to be able to use vba to go direct from Word

> >> > to
> >> > pdf
[quoted text clipped - 11 lines]
> >> > the
> >> > Windows registery for every print.  Can you help me simplify this?
Doug Robbins - Word MVP - 26 Sep 2006 04:54 GMT
I haven't investigated the reason for the difference, but it is the decimal
point in the 1234567890.001 that is triggering the behaviour.  You should
avoid using a decimal point in the field name.  I would use 1234567890-001
instead.

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Thank you and others who have contributed.  I have made much progress
> based
[quoted text clipped - 106 lines]
>> >> > the
>> >> > Windows registery for every print.  Can you help me simplify this?
Paul Shapiro - 25 Sep 2006 12:54 GMT
The only way I found to specify the Acrobat file name is setting a registry
key. The key varies depending on the Acrobat version.  In all cases the key
is in the CurrentUser part of the registry.

Here is the code I use for versions 5, 6 and 7. Note that for this to work
the user MUST have set the printer defaults to
    a) not prompt for filename (no support for setting this via registry in
Acrobat 6)
    b) desired location for output files, or leave blank for source folder.
You can specify the folder (if I remember correctly) in the registry key
setting.

   'Setup for acrobat printing
   Select Case lngAcrobatVersion
   Case 5
       'Set the printer
       mobjWord.ActivePrinter = "Acrobat PDFWriter"

       'Set the registry value telling Acrobat the file name
       fSuccess = pjsRegistryHKCUWrite( _
        strSubkey:="Software\Adobe\Acrobat PDFWriter", _
        strValueName:="PDFFileName", _
        strValueValue:=strAcrobatFileNameWithPath _
       )
   Case 6
       'Set the printer
       mobjWord.ActivePrinter = "Adobe PDF"

       'Set the registry value telling Acrobat the file name
       fSuccess = pjsRegistryHKCUWrite( _
        strSubkey:="Software\Adobe\Acrobat\PDFMaker\6.0\Global\Settings", _
        strValueName:="PDFFileName", _
        strValueValue:=strAcrobatFileNameWithPath _
       )
   Case 7
       'Set the printer
       mobjWord.ActivePrinter = "Adobe PDF"

       'Set the registry value telling Acrobat the file name, _
        as per the Distiller API Reference. _
        This key will automatically be deleted once the next pdf file is
created _
        by printing to Adobe PDF printer from the specified application. _
        NOTE: This only works if Adobe PDF printer preferences specify
prompting _
        for the file name!
       'The registry key name must be the full application path and name.
       strWordEXEFullPathName = mobjWord.Path & "\" & "WINWORD.EXE"
       fSuccess = pjsRegistryHKCUWrite( _
        strSubkey:="Software\Adobe\Acrobat Distiller\PrinterJobControl", _
        strValueName:=strWordEXEFullPathName, _
        strValueValue:=strAcrobatFileNameWithPath _
       )
Paul Shapiro

> Is there any way to include the path and file name in the code so that no
> human intervention is necessary?
[quoted text clipped - 64 lines]
>> > the
>> > Windows registery for every print.  Can you help me simplify this?
Michael Bednarek - 26 Sep 2006 10:43 GMT
>The only way I found to specify the Acrobat file name is setting a registry
>key.
[snip]

I found it much simpler to use Acrobat's PdfDistiller class, then using
"Acrobat Distiller" as printer and print to a .ps file, and lastly
.FileToPDF; see
<http://groups.google.fi/group/microsoft.public.access/msg/73a70a6ee8868107?fwc=1>.

Signature

Michael Bednarek   http://mbednarek.com/   "POST NO BILLS"

Michael Bednarek - 25 Sep 2006 13:55 GMT
>I use an application that uses Automation to create a Word document from
>within Access.  After checking and possibly correcting the Word document, we
[quoted text clipped - 18 lines]
>would be.  I've seen references to Acrobat Distiller and code modifying the
>Windows registery for every print.  Can you help me simplify this?

You're right - it's not simple.

I've used VBA code with Ghostscript and I've shown some code for
Acrobat/Distiller and PDFCreator, but I'm not familiar with PrimoPDF. I
don't use Ghostscript anymore because it involves fiddling with registry
settings on-the-fly which is inherently unstable. Acrobat/Distiller
offers the most stable COM interface, but is quite expensive. PDFCreator
now also has a COM interface and works quite well.

Acrobat:
<http://groups.google.fi/group/microsoft.public.access/msg/73a70a6ee8868107>;
PDFCreator:
<http://groups.google.com.au/group/microsoft.public.excel/msg/2133639a2b9fbecb>.

Good luck.

Signature

Michael Bednarek   http://mbednarek.com/   "POST NO BILLS"

Mike Starr - 05 Oct 2006 16:43 GMT
I dealt with this for a client template recently and resolved your issues.
However, that's on my machine at home, not on my machine at the office. The
key to resolving it was to use a Sendkeys statement to fill in the file
name. The downside was that for some reason, the first iteration of this
macro caused a 70-second system hang... every time. I was creating four
different PDFs from the same Word document. The second through fourth ones
blistered right along. I have no idea why the sendkeys statement caused a
system freeze... couldn't figure out any reason it was happening or any
resolution. Ultimately, the client was okay with it as long as it was
properly document and the users were trained to expect it. I ended  up
putting up a message box that said (essentially) "Patience, Grasshopper".
I'll post my macro code sometime within the next couple days.

Best regards

Mike
--
Mike Starr                            WriteStarr Information Services
Technical Writer   -   Online Help Developer   -    Website developer
Graphic Designer    -    Desktop Publisher   -       MS Office Expert
Phone: (262) 694-1028 - Tollfree: (877) 892-1028 - Fax:(262) 697-6334
Email: mike@writestarr.com      -      Web: http://www.writestarr.com
> I use an application that uses Automation to create a Word document from
> within Access.  After checking and possibly correcting the Word document, we
[quoted text clipped - 18 lines]
> would be.  I've seen references to Acrobat Distiller and code modifying the
> Windows registery for every print.  Can you help me simplify this?

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.