在UserForm中查找打印机会导致加载时间更长

时间:2019-03-21 19:54:55

标签: excel vba

我创建了一个用户窗体,以选择要打印的多张纸并选择要从哪台打印机进行打印。下图显示了表单,其中左侧列表框为ListBox1,右侧列表框为ListBox2。

enter image description here

与此用户表格相关联,我具有以下子功能:

Dim a As Boolean

Private Sub CommandButton1_Click()
'Print Button

Dim msg As String, msg2 As String
Dim i As Integer

For X = 1 To ListBox1.ListCount - 1 'Starts at 1 instead of 0 to exclude "Select All"
    If ListBox1.Selected(X) = True Then
       msg = msg & ListBox1.List(X) & vbCrLf 'List of Selected sheets for message box
    End If
Next X

msg2 = ListBox2.Value

Response = MsgBox("The following items will be printed with " & msg2 & vbCrLf & vbCrLf & msg, vbOKCancel, "Press OK to Print")

If Response = vbOK Then
'    Application.DisplayAlerts = False
    Application.Visible = 1
        For i = 1 To ListBox1.ListCount - 1
            If ListBox1.Selected(i) Then
                Application.ScreenUpdating = 0
                Sheets(ListBox1.List(i)).PrintOut ActivePrinter:=ListBox2.Value
            End If
        Next i
'    Application.DisplayAlerts = True
Unload Me
ElseIf Response = vbCancel Then

End If

End Sub

Private Sub CommandButton2_Click()
'Cancel Button

Unload Me

End Sub

Private Sub UserForm_Initialize()

  Dim myWorksheet As Worksheet
  With ListBox1
      .MultiSelect = fmMultiSelectMulti
      .ListStyle = fmListStyleOption
      .AddItem "Select All"
      For Each myWorksheet In Worksheets
          If myWorksheet.Visible = xlSheetVisible Then
              'add each visible worksheet to the listbox
              .AddItem myWorksheet.Name
          End If
      Next myWorksheet
  End With


Dim X As Long, Printers As Object, arrPrinters As Variant
With CreateObject("WScript.Network")
  For X = 1 To .EnumPrinterConnections.Count Step 2
    ListBox2.AddItem .EnumPrinterConnections(X)
  Next
End With

End Sub

Private Sub ListBox1_Change()
    If a = True Then Exit Sub
    With Me.ListBox1
        If .ListIndex = 0 Then
            'stop recursion
            a = True
            For i = 1 To .ListCount - 1
                .Selected(i) = .Selected(0)
            Next i
            a = False
        Else
            .Selected(0) = False
        End If
    End With
End Sub

我的工作簿有37页,我总共有10台打印机。有什么办法可以防止工作簿在打开之前(在后台搜索,以便在搜索过程中仍可将数据输入到工作簿中)搜索打印机,或者加载时间较长,这只是搜索所固有的打印机?

0 个答案:

没有答案