0

I have an embedded MS-Word document in an Excel Worksheet which name is SalaryPaycheck.

The MS-word document contains several linked fields to Worksheet cells.

I have update the linked cells, several times and perform updating above fields.

Then I need perform exporting the embedded MS-Word document each time the fields have updated, as PDF.

So I need all exported files are appending in a single pdf file.

I using below code:

Sub PrintIt()

    Dim objWord As Word.Application
    Dim objDoc As Word.Document
    Dim i as Integer

    ActiveSheet.OLEObjects("SalaryPaycheck").Activate
    Set objWord = GetObject(, "Word.Application")
    objWord.Visible = False
    Set objDoc = objWord.ActiveDocument
    objWord.Application.DisplayAlerts = wdAlertsNone
    objWord.Application.ScreenUpdating = False

    For i = 1 to 10

        Range("Key").value = i

        objDoc.Fields.Update

        objDoc.ExportAsFixedFormat _
            outputfileName:=ThisWorkbook.path & "\Results\" & "rep" & i & ".pdf" _
            , exportformat:=wdExportFormatPDF _
            , openafterexport:=False _
            , optimizefor:=wdExportOptimizeForPrint _
            , Range:=wdExportAllDocument _
            , Item:=wdExportDocumentContent _
            , includedocprops:=False _
            , keepirm:=True _
            , createbookmarks:=wdExportCreateNoBookmarks _
            , docstructuretags:=True _
            , bitmapmissingfonts:=True _
            , useiso19005_1:=False
    Next i

    objWord.Quit
    Set objDoc = Nothing
    Set objWord = Nothing

End Sub 'Print it

How can using objDoc.SaveAs2 or objDoc.ExportAsFixedFormat (shown above) same as objDoc.PrintOut' whith 'Append:=True argument?

Or How can using .PrintOut with 'Append:=True' argument in this case (PDF) which working quietly (using OutputFileName:=path & filename and PrintToFile:=True)

Tuberose
  • 434
  • 6
  • 24

1 Answers1

2

As said in the other question, just append the documents in word

Sub PrintIt()

Dim objWord As Word.Application
Dim objDocTotal As Word.Document
Dim objDoc As Word.Document
Dim i As Integer
Dim strOutfile As String
Dim rg As Word.Range

    ActiveSheet.OLEObjects("SalaryPaycheck").Activate
    Set objWord = GetObject(, "Word.Application")
    objWord.Visible = True
    Set objDoc = objWord.ActiveDocument
    Set objDocTotal = Documents.Add
    objWord.Application.DisplayAlerts = wdAlertsNone
    objWord.Application.ScreenUpdating = True

    For i = 1 To 10

        Range("Key").Value = i

        With objDoc
            .Fields.Update
            .Content.Copy
        End With

        Set rg = objDocTotal.Content
        With rg
            .Collapse Direction:=wdCollapseEnd
            If i > 1 Then .InsertBreak wdPageBreak
            .PasteAndFormat wdFormatOriginalFormatting
        End With
    Next i


    strOutfile = "<Path>\Salary.pdf"

    objDocTotal.ExportAsFixedFormat outputfileName:= _
                                    strOutfile, exportformat:=wdExportFormatPDF, _
                                    openafterexport:=False, optimizefor:=wdExportOptimizeForPrint, Range:= _
                                    wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent



    objDocTotal.Close False
    objWord.Quit
    Set objDoc = Nothing
    Set objWord = Nothing

End Sub
Storax
  • 11,158
  • 3
  • 16
  • 33
  • In real there is more loops (`For Next`) so running the above procedure made very slow but the CPU usage is approximately lower than 25%! (While above code with more `For Next` is running.) How can I running above faster with increase CPU usage? – Tuberose Jan 03 '18 at 14:22
  • I have asked the specific question [stackoverflow.com/q/](https://stackoverflow.com/q/48079894/9075944). – Tuberose Jan 03 '18 at 15:00
  • Please visit this question. ([stackoverflow.com/q/](https://stackoverflow.com/q/48095734/9075944)). As your experts, seems you can answer it. – Tuberose Jan 04 '18 at 16:14