Userform VBA选择打印机,然后再打印

时间:2019-03-13 10:27:15

标签: vba ms-word userform

我已经创建了一个用户表单,并陷入了最后阶段,并且想知道是否有人可以帮助我或将我指向正确的方向。

我让用户为他们需要打印的纸张选择复选框,但是,由于打印机在网络上,我需要他们选择打印机,因此理想情况下需要一个弹出框或打印预览。

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

0 个答案:

没有答案