Share via

Select a Printer in Word using VBA

Anonymous
2014-03-30T00:51:50+00:00

Hi,

I have  a Macro to update some DocVariables then for me to select which printer I wish to use from a list of 2.

I am using the procedure from an Excel Macro that I've set before, but it is not working in word.

My problem is that my Input Box is in a loop that I cannot leave unless I press CTRL+BREAK.

Please can you have a look at my Check Input section BOLD below...

Please note that I would like the input to be checked that it is a Number, and that the number is 1 or 2, and if Cancel is Clicked then Exit Macro.

Sub PrintCheckList()

    'PrintOut Time Change Check List

    'Settings

        'Variables

        Dim PlusMinus As String

        Dim ChangeHour As String

        Dim Default_Printer_Name As String

        Dim MyPrinter As String

        Dim Printer_Name As String

        Dim Printer_Name1 As String

        Dim Printer_Name2 As String

        Dim Printer_Name0 As Variant

        'Defaults

        Default_Printer_Name = Application.ActivePrinter

        Printer_Name1 = "Kodak ESP+7 on Ne01:"

        Printer_Name2 = "HP Photosmart B110a Series on Ne02:"

        PlusMinus = ""

        ChangeHour = ""

    'Set Change Details

    If Month(Now()) = "3" Then

        PlusMinus = "+"

        ChangeHour = "1:00am"

    Else

        PlusMinus = "-"

        ChangeHour = "2:00pm"

    End If

    'Update the Document

    With ActiveDocument

        .Variables("PlusMinus").Value = PlusMinus

        .Variables("ChangeHour").Value = ChangeHour

    End With

    'Update all Field Codes

    For Each FCRange In ActiveDocument.StoryRanges

        For Each FCField In FCRange.Fields

            FCField.Update

        Next FCField

    Next FCRange

Question:

    'Question to Set the Printer to Use

    Printer_Name0 = InputBox("Please select the Printer you wish to use..." + Chr(10) _

        + Chr(10) _

        + "     Please enter the Number for the Printer..." + Chr(10) _

        + Chr(10) _

        + "          1. " + Left(Printer_Name1, Len(Printer_Name1) - 9) + "." + Chr(10) _

        + "          2. " + Left(Printer_Name2, Len(Printer_Name2) - 9) + "." + Chr(10) _

        + Chr(10) _

        + "   The Current Printer is " + Chr(147) + Default_Printer_Name _

        + Chr(148) + "." + Chr(10) _

        + Chr(10), "Print Video Tickets...", 1)

    'Check Input

If Printer_Name0 = vbCancel Then

Exit Sub

ElseIf Not IsNumeric(Printer_Name0) Or Printer_Name0 < 1 Or _

Printer_Name0 > 2 Then

GoTo Question

End If

    'Goto Printer from your Selection

    If Printer_Name0 = "1" Then

        GoTo Printer1

    ElseIf Printer_Name0 = "2" Then

        GoTo Printer2

    End If

Printer1:

    Printer_Name = Printer_Name1

    Application.ActivePrinter = Printer_Name

    GoTo Print_Out

Printer2:

    Printer_Name = Printer_Name2

    Application.ActivePrinter = Printer_Name

Print_Out:

    ActiveDocument.ActiveWindow.PrintOut _

        Range:=wdPrintFromTo, _

        From:="1", _

        To:="1"

Cancel:

    Application.ActivePrinter = Default_Printer_Name

End Sub

Thank you in advance,

Neil

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Jay Freedman 207.7K Reputation points Volunteer Moderator
2014-03-30T01:54:54+00:00

The problem is in the test for Cancel,

If Printer_Name0 = vbCancel Then

Exit Sub

The InputBox function never returns vbCancel -- that's for MsgBox. Instead, the InputBox returns a zero-length string. There are various ways to test for that, but the best one is

If StrPtr(Printer_Name0) = 0 Then

Exit Sub


Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful