Wednesday, July 22, 2009

How to Create PDF from Word Document using VBA

Convert Word to PDF using VBA

Word 2007 has a new method - Document.ExportAsFixedFormat, which saves the document as PDF or XPS format

The following code will save the current document as PDF in the same path

Sub Convert_2_PDF()

ActiveDocument.ExportAsFixedFormat OutputFileName:= _

ActiveDocument.Path & "\" & ActiveDocument.Name & ".pdf", ExportFormat:= _

wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _

wdExportOptimizeForPrint, Range:=wdExportAllDocument, _

Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _

CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _

BitmapMissingFonts:=True, UseISO19005_1:=False

End Sub

10 comments:

  1. consider replacing ActiveDocument.Name
    with Left(ActiveDocument.Name, _
    Len(ActiveDocument.Name) - 5) or Left(ActiveDocument.Name, _
    Len(ActiveDocument.Name) - 4)

    to remove .DOCX or .DOC from before .PDF in the output name, respectively

    ReplyDelete
  2. Thanks for sharing, very useful!
    I need something for converting back to word from PDF. So far I have been using this one:
    http://www.pdftodocconverterpro.com
    Is there anything else that you would recommend?

    ReplyDelete
  3. Thanks Michael Kuzmin. This would be of good help

    ReplyDelete
  4. I tried pasting the vba code into a module into a word document and then running it. I get an "Can't execute code in break mode" error. Any suggestions.

    ReplyDelete
  5. It is working now. I copied as is and here are spaces between each line and that is the reason why it was not working.

    ReplyDelete
  6. Apologies Maka, the spaces were intended for brevity

    Extremely sorry for the time and effort you have put on this

    ReplyDelete
  7. This works great but how do it get it to do a Save As so that I can use the doc as a template and save it as a different name. I am using a blank template and save the template as different names, want to be able to type in a name in the Save As window.

    ReplyDelete
  8. Anonymous5:28 AM

    I have been looking for something like this to generate PDFs without the prompting. I stumbled across this and should be able to use it in code to generate individual PDFs from a mail merged template.

    Your a saint!

    ReplyDelete
  9. Anonymous3:47 AM

    Hi !
    I used the routine. It works well. But my problem is the directory where to save the document. The user should be able to change it. So it would be better to use the dialog "wdDialogExportAsFixedFileFormat".
    Can I preselect the path / directory and the filename for the dialog?
    I used the FileSaveAs Dialog and there it was possible to use '.Name = "abc"' and so on. But the export-dialog doesn't accept it.
    I haven't found an example code yet. Does anybody know how it works?

    Thanks
    VBAlex

    ReplyDelete
  10. Nice!! it really help me..
    May I know how to convert powerpoint into pdf?
    I tried using the same method but failed to do it.
    Any advise?

    Thanks in advanced..

    ReplyDelete

StumbleUpon
Share on Facebook

Visual Basic for Applications (VBA) Forum (recent threads)

CodeKeep VBA Feed

Visual Studio Tools for Office Forum (recent threads)

Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.

Office Business Applications (OBA) Team Blog

MSDN Code Gallery Published Resources For Tag VSTO

microsoft.public.vsnet.vstools.office Google Group