Jump to content

Word Printout Function


Recommended Posts

Modified the original _WordDocPrint and called it WordDocPrint2(). Notice the last two parameters and line 65 is modified version of line 64. There is no error checking here so you have to get the call right, I haven't tested it, but you can!

Specify a 1 for $f_PrintToFile and for $s_OutputFileName I think it should be like "C:\temp\outputfilename.txt" or whatever you want to call it.

Func _WordDocPrint2(ByRef $o_object, $f_Background = 0, $i_Copies = 1, $i_Orientation = -1, $f_Collate = 1, $s_Printer = "", $i_Range = 0, $i_From = "", $i_To = "", $s_Pages = "", $i_PageType = 0, $i_Item = 0, $f_PrintToFile = 0, $s_OutputFileName = "")
    If Not IsObj($o_object) Then
        __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidDataType")
        Return SetError($_WordStatus_InvalidDataType, 1, 0)
    EndIf
    ;
    If Not __WordIsObjType($o_object, "document") Then
        __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidObjectType")
        Return SetError($_WordStatus_InvalidObjectType, 1, 0)
    EndIf
    ;
    Local $s_ActivePrinter, $i_Extended, $i_DocOrientation = "", $i_ErrorStatusCode = $_WordStatus_Success, $s_ErrorMSG = ""

    Switch $i_Range
        Case 3
            If Not $i_From Or Not $i_To Then
                __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidValue", _
                        "When $i_Range is set to 3, then you must specify $i_From and $i_To.")
                Return SetError($_WordStatus_InvalidValue, 7, 0)
            EndIf
        Case 4
            If Not $s_Pages Then
                __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidValue", _
                        "When $i_Range is set to 4, you must specify $s_Pages.")
                Return SetError($_WordStatus_InvalidValue, 7, 0)
            EndIf
    EndSwitch

    $i_Orientation = String($i_Orientation)
    If $i_Orientation <> "-1" Then
        Switch $i_Orientation
            Case "0", "1"
                $i_DocOrientation = String($o_object.PageSetup.Orientation)
                If $i_DocOrientation <> $i_Orientation Then
                    $o_object.PageSetup.Orientation = $i_Orientation
                EndIf
            Case Else
                __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidValue")
                Return SetError($_WordStatus_InvalidValue, 4, 0)
        EndSwitch
    EndIf

    ; Setup internal error handler to Trap COM errors, turn off error notification
    Local $status = __WordInternalErrorHandlerRegister()
    If Not $status Then __WordErrorNotify("Warning", "_WordDocPrint", _
            "Cannot register internal error handler, cannot trap COM errors", _
            "Use _WordErrorHandlerRegister() to register a user error handler")
    Local $f_NotifyStatus = _WordErrorNotify() ; save current error notify status
    _WordErrorNotify(False)

    If $s_Printer Then
        $s_ActivePrinter = $o_object.Application.ActivePrinter
        $o_object.Application.ActivePrinter = $s_Printer
        If @error = $_WordStatus_ComError And $WordComErrorNumber = -2147352567 And $WordComErrorDescription = "There is a printer error." Then
            $i_ErrorStatusCode = $_WordStatus_InvalidValue
            $s_ErrorMSG = "Invalid printer name specified."
            $i_Extended = 6
        EndIf
    EndIf

    $i_From = String($i_From)
    $i_To = String($i_To)
    If Not $i_ErrorStatusCode Then
;~      $o_object.PrintOut ($f_Background, 0, $i_Range, "", $i_From, $i_To, $i_Item, $i_Copies, $s_Pages, $i_PageType, 0, $f_Collate)
        $o_object.PrintOut ($f_Background, 0, $i_Range, $s_OutputFileName, $i_From, $i_To, $i_Item, $i_Copies, $s_Pages, $i_PageType, $f_PrintToFile, $f_Collate)
        If @error = $_WordStatus_ComError Then
            $i_ErrorStatusCode = $_WordStatus_ComError
        EndIf
    EndIf

    If $i_DocOrientation <> "" And $i_DocOrientation <> $i_Orientation Then
        $o_object.PageSetup.Orientation = $i_DocOrientation
    EndIf
    If $s_ActivePrinter Then
        $o_object.Application.ActivePrinter = $s_ActivePrinter
    EndIf

    ; restore error notify and error handler status
    _WordErrorNotify($f_NotifyStatus) ; restore notification status
    __WordInternalErrorHandlerDeRegister()

    Switch $i_ErrorStatusCode
        Case $_WordStatus_Success
            Return SetError($_WordStatus_Success, 0, 1)
        Case $_WordStatus_InvalidValue
            __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidValue", $s_ErrorMSG)
            Return SetError($_WordStatus_InvalidValue, $i_Extended, 0)
        Case $_WordStatus_ComError
            __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_ComError", "There was an error while executing the 'PrintOut' Method.")
            Return SetError($_WordStatus_ComError, 0, 0)
        Case Else
            __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_GeneralError", "Invalid Error Status - Notify Word.au3 developer")
            Return SetError($_WordStatus_GeneralError, 0, 0)
    EndSwitch
EndFunc   ;==>_WordDocPrint
Link to comment
Share on other sites

Modified the original _WordDocPrint and called it WordDocPrint2(). Notice the last two parameters and line 65 is modified version of line 64. There is no error checking here so you have to get the call right, I haven't tested it, but you can!

Specify a 1 for $f_PrintToFile and for $s_OutputFileName I think it should be like "C:\temp\outputfilename.txt" or whatever you want to call it.

Func _WordDocPrint2(ByRef $o_object, $f_Background = 0, $i_Copies = 1, $i_Orientation = -1, $f_Collate = 1, $s_Printer = "", $i_Range = 0, $i_From = "", $i_To = "", $s_Pages = "", $i_PageType = 0, $i_Item = 0, $f_PrintToFile = 0, $s_OutputFileName = "")
    If Not IsObj($o_object) Then
        __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidDataType")
        Return SetError($_WordStatus_InvalidDataType, 1, 0)
    EndIf
    ;
    If Not __WordIsObjType($o_object, "document") Then
        __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidObjectType")
        Return SetError($_WordStatus_InvalidObjectType, 1, 0)
    EndIf
    ;
    Local $s_ActivePrinter, $i_Extended, $i_DocOrientation = "", $i_ErrorStatusCode = $_WordStatus_Success, $s_ErrorMSG = ""

    Switch $i_Range
        Case 3
            If Not $i_From Or Not $i_To Then
                __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidValue", _
                        "When $i_Range is set to 3, then you must specify $i_From and $i_To.")
                Return SetError($_WordStatus_InvalidValue, 7, 0)
            EndIf
        Case 4
            If Not $s_Pages Then
                __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidValue", _
                        "When $i_Range is set to 4, you must specify $s_Pages.")
                Return SetError($_WordStatus_InvalidValue, 7, 0)
            EndIf
    EndSwitch

    $i_Orientation = String($i_Orientation)
    If $i_Orientation <> "-1" Then
        Switch $i_Orientation
            Case "0", "1"
                $i_DocOrientation = String($o_object.PageSetup.Orientation)
                If $i_DocOrientation <> $i_Orientation Then
                    $o_object.PageSetup.Orientation = $i_Orientation
                EndIf
            Case Else
                __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidValue")
                Return SetError($_WordStatus_InvalidValue, 4, 0)
        EndSwitch
    EndIf

    ; Setup internal error handler to Trap COM errors, turn off error notification
    Local $status = __WordInternalErrorHandlerRegister()
    If Not $status Then __WordErrorNotify("Warning", "_WordDocPrint", _
            "Cannot register internal error handler, cannot trap COM errors", _
            "Use _WordErrorHandlerRegister() to register a user error handler")
    Local $f_NotifyStatus = _WordErrorNotify() ; save current error notify status
    _WordErrorNotify(False)

    If $s_Printer Then
        $s_ActivePrinter = $o_object.Application.ActivePrinter
        $o_object.Application.ActivePrinter = $s_Printer
        If @error = $_WordStatus_ComError And $WordComErrorNumber = -2147352567 And $WordComErrorDescription = "There is a printer error." Then
            $i_ErrorStatusCode = $_WordStatus_InvalidValue
            $s_ErrorMSG = "Invalid printer name specified."
            $i_Extended = 6
        EndIf
    EndIf

    $i_From = String($i_From)
    $i_To = String($i_To)
    If Not $i_ErrorStatusCode Then
;~      $o_object.PrintOut ($f_Background, 0, $i_Range, "", $i_From, $i_To, $i_Item, $i_Copies, $s_Pages, $i_PageType, 0, $f_Collate)
        $o_object.PrintOut ($f_Background, 0, $i_Range, $s_OutputFileName, $i_From, $i_To, $i_Item, $i_Copies, $s_Pages, $i_PageType, $f_PrintToFile, $f_Collate)
        If @error = $_WordStatus_ComError Then
            $i_ErrorStatusCode = $_WordStatus_ComError
        EndIf
    EndIf

    If $i_DocOrientation <> "" And $i_DocOrientation <> $i_Orientation Then
        $o_object.PageSetup.Orientation = $i_DocOrientation
    EndIf
    If $s_ActivePrinter Then
        $o_object.Application.ActivePrinter = $s_ActivePrinter
    EndIf

    ; restore error notify and error handler status
    _WordErrorNotify($f_NotifyStatus) ; restore notification status
    __WordInternalErrorHandlerDeRegister()

    Switch $i_ErrorStatusCode
        Case $_WordStatus_Success
            Return SetError($_WordStatus_Success, 0, 1)
        Case $_WordStatus_InvalidValue
            __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidValue", $s_ErrorMSG)
            Return SetError($_WordStatus_InvalidValue, $i_Extended, 0)
        Case $_WordStatus_ComError
            __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_ComError", "There was an error while executing the 'PrintOut' Method.")
            Return SetError($_WordStatus_ComError, 0, 0)
        Case Else
            __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_GeneralError", "Invalid Error Status - Notify Word.au3 developer")
            Return SetError($_WordStatus_GeneralError, 0, 0)
    EndSwitch
EndFunc   ;==>_WordDocPrint

Than you very much for your help! It works!

Here I shall put my work using your function.

Here is the potential of the PDFCreator COM .cConvertFile function call.

#include<Word.au3>

Const $maxTime = 30                 ; in seconds
Const $sleepTime = 250              ; in milliseconds
Const $PrinterName = "PDFCreator"   ; PDFCreator printer

$PDFCreator = ObjCreate("PDFCreator.clsPDFCreator")
$PDFCreator.cStart("/NoProcessingAtStartup")

With $PDFCreator
 $DefaultPrinter = .cDefaultprinter
 .cDefaultprinter = $PrinterName
EndWith

ConvertWordtoPDF(@ScriptDir &"\EvacuationGuide.doc")

Func ConvertWordtoPDF($sourceFilename)
    Local $Return
 Dim $objWord, $tempFile, $outputFolder, $outputFile

 $outputFolder = "D:\"
 $outputFile = $outputFolder &"Done" & ".pdf"
 $tempFile = "D:\$tmp"

 $oWordApp = _WordCreate ($sourceFilename, 0, 0)
 $oDoc = _WordDocGetCollection ($oWordApp, 0)
 _WordDocPrint2($oDoc, 0, 1, -1, 1, "", 0, "", "", "", 0, 0, 1, $tempFile)
 _WordQuit ($oWordApp, 0)
 
 $PDFCreator.cConvertFile($tempFile, $outputFile)

    Return $Return
EndFunc

 With $PDFCreator
 .cDefaultprinter = $DefaultPrinter
 Sleep(200)
 .cClose
EndWith

MsgBox (0, "", "Your PDF file is Ready")


Func _WordDocPrint2(ByRef $o_object, $f_Background = 0, $i_Copies = 1, $i_Orientation = -1, $f_Collate = 1, $s_Printer = "", $i_Range = 0, $i_From = "", $i_To = "", $s_Pages = "", $i_PageType = 0, $i_Item = 0, $f_PrintToFile = 0, $s_OutputFileName = "")
    If Not IsObj($o_object) Then
        __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidDataType")
        Return SetError($_WordStatus_InvalidDataType, 1, 0)
    EndIf
    ;
    If Not __WordIsObjType($o_object, "document") Then
        __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidObjectType")
        Return SetError($_WordStatus_InvalidObjectType, 1, 0)
    EndIf
    ;
    Local $s_ActivePrinter, $i_Extended, $i_DocOrientation = "", $i_ErrorStatusCode = $_WordStatus_Success, $s_ErrorMSG = ""

    Switch $i_Range
        Case 3
            If Not $i_From Or Not $i_To Then
                __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidValue", _
                        "When $i_Range is set to 3, then you must specify $i_From and $i_To.")
                Return SetError($_WordStatus_InvalidValue, 7, 0)
            EndIf
        Case 4
            If Not $s_Pages Then
                __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidValue", _
                        "When $i_Range is set to 4, you must specify $s_Pages.")
                Return SetError($_WordStatus_InvalidValue, 7, 0)
            EndIf
    EndSwitch

    $i_Orientation = String($i_Orientation)
    If $i_Orientation <> "-1" Then
        Switch $i_Orientation
            Case "0", "1"
                $i_DocOrientation = String($o_object.PageSetup.Orientation)
                If $i_DocOrientation <> $i_Orientation Then
                    $o_object.PageSetup.Orientation = $i_Orientation
                EndIf
            Case Else
                __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidValue")
                Return SetError($_WordStatus_InvalidValue, 4, 0)
        EndSwitch
    EndIf

    ; Setup internal error handler to Trap COM errors, turn off error notification
    Local $status = __WordInternalErrorHandlerRegister()
    If Not $status Then __WordErrorNotify("Warning", "_WordDocPrint", _
            "Cannot register internal error handler, cannot trap COM errors", _
            "Use _WordErrorHandlerRegister() to register a user error handler")
    Local $f_NotifyStatus = _WordErrorNotify() ; save current error notify status
    _WordErrorNotify(False)

    If $s_Printer Then
        $s_ActivePrinter = $o_object.Application.ActivePrinter
        $o_object.Application.ActivePrinter = $s_Printer
        If @error = $_WordStatus_ComError And $WordComErrorNumber = -2147352567 And $WordComErrorDescription = "There is a printer error." Then
            $i_ErrorStatusCode = $_WordStatus_InvalidValue
            $s_ErrorMSG = "Invalid printer name specified."
            $i_Extended = 6
        EndIf
    EndIf

    $i_From = String($i_From)
    $i_To = String($i_To)
    If Not $i_ErrorStatusCode Then
;~      $o_object.PrintOut ($f_Background, 0, $i_Range, "", $i_From, $i_To, $i_Item, $i_Copies, $s_Pages, $i_PageType, 0, $f_Collate)
        $o_object.PrintOut ($f_Background, 0, $i_Range, $s_OutputFileName, $i_From, $i_To, $i_Item, $i_Copies, $s_Pages, $i_PageType, $f_PrintToFile, $f_Collate)
        If @error = $_WordStatus_ComError Then
            $i_ErrorStatusCode = $_WordStatus_ComError
        EndIf
    EndIf

    If $i_DocOrientation <> "" And $i_DocOrientation <> $i_Orientation Then
        $o_object.PageSetup.Orientation = $i_DocOrientation
    EndIf
    If $s_ActivePrinter Then
        $o_object.Application.ActivePrinter = $s_ActivePrinter
    EndIf

    ; restore error notify and error handler status
    _WordErrorNotify($f_NotifyStatus) ; restore notification status
    __WordInternalErrorHandlerDeRegister()

    Switch $i_ErrorStatusCode
        Case $_WordStatus_Success
            Return SetError($_WordStatus_Success, 0, 1)
        Case $_WordStatus_InvalidValue
            __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidValue", $s_ErrorMSG)
            Return SetError($_WordStatus_InvalidValue, $i_Extended, 0)
        Case $_WordStatus_ComError
            __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_ComError", "There was an error while executing the 'PrintOut' Method.")
            Return SetError($_WordStatus_ComError, 0, 0)
        Case Else
            __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_GeneralError", "Invalid Error Status - Notify Word.au3 developer")
            Return SetError($_WordStatus_GeneralError, 0, 0)
    EndSwitch
EndFunc   ;==>_WordDocPrint
Edited by realshyfox

Learn, learn and ... learn

Link to comment
Share on other sites

Than you very much for your help! It works!

Here I shall put my work using your function.

Thanks to "Bob Anthony" for the function! That's cool that it works though, I didn't expect it to work, not on the first try without further tweaks anyway.

Link to comment
Share on other sites

  • 2 weeks later...

Thank you for the ConvertWordtoPDFcode! Works beautifully!

Exactly what I was attempting (and battling.

Thank to MrMitchell who helped me with the WordPrint function :huh2:

As for me I´m glad that my example helped you in your work ;)

Learn, learn and ... learn

Link to comment
Share on other sites

  • 1 year later...

Is there a way to do this with a mail merge? I am trying to merge a document to a file and I have not found a good way to do it.

This is very similar to what I would like to do but I need it to work on a mail merge.

Right now I am using VBA with sendkeys to merge to a printer and then printing it to a file.

Any help would be appreciated.

StaticTank

Modified the original _WordDocPrint and called it WordDocPrint2(). Notice the last two parameters and line 65 is modified version of line 64. There is no error checking here so you have to get the call right, I haven't tested it, but you can!

Specify a 1 for $f_PrintToFile and for $s_OutputFileName I think it should be like "C:tempoutputfilename.txt" or whatever you want to call it.

Func _WordDocPrint2(ByRef $o_object, $f_Background = 0, $i_Copies = 1, $i_Orientation = -1, $f_Collate = 1, $s_Printer = "", $i_Range = 0, $i_From = "", $i_To = "", $s_Pages = "", $i_PageType = 0, $i_Item = 0, $f_PrintToFile = 0, $s_OutputFileName = "")
    If Not IsObj($o_object) Then
        __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidDataType")
        Return SetError($_WordStatus_InvalidDataType, 1, 0)
    EndIf
    ;
    If Not __WordIsObjType($o_object, "document") Then
        __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidObjectType")
        Return SetError($_WordStatus_InvalidObjectType, 1, 0)
    EndIf
    ;
    Local $s_ActivePrinter, $i_Extended, $i_DocOrientation = "", $i_ErrorStatusCode = $_WordStatus_Success, $s_ErrorMSG = ""

    Switch $i_Range
        Case 3
            If Not $i_From Or Not $i_To Then
                __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidValue", _
                        "When $i_Range is set to 3, then you must specify $i_From and $i_To.")
                Return SetError($_WordStatus_InvalidValue, 7, 0)
            EndIf
        Case 4
            If Not $s_Pages Then
                __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidValue", _
                        "When $i_Range is set to 4, you must specify $s_Pages.")
                Return SetError($_WordStatus_InvalidValue, 7, 0)
            EndIf
    EndSwitch

    $i_Orientation = String($i_Orientation)
    If $i_Orientation <> "-1" Then
        Switch $i_Orientation
            Case "0", "1"
                $i_DocOrientation = String($o_object.PageSetup.Orientation)
                If $i_DocOrientation <> $i_Orientation Then
                    $o_object.PageSetup.Orientation = $i_Orientation
                EndIf
            Case Else
                __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidValue")
                Return SetError($_WordStatus_InvalidValue, 4, 0)
        EndSwitch
    EndIf

    ; Setup internal error handler to Trap COM errors, turn off error notification
    Local $status = __WordInternalErrorHandlerRegister()
    If Not $status Then __WordErrorNotify("Warning", "_WordDocPrint", _
            "Cannot register internal error handler, cannot trap COM errors", _
            "Use _WordErrorHandlerRegister() to register a user error handler")
    Local $f_NotifyStatus = _WordErrorNotify() ; save current error notify status
    _WordErrorNotify(False)

    If $s_Printer Then
        $s_ActivePrinter = $o_object.Application.ActivePrinter
        $o_object.Application.ActivePrinter = $s_Printer
        If @error = $_WordStatus_ComError And $WordComErrorNumber = -2147352567 And $WordComErrorDescription = "There is a printer error." Then
            $i_ErrorStatusCode = $_WordStatus_InvalidValue
            $s_ErrorMSG = "Invalid printer name specified."
            $i_Extended = 6
        EndIf
    EndIf

    $i_From = String($i_From)
    $i_To = String($i_To)
    If Not $i_ErrorStatusCode Then
;~      $o_object.PrintOut ($f_Background, 0, $i_Range, "", $i_From, $i_To, $i_Item, $i_Copies, $s_Pages, $i_PageType, 0, $f_Collate)
        $o_object.PrintOut ($f_Background, 0, $i_Range, $s_OutputFileName, $i_From, $i_To, $i_Item, $i_Copies, $s_Pages, $i_PageType, $f_PrintToFile, $f_Collate)
        If @error = $_WordStatus_ComError Then
            $i_ErrorStatusCode = $_WordStatus_ComError
        EndIf
    EndIf

    If $i_DocOrientation <> "" And $i_DocOrientation <> $i_Orientation Then
        $o_object.PageSetup.Orientation = $i_DocOrientation
    EndIf
    If $s_ActivePrinter Then
        $o_object.Application.ActivePrinter = $s_ActivePrinter
    EndIf

    ; restore error notify and error handler status
    _WordErrorNotify($f_NotifyStatus) ; restore notification status
    __WordInternalErrorHandlerDeRegister()

    Switch $i_ErrorStatusCode
        Case $_WordStatus_Success
            Return SetError($_WordStatus_Success, 0, 1)
        Case $_WordStatus_InvalidValue
            __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_InvalidValue", $s_ErrorMSG)
            Return SetError($_WordStatus_InvalidValue, $i_Extended, 0)
        Case $_WordStatus_ComError
            __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_ComError", "There was an error while executing the 'PrintOut' Method.")
            Return SetError($_WordStatus_ComError, 0, 0)
        Case Else
            __WordErrorNotify("Error", "_WordDocPrint", "$_WordStatus_GeneralError", "Invalid Error Status - Notify Word.au3 developer")
            Return SetError($_WordStatus_GeneralError, 0, 0)
    EndSwitch
EndFunc   ;==>_WordDocPrint

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...