title: VBA save word document as pdf from excel
body: <p>I have seen lots of people asking this question, but I cannot get it to work.</p>
  
  <p>Background - I am creating a mail merge, from excel into a WORD template, which I then need to save as a PDF. My VBA code is in excel - and ideally needs to stay there. Mail merge is working, creating and saving the word documents as expected, but I cannot save as PDF.</p>
  
  <p>My code to create the mail merge:</p>
  
  <pre><code>'Declare Some Variable
  Dim FTR As String
  Dim cDir As String
  Dim r As Long
  Dim ThisFileName As String
  Dim bCreatedWordInstance As Boolean
  
  
  'Set Up WORD object
  Dim objWord As Object
  Dim objDoc As Object
  
  On Error Resume Next
  
      bCreatedWordInstance = False
      Set objWord = GetObject(, "Word.Application")
  
      If objWord Is Nothing Then
          Err.Clear
          Set objWord = CreateObject("Word.Application")
          bCreatedWordInstance = True
      End If
      If objWord Is Nothing Then
          MsgBox "Could not start Word"
          Err.Clear
          On Error GoTo 0
          Exit Sub
      End If
      objWord.Visible = True
  
      'Open Word Template
      Set objDoc = objWord.Documents.Open("C:\MailMergeTest\MAILMERGE TEMPLATE.dotx")
  
      objWord.Activate
  
  
  ' Let Word trap the errors
  On Error GoTo 0
  
  ' Set to True if you want to see the Word Doc flash past during construction
  objWord.Visible = False
  
  
  ' Setup filenames
  Const WTempName = "MAILMERGE TEMPLATE.dotx" '
  Dim NewFileName As String
  
  
  ' Setup directories
  cDir = ActiveWorkbook.Path + "\" 'Change if appropriate
  ThisFileName = ThisWorkbook.Name
  
  
  
  
  lastrow = Sheets("Sheet1").Range("A" &amp; Rows.Count).End(xlUp).Row
  'Merge the data
  With objDoc
  With .MailMerge
  .OpenDataSource Name:=cDir + ThisFileName, sqlstatement:="SELECT *  FROM `Sheet1$`"   ' Set this as required
  
  
  For r = 1 To lastrow
      If Cells(r, 12).Value = "FTR Generated Already" Then GoTo nextrow
      FTR = Sheets("Sheet1").Cells(r, 3).Value
  
          .Destination = wdSendToNewDocument
          .SuppressBlankLines = True
              With .DataSource
                  .FirstRecord = r - 1
                  .LastRecord = r - 1
                  .ActiveRecord = r - 1
              End With
          .Execute Pause:=False
  
      ' Save new file
      NewFileName = "MAILMERGE TEST - " &amp; FTR &amp; ".docx"
      NewFileNamePDF = "MAILMERGE TEST - " &amp; FTR &amp; ".pdf"
      objWord.ActiveDocument.SaveAs cDir + NewFileName
  
      ' ***** SAVE AS PDF CODE HERE
  
  
  
   GoTo nextrow
  
  
  0:
      Set objWord = Nothing
      Cells(r, 12).Value = "FTR Generated Already"
  
  nextrow:
  
          Next r
      End With
      End With
      ' Close the New Mail Merged Document
              If bCreatedWordInstance Then
                  objWord.Quit
              End If
  
      ' Close the Mail Merge Main Document
      objDoc.Close savechanges:=wdDoNotSaveChanges
      Set objDoc = Nothing
  </code></pre>
  
  <p>I have tried the following:</p>
  
  <pre><code>objDoc.ExportAsFixedFormat OutputFileName:=cDir &amp; NewFileNamePDF, ExportFormat:=wdExportFormatPDF
  </code></pre>
  
  <p>...this comes up with an error message saying 'Invalid procedure call or argument'.</p>
  
  <p>Also tried:</p>
  
  <pre><code>objWord.ActiveDocument.SaveAs2 cDir + NewFileNamePDF, FileFormat:=wdFormatPDF
  </code></pre>
  
  <p>.. which exports a file, but it cannot be opened as a PDF.</p>
  
  <p>Can anyone help out here?</p>
  
  <p>Thank you in advance for your advice</p>
  
  <p>Lee</p>