Help with .docx to pdf conversion

craigachan

Registered User.
Local time
Today, 05:44
Joined
Nov 9, 2007
Messages
285
I have been running code that will launch a Word Template, fill in the merged fields the rename the document and save with the new doc name. This works well. My current project requires that I now save the word doc as a pdf for use with NoteTaker on the iPad. I have been successful in adapting code that I have found to make the .docx to .pdf conversion running sections of the code one by one. But when I put the code together, I will get the .docx all of the time, but the .pdf is only produced some of the time. I've tried separating the code into subroutines and I've tried slowing the code down with msgboxes, but I can't seem to understand why my results are intermittant. Can someone check my code and advise me? Thank you very much.

Code:
Function LtrMergeWord(strdocname As String, _
                        strDataDir As String, _
                        Optional strOutDocName As String)

   ' This code takes a word document that has been setup as a MERGE document.
   ' This merge document is opened, then mailmerge is executed. The original
   ' document is then closed. The result is a raw word document with no connectons
   ' to the merge.txt (a csv source data file).
   
   'Parms:
   '        strDocName     - full path name of word doc (.doc)
   '        strDataDir     - dir (full path) where docuemnts and the merge.888 file is placed
   '        strOutDocName  - full path name of merged document (saved).
   '
   ' The above parms are suppled by other routines. You likey should not need to call this
   ' routine directly. See the sub called MergeNoPrompts.
   
   ' Albert D. Kallal      (c) 2001
   ' kalla@msn.com
   '
   
   Dim wordApp       As Object            ' running instance of word
   Dim wordDoc       As Object            ' one instance of a word doc
   Dim strActiveDoc  As String            ' doc name (no path)
   Dim lngWordDest   As Long              ' const for dest, 0 = new doc, 1 = printer
   Dim MyPbar        As New clsRidesPBar  ' create a instance of our Progress bar.
   
   
   MyPbar.ShowProgress
   MyPbar.TextMsg = "Launching Word...please wait..."
   MyPbar.Pmax = 4      ' 4 steps to inc
   MyPbar.IncOne        ' step 1....start!
   
   On Error GoTo CreateWordApp
   Set wordApp = GetObject(, "Word.Application")
   On Error Resume Next
   
   MyPbar.IncOne        ' step 2, word is loaded.
   
   Set wordDoc = wordApp.Documents.Open(strdocname)
    
   MyPbar.IncOne        ' step 3, doc is loaded
   
   strActiveDoc = wordApp.ActiveDocument.Name
   
   wordDoc.MailMerge.OpenDataSource _
        Name:=strDataDir & TextMergeRout, _
        ConfirmConversions:=False, _
        ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
        PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
        WritePasswordTemplate:="", Revert:=False, Format:=0, _
        Connection:="", SQLStatement:="", SQLStatement1:=""
   
   
   With wordDoc.MailMerge
      .Destination = 0        ' 0 = new doc
      .MailAsAttachment = False
      .MailAddressFieldName = ""
      .MailSubject = ""
      .SuppressBlankLines = True
      With .datasource
         .FirstRecord = 1
'            .LastRecord = 1
      End With
      .Execute Pause:=True
   End With
   MyPbar.IncOne  ' step 4, doc is merged
   wordDoc.Close (False)
   
   wordApp.Visible = True
   wordApp.Windows(wordApp.Windows.count).Activate
   If strOutDocName <> "" Then
      wordApp.ActiveDocument.SaveAs strOutDocName
   End If
   
   MyPbar.HideProgress
   
'   AppActivate "Microsoft Word"
   wordApp.Activate
   wordApp.WindowState = 2 '0  'wdWindowStateRestore  1 = maximize   2 = mimimize
    
    '===== Make PDF Doc for iPad
    Dim response
    Dim strwdocname As String
    
    response = MsgBox("Create .pdf for iPad?", vbYesNo)
    
    If response <> vbNo Then
        
        wordApp.Activate
        wordApp.WindowState = 0
        MsgBox "'" & ActiveDocument.Name & "'", vbOKOnly
        
        '===Convert2PDF - Create a PDF doc to use with iPad NoteTaker
    
        strdocname = Mid(ActiveDocument.Name, 1, (Len(ActiveDocument.Name) - 5))
        ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        ActiveDocument.Path & "\" & strwdocname & ".pdf", ExportFormat:= _
        wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
        wdExportOptimizeForPrint, Range:=wdExportAllDocument, _
        Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
        CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
        BitmapMissingFonts:=True, UseISO19005_1:=False
        'MsgBox ActiveDocument.Name
        
        '=========== Close Word
        'wordApp.Application.Quit
        'wordDoc.Close (True)
    Else
        wordApp.Activate
        wordApp.windostate = 0
    End If
    
   Set wordApp = Nothing
   Set wordDoc = Nothing
   'Set MyPbar = Nothing

    DoEvents

'   If bolShowMerge = True Then
'      WordApp.Dialogs(676).Show       'wdDialogMailMerge
'   End If
   
   Exit Function
   
CreateWordApp:
   ' this code is here to use the EXISTING copy of
   ' ms-access running. If getobject fails, then
   ' ms-word was NOT running. The below will then
   ' launch word
   Set wordApp = CreateObject("Word.Application")
   Resume Next
 
End Function
 
Last edited:

Users who are viewing this thread

Back
Top Bottom