0

I want to print a macro-enabled MS-Word Document that is embedded in a Excel Worksheet which it's name is SalaryPaycheck, from the excel macro module.

I using code below:

Sub PrintIt()

    Dim objWord As Word.Application
    Dim objDoc As Word.Document
    Dim Oshp As Object
    Dim strCurrentPrinter As String

    ActiveSheet.OLEObjects("SalaryPaycheck").Activate
    Set objWord = GetObject(, "Word.Application")
    objWord.Visible = False
    Set objDoc = objWord.ActiveDocument
    objWord.Application.DisplayAlerts = wdAlertsNone
    objDoc.Application.ActivePrinter = "Adobe PDF on Ne06:"


    objDoc.PrintOut Background:=False


    objWord.Quit
    Set objDoc = Nothing
    Set objWord = Nothing
    Application.ScreenUpdating = True
    Exit Sub

End Sub 'Print it

The PrintOut opens a dialog box which asks about path and file name.

I want to made file name and path predefined so PrintOut runs quietly.

Tuberose
  • 434
  • 6
  • 24
  • 1
    Why not save as a PDF document instead of printing? – Storax Jan 02 '18 at 09:57
  • Because the `PrintOut` procedure has the `Append` method and I need appending several documents in a single pdf file. – Tuberose Jan 02 '18 at 10:26
  • That is a different question then. But you could join the several documents into a single word file first and then export it as a PDF file – Storax Jan 02 '18 at 10:31
  • The question asked [stackoverflow.com/q/](https://stackoverflow.com/q/48059723/9075944). – Tuberose Jan 02 '18 at 11:00

1 Answers1

1

If you have an up-to-date Word version then you could export/save the file to a PDF document directly. Change your code to

Sub PrintIt()

    Dim objWord As Word.Application
    Dim objDoc As Word.Document
    Dim Oshp As Object
    Dim strCurrentPrinter As String

    ActiveSheet.OLEObjects("SalaryPaycheck").Activate
    Set objWord = GetObject(, "Word.Application")
    objWord.Visible = False
    Set objDoc = objWord.ActiveDocument
    objWord.Application.DisplayAlerts = wdAlertsNone
 '   objDoc.Application.ActivePrinter = "Adobe PDF on Ne06:"
 '   objDoc.PrintOut Background:=False

    Dim strOutFile As String
    strOutFile = "<filename>.pdf"

    objDoc.ExportAsFixedFormat OutputFileName:= _
        strOutFile, ExportFormat:=wdExportFormatPDF, _
        OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
        wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent



    objWord.Quit
    Set objDoc = Nothing
    Set objWord = Nothing
    Application.ScreenUpdating = True
    Exit Sub

End Sub
Storax
  • 11,158
  • 3
  • 16
  • 33