在excel VBA中选择打印机

时间:2015-06-29 08:13:39

标签: excel vba printing

当尝试在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

我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分: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