【问题标题】:Excel VBA & VB6 PrinterExcel VBA & VB6 打印机
【发布时间】:2014-09-25 19:17:50
【问题描述】:

我有以下代码,这段代码是用 VB6 编写的,但我无法打开表单或检查任何参考资料。

Private Sub PopulatePrinterCombo(cmbDestination As ComboBox)
  Dim objPrinter  As Printer 'a printer in the Printers collection object

  'Add the printers to the combo box
  For Each objPrinter In printers
    cmbPrinter.AddItem objPrinter.DeviceName
  Next

  'Set current selection to the default printer
  cmbDestination.Text = Printer.DeviceName
End Sub

我目前正在将代码复制到 Excel VBA 宏中,问题是 Dim objPrinter As Printer 代码,我不断收到一条错误消息,上面写着“用户定义的类型未定义”,我是否需要添加 VBA 的引用才能能够获得将变量声明为“打印机”之类的选项?

我的第二个问题是我不完全理解For Each objPrinter In printers 行中的“打印机”,什么是“打印机”?谁能给我解释一下。

谢谢

第 2 部分

我现在正在尝试打印文件,我的代码如下:

  'Initialize values
  intDraftsPrinted = 0
  If objDraftPaths.Count > 1 Then

  Else
    intSelectedDraftCount = CountSelectedDrafts
  End If

  'prompt user to make sure
  intMsgBoxResponse = MsgBox("You selected " & intSelectedDraftCount & " part numbers. After removing duplicates" & vbNewLine & "there were " & objDraftPaths.Count & " unique draft files found." & vbNewLine & "Do you want to print these files?", vbYesNo, "TD Printer")

  If intMsgBoxResponse <> vbYes Then
    intSelectedDraftCount = 0 'So the following for loop will not entered
  Else
    intSelectedDraftCount = objDraftPaths.Count
  End If

  For i = 1 To intSelectedDraftCount

    booSuccess = False

    'open the draft file
    Set objDraftDocument = OpenSolidEdgeDraft(objDraftPaths.Item(i))
    If objDraftDocument Is Nothing Then
      'could not open file
      MsgBox "Could not open the following draft file:" & vbNewLine & _
             objDraftPaths.Item(i), vbExclamation, "Solid Edge Error"
    Else
      'Print the draft file
      For Each objSheet In objDraftDocument.Sheets
        strSheetSize = DetermineSheetSize(objSheet)
        If strSheetSize <> "" Then
          'Determine orientation
          If InStr(1, strSheetSize, "90") <> 0 Then
            'Print as landscape
            intOrientation = vbPRORLandscape

          Else
            'Print as portrait
            intOrientation = vbPRORPortrait
          End If
          'Specify Sheet Size
          Select Case Left(strSheetSize, 1)
            Case "A"
              intPaperSize = VBRUN.PrinterObjectConstants.vbPRPSLetter
            Case "B"
              intPaperSize = VBRUN.PrinterObjectConstants.vbPRPS11x17
            Case "C"
              intPaperSize = VBRUN.PrinterObjectConstants.vbPRPSCSheet
            Case "D"
              intPaperSize = VBRUN.PrinterObjectConstants.vbPRPSDSheet
            Case "E"
              intPaperSize = VBRUN.PrinterObjectConstants.vbPRPSESheet
            Case Else
              intPaperSize = VBRUN.PrinterObjectConstants.vbPRPSLetter
          End Select

          'Enable error handling
          On Error Resume Next

          'Activate the current sheet
          objSheet.Activate
          If Err Then
            'Could not activate sheet
            MsgBox "An error occurred while attempting to print: " & vbNewLine & objDraftPaths.Item(i) & vbNewLine & "The error was:" & vbNewLine & "Error Number: " & Err.Number & vbNewLine & "Error Description: " & Err.Description, vbExclamation, "Solid Edge Error"
            Err.Clear
          Else
            'Print to the printer specified by the combo box
            objDraftDocument.PrintOut cmbPrinter.Text, 1, intOrientation, intPaperSize, , , , igPrintSelected
            If Err Then
              'Could not print document
              MsgBox "An error occurred while attempting to print: " & vbNewLine & objDraftPaths.Item(i) & vbNewLine & "The error was:" & vbNewLine & "Error Number: " & Err.Number & vbNewLine & "Error Description: " & Err.Description, vbExclamation, "Solid Edge Error"
              Err.Clear
            Else
              booSuccess = True
            End If
          End If

          'Disable error handling
          On Error GoTo 0

        End If
      Next

      'Close the file
      objDraftDocument.Close False

      intDraftsPrinted = intDraftsPrinted + 1

    End If

  Next i

  'Dereference objects
  Set objSheet = Nothing
  Set objDraftDocument = Nothing
  'Set objDraftPaths = Nothing

  PrintSelectedDrafts = intDraftsPrinted

现在问题来了,当我点击以下行时:intOrientation = vbPRORLandscape 在 excel VBA 中,它不能识别“vbPRORLandscape”以及下一行“vbPRORPortrait”。有办法解决吗?

另外,我有一种感觉,VBRUN.PrinterObjectConstants.vbPRPSLetter 和这些行的其余部分可能无法正常工作。不过它适用于 VB6。

谢谢

【问题讨论】:

  • Google for : application.printers excel 看看其他人是如何处理这个问题的。 Excel 中不存在 Application.printers。没有等价物。大多数人都在使用 WinAPI 来获取打印机列表。

标签: vba excel vb6


【解决方案1】:

似乎Printers 集合在 MS Access VBA 环境中可用,但我不认为它是 Excel VBA 环境固有的。

我使用 Windows Script Host 的 WshNetwork 对象来列出可用的打印机。我使用下面的子例程来使用连接到系统的打印机列表填充 ComboBox。为了使此代码正常工作,您需要将“Windows 脚本宿主对象模型”引用添加到您的 VBA 项目中。 (菜单:工具>参考[从列表中选择])

我添加了 (j) 循环以按字母顺序排列列表。

Sub populatePrintersList()
    Dim nwo As New WshNetwork
    Dim i As Integer
    Dim j As Integer
    Dim bAdd As Boolean

    bAdd = True
    cmbPrinter.Clear
    For i = 0 To (nwo.EnumPrinterConnections.Count / 2) - 1
        For j = 0 To cmbPrinter.ListCount - 1
            If nwo.EnumPrinterConnections(i * 2 + 1) < cmbPrinter.List(j) Then
                cmbPrinter.AddItem nwo.EnumPrinterConnections(i * 2 + 1), j
                bAdd = False
                Exit For
            End If
        Next j
        If bAdd Then cmbPrinter.AddItem nwo.EnumPrinterConnections(i * 2 + 1): bAdd = True
    Next i

    cmbPrinter.ListIndex = 0
End Sub

第 2 部分:

MSDN 包含Worksheet.PrintOut 方法的参考资料:Worksheet.PrintOut

Worksheet.PageSetup 对象的方法和属性的详细文档也可以在 MSDN 上找到:Worksheet.PageSetup

我建议使用这些资源来寻找大量答案。

【讨论】:

  • 该代码运行良好,但现在我的下一步出现错误。您能否在问题中查看上面的“第 2 部分”。谢谢
猜你喜欢
  • 2020-03-19
  • 2015-02-16
  • 2012-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-13
相关资源
最近更新 更多