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

12 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
  11. Placing image in each page of word and convert it in single pdf using vba

    Dear All,

    I am new to VBA. Can any one answer me the below queries

    I have a set of images in a folder which needs to be converted into a single pdf using vba or using vba into the word document.

    1. I was trying to paste all the images in each page of a word document using vba but instead of each page, images are automatically getting pasted in the first page itself.

    2. Please also tell me how to save word files to pdf using vba.

    Thanks in Advance.

    Find below my code.

    Sub CollateWorkbooks()
    Dim MyPath As String
    Dim strFilename As String
    Dim i As Integer
    'Dim fromTop As Integer
    'fromTop = 5
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    MyPath = "C:\Users\user\Desktop\New folder" ' change to suit
    strFilename = Dir(MyPath & "\*.jpg", vbNormal)
    If Len(strFilename) = 0 Then Exit Sub
    Do Until strFilename = ""
    ActiveDocument.Shapes.AddPicture FileName:=MyPath & "\" & strFilename, _
    LinkToFile:=False, _
    SaveWithDocument:=True, _
    Left:=-5, _
    top:=5, _
    Width:=300, _
    Height:=150
    strFilename = Dir()
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Count:=1
    'fromTop = fromTop + 160
    Loop
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    End Sub

    ReplyDelete
  12. Anonymous8:06 AM

    When I use this code segment it tells me:

    Run-Time error '-2147467259 (8004005)':
    This file is read only

    ANy suggestions?

    ReplyDelete

StumbleUpon
Share on Facebook
Related Posts Plugin for WordPress, Blogger...

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