【问题标题】:Selecting printer in excel VBA在excel VBA中选择打印机
【发布时间】:2015-06-29 08:13:39
【问题描述】:

当尝试在 excel 中的特定打印机上打印时,当服务器更改其提示时会给出错误消息。在尝试解决此问题时,我使用了“on error resume next”功能,但这很丑陋(并且并非一直有效)。

这是我目前的公式

    Application.ScreenUpdating = False
ActivePrinter = "\\w8vvmprint01\Moecombi04 op Ne01:"
On Error Resume Next
ActivePrinter = "\\w8vvmprint01\Moecombi04 op Ne02:"
On Error Resume Next
ActivePrinter = "\\w8vvmprint01\Moecombi04 op Ne03:"
On Error Resume Next
ActivePrinter = "\\w8vvmprint01\Moecombi04 op Ne04:"
On Error Resume Next
ActivePrinter = "\\w8vvmprint01\Moecombi04 op Ne05:"
On Error Resume Next
ActivePrinter = "\\w8vvmprint01\Moecombi04 op Ne06:"
On Error Resume Next
ActivePrinter = "\\w8vvmprint01\Moecombi04 op Ne07:"
On Error Resume Next
ActivePrinter = "\\w8vvmprint01\Moecombi04 op Ne08:"
On Error Resume Next

我能做些什么来解决这个问题?

【问题讨论】:

    标签: excel vba printing


    【解决方案1】:
    #If VBA7 Then
    Private Declare PtrSafe Function GetProfileString Lib "kernel32.dll" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
    #Else
    Private Declare Function GetProfileString Lib "kernel32.dll" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
    #End If
    
    Public Function GetPrinterPort(ByVal PrinterName As String) As String
      Dim t As Long
    
      Do
        GetPrinterPort = String$(Len(GetPrinterPort) + 256, 0)
        t = GetProfileString("PrinterPorts", PrinterName, "", GetPrinterPort, Len(GetPrinterPort))
      Loop Until t < Len(GetPrinterPort) - 1
    
      If t <= 0 Then Err.Raise 5, , "Cannot get printer port for " & PrinterName
    
      GetPrinterPort = Split(Left$(GetPrinterPort, t), ",")(1)
    End Function
    

    用法:

    port = GetPrinterPort("\\w8vvmprint01\Moecombi04")
    

    然后使用port 构建完整的打印机名称。你可能想尊重localization issues

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-19
      • 1970-01-01
      • 2010-12-10
      • 2022-12-21
      • 2017-03-17
      • 2015-02-16
      相关资源
      最近更新 更多