Link to home
Create AccountLog in
Avatar of Juan Velasquez
Juan VelasquezFlag for United States of America

asked on

Saving word document as pdf

Hello,

I am using the function below from within Excel to merge and save documents as word documents.  I'm now trying to figure out how to save these documents as pdf documents.  I am using Access 2003 with Word 2007 (that's how the environment is configured).  Word 2007 does have the pdf addin installed.  I've bolded the area where I thinkg the code should go
Private Function MwMergeExecute() As Boolean
 On Error GoTo Err_MwMergeExecute
              Dim strSQL          As String
              Dim sCopySuccess    As String
              Dim intPos          As Integer
              Dim strName         As String
              'if we're executing a new, temporary, or modified merge def,
              'then we've already prepared the data
              'otherwise prepare the data
     If gblnMergeDefNew = False _
                  And gblnMergeDefTemp = False _
                  And gblnMergeDefModify = False Then
             If MwGetData(gstrDataSource, gstrDataSourceCriteria) = False Then Exit Function
     End If
              'open the mail merge main document
     Set mobjword = CreateObject("Word.Application")
     Set mobjWordMainDoc = mobjword.Documents.Open(FileName:=gstrMainDocName, _
                                                            ReadOnly:=True)
              'this is here for debugging
          '    mobjWord.Visible = True
              'set the active printer
     If gblnSendToPrinter = True Then
         If MwIsValidPrinter(gstrSendToPrinterName, True) _
                      And (gstrSendToPrinterName <> "(Default Printer)" _
                      And gstrSendToPrinterName <> "(Pitney Bowes)") _
                      Then mobjword.ActivePrinter = gstrSendToPrinterName
     End If
              'set the main doc properties
     With mobjWordMainDoc.MailMerge
                  '******************************************************************************
                  'TO DO: Must redo how main doc type is set, wizard must decide for the user now
    .MainDocumentType = gbytMainDocType
                  '******************************************************************************
    .SuppressBlankLines = True
    .OpenDataSource Name:=gstrDataSourceFileName, _
                                      LinkToSource:=True, _
                                      SQLStatement:="SELECT * FROM `tblMailMergeWizard_Data`"
                  'merge to one document per record
         If gblnSendToSeparateDocs = True Then
             If MwMergeToSeperateDocs = True Then MwMergeExecute = True
                      'the log is updated by the above proc so goto exit
             GoTo Exit_MwMergeExecute
         End If
                  'merge to a document
         If gblnSendToDoc = True Then
            .Destination = wdSendToNewDocument
            .Execute
             Set mobjWordSendToDoc = mobjword.ActiveDocument
                      'set the send to doc file name to user supplied name - TimeStamp.doc
             If Right$(gstrSendToDocName, 5) = ".docx" Then _
                          gstrSendToDocName = Left$(gstrSendToDocName, Len(gstrSendToDocName) - 5) _
                          & "-" & MwGetTimeStamp & ".docx"
                      'if user wants to edit then close main doc & show destination doc
             If gblnSendToDocAndEdit = True Then
                          'mobjWordMainDoc.Close wdSaveChanges
                 mobjWordMainDoc.Close SaveChanges:=wdDoNotSaveChanges
                 mobjWordSendToDoc.SaveAs (gstrSendToDocName)
                 mobjWordSendToDoc.Activate
                 mobjword.Visible = True
                 GoTo UpdateTheLog
             End If
                      'print it
             If gblnSendToPrinter = True And gstrSendToPrinterName <> "(Pitney Bowes)" Then
                 mobjWordSendToDoc.PrintOut Background:=False
             End If
                  'don't merge to a doc but print (merge to printer)
         ElseIf gblnSendToDoc = False And gblnSendToPrinter = True And gstrSendToPrinterName <> "(Pitney Bowes)" Then
            .Destination = wdSendToPrinter
            .Execute
         End If
         If gblnSendToEmail = True Then
            .MainDocumentType = wdEMail
            .Destination = wdSendToEmail
            .MailAddressFieldName = gstrSendToEmailAddressFieldName
            .MailSubject = gstrSendToEmailSubject
            .MailFormat = gbytSendToEmailFormat
            .MailAsAttachment = gblnSendToEmailAsAttachment
            .Execute
         End If
          '        If gblnSendToFax Then
          '            .MainDocumentType = wdFax
          '            .Destination = wdSendToFax
          '            .Execute
          '        End If
                  'save and close the destination doc
                  'mobjWordMainDoc.Close wdSaveChanges
         mobjWordMainDoc.Close SaveChanges:=wdDoNotSaveChanges
         If gstrSendToDocName <> "" Then mobjWordSendToDoc.SaveAs (gstrSendToDocName)

' THIS IS WHERE I NEED TO SAVE THE DOCUMENT AS A PDF FILE
         mobjWordSendToDoc.Close
     End With
UpdateTheLog:
     If gblnLogUpdate = True Then Call MwLogUpdate
     If gblnLogInsert = True Then Call MwLogInsert
     MwMergeExecute = True
Exit_MwMergeExecute:
     On Error Resume Next
              'close word if user isn't editing the doc
     If gblnSendToDocAndEdit = False Then mobjword.Quit SaveChanges:=wdDoNotSaveChanges
     Set mobjWordMainDoc = Nothing
     Set mobjWordSendToDoc = Nothing
     Set mobjword = Nothing
     Exit Function
Err_MwMergeExecute:
     If Err = 3262 Then
         MsgBox "The mail merge data source is already in use, it " _
                  & "may be in use by another mail merge document.  Please " _
                  & "insure that Microsoft Word doesn't already have an open " _
                  & "mail merge document and try this operation again.", vbOKOnly, gstrAppName
         MwMergeExecute = False
         Resume Exit_MwMergeExecute
     End If
     LogErrorToTable Err.Number, Err.Description, "MwMerge", "MwMergeExecute", Erl
     Resume Exit_MwMergeExecute
          End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of TommySzalapski
TommySzalapski
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Hammo777
Hammo777

You don't need the PDF addin.  PDF is a native format for word 2007:

Sub Convert_2_PDF()

ActiveDocument.ExportAsFixedFormat OutputFileName:= _

ActiveDocument.Path & "\" & ActiveDocument.Name & ".pdf", ExportFormat:= _

wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _

wdExportOptimizeForPrint, Range:=wdExportAllDocument, _

Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _

CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _

BitmapMissingFonts:=True, UseISO19005_1:=False


End Sub

Hammo777, please do not simply plagurize content. If you find code on the internet that you think would be useful, link to it or at least cite it as a reference.
http://vbadud.blogspot.com/2009/07/how-to-create-pdf-from-word-document.html

Note: The code I posted does basically the same thing. Most of those options are the default and are not needed explicitly.
Noted.
Welcome to EE, by the way, and good luck on your first 10,000 points.
Avatar of Juan Velasquez

ASKER

Thanks for the help on the syntax