PDA

View Full Version : save and Merge PDFs From Excel



MH123
10-16-2016, 07:40 AM
Hi

i'm trying to fill and save word documents from excel data , i have some problems the first one

i want to save my documents as PDF too , but when i want to open PDF file , The acrobat reader doesn't open it and shown this error :

acrobat couldn't open .... (screenshot of error attached )

I used this code for save pdf :

doc.saveas ....."name.pdf"


Why ? And How to fix this ?

MH123
10-16-2016, 07:45 AM
and second

Is there possible to attach pdf to cells and merge them ?or from excel vba could i merge 2 pdf together ?
if yes , how ?

Thanks

Kenneth Hobs
10-16-2016, 08:53 AM
It is best to ask one question per thread. It is also best not to reply to your own thread too quickly. Some helpers look for 0 responses and assume that 1 response was enough to help someone. I would suggest waiting 1 day before responding to your own thread to bump it to the top for those that sort by newest first.

For #1, you saved the file as an MSWord file but gave it a pdf file extension. As such, Windows does not know how to open a pdf file that is really an MSWord file. Had you recorded a macro, you would have seen the proper syntax. You may find this routine handy.

'Requires Tools > References > Microsoft Word 14.0 Object Library
Sub MakeWordPDFFile(sMSWordFilename As String, sOutputFilename As String)
Dim wdApp As Word.Application, wdDoc As Word.Document
Dim wdExportFormatPDF As Integer, wdExportOptimizeForPrint As Integer
Dim wdExportAllDocument As Integer, wdExportDocumentContent As Integer
Dim wdExportCreateNoBookmarks As Integer

If Not CreateObject("Scripting.FileSystemObject").FileExists(sMSWordFilename) Then _
Exit Sub

On Error GoTo errorHandler
Set wdApp = New Word.Application
With wdApp
Set wdDoc = .Documents.Open(sMSWordFilename)
.Visible = False
End With

If CreateObject("Scripting.FileSystemObject").FileExists(sOutputFilename) Then _
Kill sOutputFilename

wdExportFormatPDF = 17
wdExportOptimizeForPrint = 0
wdExportAllDocument = 0
wdExportDocumentContent = 0
wdExportCreateNoBookmarks = 0

wdDoc.ExportAsFixedFormat _
OutputFileName:=sOutputFilename, ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent, _
IncludeDocProps:=True, KeepIRM:=True, CreateBookmarks:= _
wdExportCreateNoBookmarks, DocStructureTags:=True, BitmapMissingFonts:= _
True, UseISO19005_1:=False
wdDoc.Close False

errorExit:
On Error Resume Next
Set wdDoc = Nothing
Set wdApp = Nothing
Exit Sub

errorHandler:
MsgBox "Unexpected error: " & Err.Number & vbLf & Err.Description
Resume errorExit
End Sub


As for #2, that is more involved and more information would be needed. Pasting code between code tags or attaching a file is a better way to help us help you than an image though that was fine for #1. Click the # icon on a reply's toolbar to insert the tags. Click Go Advanced button in lower right of a reply and then the paperclip icon on the toolbar to Browse and Upload a file.

For the merging issue, that depends on many things. If you want to merge embedded files, then we would need to know if they are links to files or fully embedded. If it is to merge pdf files that exist, that is another matter though that method is used anyway.

Excel has no way to merge existing pdf files. VBA can be used to call a 3rd party program or reference a 3rd party installed object. Some of those ways are Acrobat - not Reader, PDFSam, PDFCreator v1+, PDFCreator v2+, PDFFill, etc.