Share via

Userform VBA Select Printer before printing

Anonymous
2019-03-13T12:00:53+00:00

I have created a userform and have become stuck on the final stage and was wondering if somebody could assist or point me in the right direction.

I have the user selecting checkboxes for the sheets they require to print, however, due to the printers being on a network I need them to select a printer so ideally would need a popup box or print preview.

Apologies in advance if the code is bad this is my first time using a userform and coding in this way.

Private Sub CommandButton1_Click()

'OK = MsgBox("Printing", vbExclamation + vbOKOnly, "Sucsess")

If Me.CheckBox1 = 0 And Me.CheckBox2 = 0 And Me.CheckBox3 = 0 And Me.CheckBox4 = 0 And Me.CheckBox5 = 0 And Me.CheckBox6 = 0 And Me.CheckBox7 = 0 And Me.CheckBox8 = 0 And Me.CheckBox9 = 0 And Me.CheckBox10 = 0 Then
        MsgBox "You need to select at least 1 form", vbCritical + vbOKOnly, "Error"
            Else
                If Me.CheckBox2 = 1 And Me.ComboBox3 = Null Then
                     MsgBox "Please select the number of witness statements required!", vbCritical + vbOKOnly, "Witness Staements"
                        Else
                            OK = MsgBox("Printing", vbExclamation + vbOKOnly, "Sucsess")

'Print the First Page everytime
ActiveDocument.ActiveWindow.PrintOut Range:=wdPrintRangeOfPages, Item:= _
wdPrintDocumentContent, Pages:="1" & p
'Inserting Pre info Code into the form
Dim Pg1Name As Range
    Set Pg1Name = ActiveDocument.Bookmarks("Pg1Name").Range
    Pg1Name.Text = Me.TextBox3.Value

Dim S1Name As Range
    Set S1Name = ActiveDocument.Bookmarks("S1Name").Range
    S1Name.Text = Me.TextBox3.Value

Dim S5Name As Range
    Set S5Name = ActiveDocument.Bookmarks("S5Name").Range
    S5Name.Text = Me.TextBox3.Value

Dim S6Name As Range
    Set S6Name = ActiveDocument.Bookmarks("S6Name").Range
    S6Name.Text = Me.TextBox3.Value

Dim Pg1Date As Range
    Set Pg1Date = ActiveDocument.Bookmarks("Pg1Date").Range
    Pg1Date.Text = Me.TextBox1.Value

Dim S1Date As Range
    Set S1Date = ActiveDocument.Bookmarks("S1Date").Range
    S1Date.Text = Me.TextBox1.Value

Dim Pg1Time As Range
    Set Pg1Time = ActiveDocument.Bookmarks("Pg1Time").Range
    Pg1Time.Text = Me.TextBox2.Value

Dim S1Time As Range
    Set S1Time = ActiveDocument.Bookmarks("S1Time").Range
    S1Time.Text = Me.TextBox2.Value

Dim Pg1Dept As Range
    Set Pg1Dept = ActiveDocument.Bookmarks("Pg1Dept").Range
    Pg1Dept.Text = Me.ComboBox2.Value

Dim S5Dept As Range
    Set S5Dept = ActiveDocument.Bookmarks("S5Dept").Range
    S5Dept.Text = Me.ComboBox2.Value

Dim S6Dept As Range
    Set S6Dept = ActiveDocument.Bookmarks("S6Dept").Range
    S6Dept.Text = Me.ComboBox2.Value

Dim Pg1Site As Range
    Set Pg1Site = ActiveDocument.Bookmarks("Pg1Site").Range
    Pg1Site.Text = Me.ComboBox1.Value

'Print Section 1 Immediate Response
If Accident_Incident_Form_Creator.CheckBox1.Value = True Then
    ActiveDocument.ActiveWindow.PrintOut Range:=wdPrintRangeOfPages, Item:=wdPrintDocumentContent, Pages:="2" & p
        End If

'Print Section 2 Witness Statement
If Accident_Incident_Form_Creator.CheckBox2.Value = True Then
    ActiveDocument.ActiveWindow.PrintOut Range:=wdPrintRangeOfPages, Item:=wdPrintDocumentContent, Copies:=Me.ComboBox3.Value, Pages:="3" & p
        End If

'Print Section 3 Information Gathering
If Accident_Incident_Form_Creator.CheckBox3.Value = True Then
    ActiveDocument.ActiveWindow.PrintOut Range:=wdPrintRangeOfPages, Item:=wdPrintDocumentContent, Pages:="4" & p
        End If

'Print Section 4 First Aid
If Accident_Incident_Form_Creator.CheckBox4.Value = True Then
    ActiveDocument.ActiveWindow.PrintOut Range:=wdPrintRangeOfPages, Item:=wdPrintDocumentContent, Pages:="5" & p
        End If

'Print Section 5 Damage Report
If Accident_Incident_Form_Creator.CheckBox5.Value = True Then
    ActiveDocument.ActiveWindow.PrintOut Range:=wdPrintRangeOfPages, Item:=wdPrintDocumentContent, Pages:="6" & p
        End If

'Print Section 6 Environmental Report
If Accident_Incident_Form_Creator.CheckBox6.Value = True Then
    ActiveDocument.ActiveWindow.PrintOut Range:=wdPrintRangeOfPages, Item:=wdPrintDocumentContent, Pages:="7" & p
        End If

'Print Section 7 & 8 Manual Handling & MHE
If Accident_Incident_Form_Creator.CheckBox7.Value = True Then
    ActiveDocument.ActiveWindow.PrintOut Range:=wdPrintRangeOfPages, Item:=wdPrintDocumentContent, Pages:="8" & p
        End If

'Print Section 9 Root Cause Investigation
If Accident_Incident_Form_Creator.CheckBox8.Value = True Then
    ActiveDocument.ActiveWindow.PrintOut Range:=wdPrintRangeOfPages, Item:=wdPrintDocumentContent, Pages:="9" & p
        End If

'Print Section 10 Root Cause Corrective Actions
If Accident_Incident_Form_Creator.CheckBox9.Value = True Then
    ActiveDocument.ActiveWindow.PrintOut Range:=wdPrintRangeOfPages, Item:=wdPrintDocumentContent, Pages:="9" & p
        End If

'Print Section 11 Safety Signoff
If Accident_Incident_Form_Creator.CheckBox10.Value = True Then
    ActiveDocument.ActiveWindow.PrintOut Range:=wdPrintRangeOfPages, Item:=wdPrintDocumentContent, Pages:="11" & p
        End If

If OK = 1 Then

'Close and don't save
    Application.DisplayAlerts = False

    ActiveDocument.Close
    ActiveWindow.Close

    Application.DisplayAlerts = True

End If
End If
End If

End Sub
Microsoft 365 and Office | Install, redeem, activate | For home | Other

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

1 answer

Sort by: Most helpful
  1. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2019-03-13T23:49:20+00:00

    See https://wordmvp.com/FAQs/MacrosVBA/AvailablePrinters.htm

    Using the information in that article, you could load a combobox on your form with a list of the printers.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments