用户窗体VBA打印前选择打印机

用户窗体VBA打印前选择打印机,vba,ms-word,userform,Vba,Ms Word,Userform,我已经创建了一个userform,并且已经陷入了最后阶段,我想知道是否有人可以帮助我或者为我指明正确的方向 我让用户为他们需要打印的纸张选择复选框,但是,由于打印机在网络上,我需要他们选择打印机,因此理想情况下需要弹出框或打印预览 Private Sub CommandButton1_Click() 'OK = MsgBox("Printing", vbExclamation + vbOKOnly, "Sucsess") If Me.CheckBox1 = 0 And Me.CheckBox

我已经创建了一个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

代码的当前版本更好,但还不理想。特别是嵌套的
If
部分。如果连续缩进
的每个级别,以便于识别,则会很有帮助。一般的代码惯例是将所有
Dim
语句放在开头。这不是绝对必要的,也是个人喜好的问题,但我发现如果中间没有
Dim
行,阅读代码流会更容易。@Cindymister希望我已经理解了你的评论。我把“If”语句分为几个部分,每个部分都有一个缩进。我也采纳了关于两个问题的评论,删除了一个。这是正确的想法。。。我已经编辑了代码的顶部,以显示它是如何按照惯例完成的,从第一节开始。对于嵌套的If级别来说,非常重要的是
End If
与合作伙伴
If
出现在同一级别(就像我为
Else
设置的级别一样)。否则,很难识别哪些对属于一起,尤其是当代码很长时,这里就是这样。由于我们不必费心解决代码如何运行的问题,因此这不是一个大问题,但有一个好习惯:-)谢谢@Cindymister,这很有用。代码的当前版本更好,但还不理想。特别是嵌套的
If
部分。如果连续缩进
的每个级别,以便于识别,则会很有帮助。一般的代码惯例是将所有
Dim
语句放在开头。这不是绝对必要的,也是个人喜好的问题,但我发现如果中间没有
Dim
行,阅读代码流会更容易。@Cindymister希望我已经理解了你的评论。我把“If”语句分为几个部分,每个部分都有一个缩进。我也采纳了关于两个问题的评论,删除了一个。这是正确的想法。。。我已经编辑了代码的顶部,以显示它是如何按照惯例完成的,从第一节开始。对于嵌套的If级别来说,非常重要的是
End If
与合作伙伴
If
出现在同一级别(就像我为
Else
设置的级别一样)。否则,很难识别哪些对属于一起,尤其是当代码很长时,这里就是这样。既然我们不需要解决代码如何运行的问题,这不是一个大问题,但这是一个好习惯:-)谢谢@Cindymister,这很有用。