Use vba to Print Word document to pdf

W

WC Justice

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?
 
D

Doug Robbins - Word MVP

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


--
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
 
W

WC Justice

Is there any way to include the path and file name in the code so that no
human intervention is necessary?

Doug Robbins - Word MVP said:
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


--
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

WC Justice said:
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?
 
D

Doug Robbins - Word MVP

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.

--
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

WC Justice said:
Is there any way to include the path and file name in the code so that no
human intervention is necessary?

Doug Robbins - Word MVP said:
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


--
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

WC Justice said:
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?
 
P

Paul Shapiro

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

WC Justice said:
Is there any way to include the path and file name in the code so that no
human intervention is necessary?

Doug Robbins - Word MVP said:
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


--
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

WC Justice said:
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?
 
M

Michael Bednarek

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?

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.
 
W

WC Justice

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?


Doug Robbins - Word MVP said:
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.

--
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

WC Justice said:
Is there any way to include the path and file name in the code so that no
human intervention is necessary?

Doug Robbins - Word MVP said:
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


--
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,
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?
 
D

Doug Robbins - Word MVP

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.

--
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

WC Justice said:
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?


Doug Robbins - Word MVP said:
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.

--
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

WC Justice said:
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.

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


--
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,
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?
 
M

Mike Starr

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: (e-mail address removed) - Web: http://www.writestarr.com
 

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