Convert to PDF

D

DaveLett

I'm posting a question that was raised in a different forum. We use a tool
that can publish to Word, and the tool allows you to create AfterPublish Word
routines. The OP is looking to convert a published Word document to PDF. The
OP found and modified the following routine. I have commented to show what
was working and what wasn't working. The OP is using Word 2003 and Acrobat 7.
How can we remove the two MsgBox lines and prevent Word from crashing?

Sub ConvertToPDFWithLinks()
' ----------------------------------------------------
' ConvertToPDFWithLinks - convert the current document
' to a PDF file, in the same folder as the document
' ----------------------------------------------------
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

'''The following line is from the original routine
'''pmkr.GetCurrentConversionSettings stng

'''The OP changed the line above to the one following
'''The OP says that Word does not raise an error when
'''executing the following line but does raise an error
'''when executing the previous line
Set stng = pmkr.GetCurrentConversionSettings()
stng.AddBookmarks = True ' make desired settings
stng.AddLinks = True
stng.AddTags = False
stng.ConvertAllPages = True
stng.CreateFootnoteLinks = True
stng.CreateXrefLinks = True
stng.OutputPDFFileName = pdfname
stng.PromptForPDFFilename = False
stng.ShouldShowProgressDialog = True
stng.ViewPDFFile = False

'''The OP says that they added this MsgBox b/c, without it,
'''Word crashes
MsgBox "Starting Conversion", vbOKOnly, "Starting Conversion"

'''The following line is from the original routine
'''pmkr.CreatePDFEx stng, 0 'perform conversion

'''The OP changed the line above to the one following
'''The OP says that Word does not raise an error when
'''executing the following line but does raise an error
'''when executing the previous line; notice that the only
'''change is commenting out the second parameter
pmkr.CreatePDFEx stng ', 0 perform conversion

'''The OP says that they added this MsgBox b/c, without it,
'''Word crashes
MsgBox "Conversion complete", vbOKOnly, "Conversion done"

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

Thanks
Dave
 
G

Graham Mayor

Dave
This attracted my attention because I had been looking for a way to
establish of the Word 2007 PDF add-in was installed, and while it doesn't
help with that I took a look at the code. I am using Acrobat 8 as opposed to
7, which should be essentially similar, and in Word 2003, the following
works provided a reference is made to the AdobePDFMakerForOffice library.
FWIW it also works in Word 2007. I have added a few more notes :)


Sub ConvertToPDFWithLinks()
' ----------------------------------------------------
' ConvertToPDFWithLinks - convert the current document
' to a PDF file, in the same folder as the document
' ----------------------------------------------------
Dim pdfname, i, a
Dim pmkr As AdobePDFMakerForOffice.PDFMaker
Dim stng As AdobePDFMakerForOffice.ISettings

With ActiveDocument

'The document must be saved - so save it!

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

'The original definition of pdfname displayed an error
'This one works

pdfname = Left(.name, InStrRev(.name, ".")) & "pdf"

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

'''The following line is from the original routine
pmkr.GetCurrentConversionSettings stng

'''The OP changed the line above to the one following
'''The OP says that Word does not raise an error when
'''executing the following line but does raise an error
'''when executing the previous line

'Not here - the original line works

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

'''The OP says that they added this MsgBox b/c, without it,
'''Word crashes

'Again not here. The routine works without the message box.

'MsgBox "Starting Conversion", vbOKOnly, "Starting Conversion"

'''The following line is from the original routine
'and works now reinstated

pmkr.CreatePDFEx stng, 0 'perform conversion

'''The OP changed the line above to the one following
'''The OP says that Word does not raise an error when
'''executing the following line but does raise an error
'''when executing the previous line; notice that the only
'''change is commenting out the second parameter
'pmkr.CreatePDFEx stng ', 0 perform conversion

'''The OP says that they added this MsgBox b/c, without it,
'''Word crashes

'Not here. It works as is.

'MsgBox "Conversion complete", vbOKOnly, "Conversion done"

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


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

DaveLett

Hi Graham,
I have the same experience that you do; OP still gets error with original
routine. Thanks for having a look, though.

Dave
 
G

Graham Mayor

This piece of code is quite handy. I have been trying to call it from a
batch process - essentially the code below - but while it will happily
convert the first document in the batch, it then has a hissy fit. It throws
an error at the line oDoc.Close SaveChanges:=wdSaveChanges
because it appears to close then re-opens the document for some reason.
which screws up the batch. Even taking out that line (which should leave the
doc open and carry on with the next) doesn't rectify the issue. If you have
any insights into how to get around that, I would be interested in hearing
them.

The ever helpful Adobe were a waste of space -
http://support.adobe.com/devsup/devsup.nsf/docs/52841.htm :(

Thankfully for my own use (preserving hyperlinks) Word 2007's PDF plug-in
works without all the drama, but Acrobat is the only way for older version
users.

Sub BatchProcess()
Dim strFileName As String
Dim strPath As String
Dim oDoc As Document
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
.title = "Select folder and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User", , _
"List Folder Contents"
Exit Sub
End If
strPath = fDialog.SelectedItems.Item(1)
If Right(strPath, 1) <> "\" _
Then strPath = strPath + "\"
End With
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
If Left(strPath, 1) = Chr(34) Then
strPath = Mid(strPath, 2, Len(strPath) - 2)
End If
strFileName = Dir$(strPath & "*.doc")
While Len(strFileName) <> 0
Set oDoc = Documents.Open(strPath & strFileName)
'Do what you want with oDoc
'*********************************
Call ConvertToPDFWithLinks
'*********************************
oDoc.Close SaveChanges:=wdSaveChanges
strFileName = Dir$()
Wend
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
M

Mauricio Vengoechea

Hi All,

I have a MS word file with several pages and would like to convert it into pdf format, creating one different pdf per page (I have Adobe Acrobat 8 Pro and Word 2003).

I would like to create a VBA/macro in Word to do so if possible.

I need the VBA code to do the following:

1. Each page has a unique employee ID (e.g. "ID 123456") in the first line of the page. Convert each page into a separate pdf
2. Save each pdf file with the employee ID number as the name.

Would you please tell me how to do that? Thank you very much for your help in advance. It is greatly appreciated!

Should you want me to clarify any unknowns, please post your questions here. Thanks!
 
G

Graham Mayor

If this is the result of a mail merge then it would be simpler to use the
addin from http://www.gmayor.com/individual_merge_letters.htm to split the
documents on the fly into PDF format documents.

If that doesn't work for you then you need to clarify - Each page has a
unique employee ID (e.g. "ID 123456") in the first line of the page.
'Page' and 'Line' are a vague concepts in Word.

In the case of page, what ends the page - is there a section break, a page
break or simple automatic text flow. Is there a header/footer with content?
How would that be related to the individual documents when split?

As for 'line', do you in fact mean paragraph? Is that paragraph on the page
or in the header/footer. Is there anything else in the 'line'. Are there any
empty paragraphs before the 'line'?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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