簡體   English   中英

Excel VBA和VB6打印機

[英]Excel VBA & VB6 Printer

我有以下代碼,此代碼是用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代碼,我不斷收到一條錯誤消息,提示“ USER DEFINED TYPE NOT DEFINED”,我是否需要添加VBA上的引用才能獲取將變量聲明為“打印機”之類的選項?

我的第二個問題是我不完全了解“ For Each objPrinter In printers ”行中的“打印機”, 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

現在,當我在excel VBA中點擊以下行時出現問題: intOrientation = vbPRORLandscape ,它無法識別“ vbPRORLandscape”以及下一行“ vbPRORPortrait”。 有辦法解決嗎?

另外,我有一種感覺,即VBRUN.PrinterObjectConstants.vbPRPSLetter和其他這些行可能無法正常運行。 它在VB6中有效。

謝謝

看來Printers Collection在MS Access VBA環境中可用,但我認為它不是Excel VBA環境所固有的。

我使用Windows腳本宿主的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

在MSDN上也可以找到有關Worksheet.PageSetup對象的方法和屬性的深入文檔: Worksheet.PageSetup

我建議使用這些資源來找到大量答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM