I typically have multiple documents of different types (docx, pdf, png, jpg) that need to be combined into a single PDF file. For example, I have the following files right now in the directory:

DeclarationForHearing20180512.docx, ExhibitA.pdf, ExhibitB.png,
ExhibitC.jpg, TestimonyWitness1.docx, Evidence212232.pdf, etc...

I need to combine these into a single PDF file. Right now, I go into each Word doc and export it as PDF. Then I convert all the image files (png, jpg) into PDFs by printing to a PDF printer. Then I use PDFSam Basic to merge all the resulting PDFs into a single one.

As you can probably tell, this is a tedious procedure. Ideally, I'd like to write a batch file (or similar: powershell or vbs) that looped through the files in the directory, did all the necessary conversions, then merged into the final document.

Is it possible to do something like that?

  • Adobe Acrobat has this functionality, just need the select the files in any format and it combines them into one pdf. – Máté Juhász Apr 20 '18 at 9:41

I don't think just on single click the JOB can be accomlished. Therefore I would like to suggets solution for Few of them.

1. VBA code to convert multiple Word files in a folder to individual pdf files.

Sub convertword()
    Dim irow As Integer
    Dim objWord As Word.Application
    Dim newdoc As Word.Document
    Set objWord = New Word.Application
    objWord.Visible = True

    irow = 4
    Do While Cells(irow, 2) <> Empty
        Set newdoc = objWord.Documents.Open(Cells(irow, 2).Value)
        newdoc.ExportAsFixedFormat OutputFileName:=Cells(irow, 3).Value, _
        newdoc.Close (False)
        irow = irow + 1
End Sub

N.B. Add a reference to Microsoft Word 15.0 Object Library and then try this code.

This will help you to combine PDF files with VBA.

Dim objCAcroPDDocDestination As Acrobat.CAcroPDDoc
Dim objCAcroPDDocSource As Acrobat.CAcroPDDoc
Dim i As Integer
Dim iFailed As Integer

On Error GoTo NoAcrobat:
Set objCAcroPDDocDestination = CreateObject("AcroExch.PDDoc")
Set objCAcroPDDocSource = CreateObject("AcroExch.PDDoc")

objCAcroPDDocDestination.Open (arrFiles(LBound(arrFiles))) 'open the first file

   For i = LBound(arrFiles) + 1 To UBound(arrFiles)
        objCAcroPDDocSource.Open (arrFiles(i))
        If objCAcroPDDocDestination.InsertPages(objCAcroPDDocDestination.GetNumPages - 1, objCAcroPDDocSource, 0, objCAcroPDDocSource.GetNumPages, 0) Then
          MergePDFs = True

          iFailed = iFailed + 1
        End If
    Next i
objCAcroPDDocDestination.Save 1, strSaveAs 'Save it as a new name
Set objCAcroPDDocSource = Nothing
Set objCAcroPDDocDestination = Nothing

If iFailed <> 0 Then
    MergePDFs = False
End If
On Error GoTo 0
End Function

N.B. Add Adobe Acrobat X.0 Type Library from Reference.

  • Thanks. This gets me started. Are there anyway to call these VBA methods from command line? – AngryHacker Apr 19 '18 at 18:15
  • " Add Adobe Acrobat X.0 Type Library from Reference." - does it work also if OP doesn't have Acrobat installed? – Máté Juhász Apr 20 '18 at 9:42
  • @MátéJuhász, If you want to use the Adobe Acrobat type library, you need to have Adobe Acrobat installed. :-) – Rajesh S Apr 20 '18 at 11:10
  • 1
    If you have Acrobat already, you can use its built in functionality to combine multiple, various type files into pdf, don't need to have a macro for that. – Máté Juhász Apr 20 '18 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.