Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How do I invoke Word PDFMaker from a macro?

New Here ,
Jan 21, 2009 Jan 21, 2009
Hi,

I need to automate creating PDF's from Word with hyperlink fields converted precisely. The Adobe toolbar "Convert to Adobe PDF" button does exactly what I need. How do I invoke this type of conversion programmaticially, e.g. from a Word macro? Can anyone point me to a SDK document that discusses this?

(BTW, printing the document to the "Adobe PDF" printer will not work. I need the exact functionality of the toolbar button).
24.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 21, 2009 Jan 21, 2009
I have found enough information to script the conversion, but not the actual documentation. I'd still appreciate any pointers anyone could give me to the documentation for the AdobePDFMakerForOffice.PDFMaker object.

It would be pretty peculiar for this not to be documented since as far as I can tell, this is the only way to create PDF files from Word documents with functional intra- and inter-document hyperlinks. If there is a better, documented way please let me know by responding to this note!

In any case, since I haven't seen this explained anywhere it might be useful to post some notes here. To script a conversion that doesn't require user intervention, get a reference to the PDFMaker add-in object from Word. From that, use the GetCurrentConversionSettings (or GetDefaultConversionSettings) method to get an ISetting object. Set property OutputPDFFilename to the desired PDF filename, and property PromptForPDFFilename to False. After making any other desired setting changes, use the PDFmaker object's CreatePDFEx property to perform the conversion.

Here is a sample routine in Word VBA. Before you can run it, you must open the Word macro editor and using Tools > References, add a reference to your copy of AcrobatPDFMakerForOffice.tlb, in e.g. \Program Files\Adobe\Acrobat 8.0\PDFMaker\Office.

>
' ----------------------------------------------------
' ConvertToPDFWithLinks - convert the current document
' to a PDF file, in the same folder as the document
' ----------------------------------------------------

Sub ConvertToPDFWithLinks()
Dim pdfname, i, a
Dim pmkr As AdobePDFMakerForOffice.PDFMaker
Dim stng As AdobePDFMakerForOffice.ISettings

If Not ActiveDocument.Saved Then
MsgBox "You must save the document before converting it to PDF", vbOKOnly, ""
Exit Sub
End If

Set pmkr = Nothing ' locate PDFMaker object
For Each a In Application.COMAddIns
If InStr(UCase(a.Description), "PDFMAKER") > 0 Then
Set pmkr = a.Object
Exit For
End If
Next
If pmkr Is Nothing Then
MsgBox "Cannot Find PDFMaker add-in", vbOKOnly, ""
Exit Sub
End If

pdfname = ActiveDocument.FullName ' construct output name
i = InStrRev(pdfname, ".")
pdfname = IIf(i = 0, pdfname, Left(pdfname, i - 1)) & ".pdf"

' delete PDF file if it exists
If Dir(pdfname) <> "" Then Kill pdfname

pmkr.GetCurrentConversionSettings stng
stng.AddBookmarks = True ' make desired settings
stng.AddLinks = True
stng.AddTags = True
stng.ConvertAllPages = True
stng.CreateFootnoteLinks = True
stng.CreateXrefLinks = True
stng.OutputPDFFileName = pdfname
stng.PromptForPDFFilename = False
stng.ShouldShowProgressDialog = True
stng.ViewPDFFile = False

pmkr.CreatePDFEx stng, 0 ' perform conversion

If Dir(pdfname) = "" then ' see if conversion failed
MsgBox "Could not create " & pdfname, vbOKOnly, "Conversion failed"
End If
End Sub

' ----------------------------------------------------

The steps of deleting the pdf file if it exists before converting and checking for the file after conversion are necessary. If the output file cannot be created (permissions, read-only, open etc) CreatePDFEx does not throw an error. It's better to find out.

Note that Intellisense and the object browser can show you all of the available properties and methods of pmkr and stng. Here's the list, though I can't guarantee its accuracy -- I might have mistyped:

PDFMaker methods:
CreatePDF (retval as Long)
CreatePDFEx (settings as AdobePDFMakerForOffice.ISettings, retval as Long)
GetCurrentConversionSettings (byref settings as AdobePDFMakerForOffice.ISettings)
GetDefaultConversionSettings (byref settings as AdobePDFMakerForOffice.ISettings)

ISettings properties: (all Boolean except as noted)
AddBookmarks
AddLinks
AddTags
AdvancedTagging
AllowBleeds
AlwaysFlattenLayers
AttachSourceFile
ConversionRoute
ConvertAllPages
ConvertHiddenSlides
ConvertMultimedia
ConvertSpeakerNotes
CreateDocInfo
CreateFootnoteLinks
CreateLayers
CreateMetadata
CreateTextNodes
CreateThreads
CreateXrefLinks
FitToOnePage
InvokedFromFeat
IsAutomation
IsConversionSilent
JobOptions as String
LayoutBasedOnPrinterSettings
OpenLayerPane
OutputPDFFileName as String
PDFACompliance
PreserveSpotColor
PreserveTransparency
PrintActiveSheetOnly
PrintBleedMarks
PrintCropMarks
PromptForPDFFilename
PromptForSheetSelection
Reserved as Long
SaveAnimations
SaveSlideShowTransitions
SecuritySettings as Object
SendForReview
SendViaEmail
ShouldShowProgressDialog
ViewPDFFile

ISettings methods:
GetConversionRange (byref Range)
SetConversionRange (Range)
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 04, 2013 Nov 04, 2013

I came across the above macro only recently (Nov 2013).  I can confirm that it works with Acrobat XI. I now have a nice macro that recursively scans a set of subdirectories and converts all word documents meeting my specific naming criteria to pdf.  Definitely a happy bunny so thanks to Brian.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 27, 2013 Dec 27, 2013

This code works great. The only issue I am having is clearing up the objects. I run a loop that uses this code and have several acrodist.exe running in the background. Is there a way to clean up the pdfmaker objects to successfuly close acrodist.exe after each iteration? I have tried releasecomobjects and setting the objects to nothing but that has not worked for me.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 27, 2013 Dec 27, 2013

Just try to remember the Acrobat add-in macro for MS Office is subject to Microsoft changes to MS Office and Adobe's updating to fix bugs and react to Microsoft's changing their products. What macros worked in MS Office 95 no longer work in MS Office 2010 because MS has changed the scripting language at least once. Also there is no requirement that Adobe keeps the same macro name for different versions of MS Office. Adobe has even changed the name of the executable exe file for Acrobat.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 21, 2014 Feb 21, 2014

Humiro, before the sub ends you need to set the adobe objects to nothing:

Set pmkr = nothing

Set stng = nothing

that should do the trick

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 08, 2017 Feb 08, 2017

I realize this is a fairly old post, but it turned out to be exactly what I was looking for. I'd like to say thank you for putting together this sample as it got me closer to where I need to be very quickly!

Now, I do have one question. Out of the box, the sample Sub provided works, but I am having a problem where as soon as pmkr.CreatePDFEx is called, VBA execution is halted, and macros within the document are disabled. (So although PDF Maker runs, the VBA can't pick back up afterwards.) Is anyone familiar with why this might be happening, or if there's a fix for it? Thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 09, 2017 Feb 09, 2017

In case you missed it: it is and remains unsupported. As such it's undocumented and likely to change or vanish. So, happy it works for you, but don't make any promises about the future. For enterprise server use look at LiveCycle PDF Generator.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 13, 2017 Mar 13, 2017

Caught that, yep! The reason I ask is that my current workplace has a process set in place where Word documents need to be processed with PDF Maker (in order to retain relative file links) and I was looking at a quick way to automate it. I was hoping that this would work as a temporary method until a better process can be adopted, but still haven't been able to get it to continue macro execution after invoking PDF Maker.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 18, 2017 Apr 18, 2017

Thank you for your post.  I have numerous (over 1000) PDFs that need to be regenerated from DOCXs after the documents are modified.  These documents are located over many folders.

I am running Win 7 Pro, Word 2010 and Acrobat XI Standard.  Like the previous poster, I have been unable to successfully execute pmkr.CreatePDFEx, but pmkr.CreatePDF works just fine.  I believe the latter uses whatever conversion profile is selected in the preferences for PDFMaker for Word, which I have set to not prompt for the output PDF name (outputs to the same base name as the DOCX file).  This will save me an amazing amount of time.

If I could figure out how to make Distiller work for this task, I would attempt that.  I have been unable to find documentation to interface with Distiller, at least with Acrobat XI Standard.  The ability to right-click on a DOCX and select "Convert to Adobe PDF" via Windows context menu would suffice if I could make that work in batch mode.  Again, I think this is a feature only available in the Pro versions of Acrobat.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2017 Apr 19, 2017

michaelm58012143  wrote

If I could figure out how to make Distiller work for this task, I would attempt that.  I have been unable to find documentation to interface with Distiller, at least with Acrobat XI Standard.  The ability to right-click on a DOCX and select "Convert to Adobe PDF" via Windows context menu would suffice if I could make that work in batch mode.  Again, I think this is a feature only available in the Pro versions of Acrobat.

I can help you with this part: As was explained by TSN, Distiller by itself does not help you with converting from MS Word to PDF, but when you use the "Adobe PDF" printer, the MS Word document is actually converted to PostScript and then passed to Distiller in order to convert it to PDF. The only problem is that in order to automate this, you need to be able to "tell" Distiller what filename to use for the output file. How this is done is documented in the Acrobat SDK: Acrobat DC SDK Documentation

You would have to automate MS Word to open a document, and then print it to the "Adobe PDF" printer (after you set the registry keys required by this solution). A few years ago, I wrote a tutorial about how to do that in VBS: Prevent the Save Dialog when Printing to the Adobe PDF Printer

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 19, 2022 Oct 19, 2022

The "retVal" in the following command is used to specify the PDF specification. 
pmkr.CreatePDFEx stng, 0 ' perform conversion
Here are the values I've been able to ID
0 seems to indicate the current specification (Adobe DC the value is 2)
7 Adobe 7 (1.6)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 03, 2023 Jul 03, 2023
LATEST

I've found this quite helpful as ExportAsFixedFormat in VBA can't or won't give a truly hi-res PDF like using "Create PDF" from the Acrobat ribbon. I am having a couple of problems with it though, if anyone using it successfully wants to weigh in:

  1.  It won't accept a SharePoint address as the path: Dir(pdfname) throws an error "Bad file name or number" and I don't get a PDF (and I assume the error has to do with why I'm not getting the PDF). It's the same path that works when using ExportAsFixedFormat, so it should be valid. If I put in a local path then I get the PDF without problems.
  2.  When it makes the PDF it closes the Word doc, which isn't what I want. I can't see any option to control this though.
  3.  OK there is a third: the dialogs I get when using it include "Printing" on top of "Converting to Adobe PDF" and it seems to cycle through the document twice, whereas pressing the button in the Ribbon just gives "Converting to Adobe PDF" and it goes through once. Just curious about this as it's apparently not the exact process that pressing the button on the ribbon uses?

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 22, 2009 Jan 22, 2009
It might not be a wise thing to explore.
http://support.adobe.com/devsup/devsup.nsf/docs/52841.htm
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 13, 2009 Jul 13, 2009

Sorry to kvetch, but do I understand correctly that Adobe removed this feature from Acrobat by declaring it unsupported.  For years I've been able to generate documents programmatically and store them as PDFs, with hyperlinks intact, with "Application.Run MacroName:='AdobePDFMaker.AutoExec.ConvertToPDF'."  Now this capability just disappears?  I would never have upgraded from Acrobat 6 had I known this.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 13, 2009 Jul 13, 2009

These feature wasn't supported.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 13, 2009 Jul 13, 2009

Hmm.  I can split hairs, too, so maybe the use of this macro by customers was never officially sanctioned or "supported" by Adobe.  But Adobe itself created the ConvertToPDF macro, and shipped it and installed it on customer machines as part of Acrobat over several releases.  In that context, to suddenly disable this function and say it was never supported seems a little misleading, no?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 19, 2017 Apr 19, 2017

All supported interfaces are described in the Acrobat SDK. Including Distiller. But Distiller has only one job: converting PostScript to PDF. It doesn't read Word documents. It would read PostScript generated by printing to file, but when you use PDFMaker it does not use printing, or PostScript, or Distiller.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines