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
consider replacing ActiveDocument.Name
ReplyDeletewith 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
Thanks for sharing, very useful!
ReplyDeleteI 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?
Thanks Michael Kuzmin. This would be of good help
ReplyDeleteI 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.
ReplyDeleteIt is working now. I copied as is and here are spaces between each line and that is the reason why it was not working.
ReplyDeleteApologies Maka, the spaces were intended for brevity
ReplyDeleteExtremely sorry for the time and effort you have put on this
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.
ReplyDeleteI 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.
ReplyDeleteYour a saint!
Hi !
ReplyDeleteI 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
Nice!! it really help me..
ReplyDeleteMay I know how to convert powerpoint into pdf?
I tried using the same method but failed to do it.
Any advise?
Thanks in advanced..
Placing image in each page of word and convert it in single pdf using vba
ReplyDeleteDear 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
When I use this code segment it tells me:
ReplyDeleteRun-Time error '-2147467259 (8004005)':
This file is read only
ANy suggestions?