From: DaveLett on 27 Oct 2009 11:41 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
From: Graham Mayor on 28 Oct 2009 04:11 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 My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> ><<> <>>< ><<> <>>< <>><<> DaveLett wrote: > 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
From: DaveLett on 29 Oct 2009 10:11 Hi Graham, I have the same experience that you do; OP still gets error with original routine. Thanks for having a look, though. Dave
From: Graham Mayor on 29 Oct 2009 11:44 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 My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> ><<> <>>< ><<> <>>< <>><<> DaveLett wrote: > Hi Graham, > I have the same experience that you do; OP still gets error with > original routine. Thanks for having a look, though. > > Dave
|
Pages: 1 Prev: Show/hide dropdown boxes Next: Get keybindings of word commands in commandbarcontrols? |