Printing 当默认打印机为网络打印机时,未设置Word 2007 Application.ActivePrinter

Printing 当默认打印机为网络打印机时,未设置Word 2007 Application.ActivePrinter,printing,ms-word,word-automation,Printing,Ms Word,Word Automation,我试图使用一些Word 2007自动化,让用户能够选择打印机打印和编译Word文档。我们可以在本地或网络打印机上打印。网络打印机在代码中由其完全限定路径(printername+端口,如果有)指定 问题在于Windows 2008终端服务器在默认为网络打印机时无法更改打印机。当原始默认设置为本地打印机时,它可以正常工作 我们通过以下方式打印文档: 将默认打印机更改为用户需要的打印机。(完成人) 打印文档 将默认打印机更改回原始默认打印机 当我在Word 2007中将默认打印机设置为网络打印机(重

我试图使用一些Word 2007自动化,让用户能够选择打印机打印和编译Word文档。我们可以在本地或网络打印机上打印。网络打印机在代码中由其完全限定路径(printername+端口,如果有)指定

问题在于Windows 2008终端服务器在默认为网络打印机时无法更改打印机。当原始默认设置为本地打印机时,它可以正常工作

我们通过以下方式打印文档:

  • 将默认打印机更改为用户需要的打印机。(完成人)
  • 打印文档
  • 将默认打印机更改回原始默认打印机
  • 当我在Word 2007中将默认打印机设置为网络打印机(重定向打印机)时,它不会更改打印机,但是在Word 2003中它确实可以工作,但在我的情况下,这不是一个选项。我必须用Word 2007来处理这个问题

    是否有更好的解决方案,或者是否有我特别做错的事情

    下面是示例代码,我尝试使用ActivePrinter上的断点和strpInterName更改来调试它

    示例代码的参考:

    Option Explicit
    
    Const PRINTER_ENUM_CONNECTIONS = &H4
    Const PRINTER_ENUM_LOCAL = &H2
    
    Private Declare Function EnumPrinters Lib "winspool.drv" Alias "EnumPrintersA" _
            (ByVal flags As Long, ByVal name As String, ByVal Level As Long, _
            pPrinterEnum As Long, ByVal cdBuf As Long, pcbNeeded As Long, _
            pcReturned As Long) As Long
    
    Private Declare Function PtrToStr Lib "kernel32" Alias "lstrcpyA" _
            (ByVal RetVal As String, ByVal Ptr As Long) As Long
    
    Private Declare Function StrLen Lib "kernel32" Alias "lstrlenA" _
           (ByVal Ptr As Long) As Long
    
    
    Public Function ListPrinters() As Variant
    
    Dim bSuccess As Boolean
    Dim iBufferRequired As Long
    Dim iBufferSize As Long
    Dim iBuffer() As Long
    Dim iEntries As Long
    Dim iIndex As Long
    Dim strPrinterName As String
    Dim iDummy As Long
    Dim iDriverBuffer() As Long
    Dim StrPrinters() As String
    
    iBufferSize = 3072
    
    ReDim iBuffer((iBufferSize \ 4) - 1) As Long
    
    'EnumPrinters will return a value False if the buffer is not big enough
    bSuccess = EnumPrinters(PRINTER_ENUM_CONNECTIONS Or _
            PRINTER_ENUM_LOCAL, vbNullString, _
            1, iBuffer(0), iBufferSize, iBufferRequired, iEntries)
    
    If Not bSuccess Then
        If iBufferRequired > iBufferSize Then
            iBufferSize = iBufferRequired
            Debug.Print "iBuffer too small. Trying again with "; _
            iBufferSize & " bytes."
            ReDim iBuffer(iBufferSize \ 4) As Long
        End If
        'Try again with new buffer
        bSuccess = EnumPrinters(PRINTER_ENUM_CONNECTIONS Or _
                PRINTER_ENUM_LOCAL, vbNullString, _
                1, iBuffer(0), iBufferSize, iBufferRequired, iEntries)
    End If
    
    If Not bSuccess Then
        'Enumprinters returned False
        MsgBox "Error enumerating printers."
        Exit Function
    Else
        'Enumprinters returned True, use found printers to fill the array
        ReDim StrPrinters(iEntries - 1)
        For iIndex = 0 To iEntries - 1
            'Get the printername
            strPrinterName = Space$(StrLen(iBuffer(iIndex * 4 + 2)))
            iDummy = PtrToStr(strPrinterName, iBuffer(iIndex * 4 + 2))
            StrPrinters(iIndex) = strPrinterName
        Next iIndex
    End If
    
    ListPrinters = StrPrinters
    
    End Function
    
    
    'You could call the function as follows:
    
    
    
    
    Sub Test()
    
    Dim StrPrinters As Variant, x As Long
    Dim strPrinterName As String
    
    StrPrinters = ListPrinters
    
    'Fist check whether the array is filled with anything, by calling another function, IsBounded.
    If IsBounded(StrPrinters) Then
        For x = LBound(StrPrinters) To UBound(StrPrinters)
            Debug.Print StrPrinters(x)
    
         ' Message out Printer name
            strPrinterName = StrPrinters(x)
    
          ' Message otu Active Printer
            Application.ActivePrinter = GetFullNetworkPrinterName(strPrinterName)
    
    
        Next x
    Else
        Debug.Print "No printers found"
    End If
    
    End Sub
    
    
    
    
    Public Function IsBounded(vArray As Variant) As Boolean
    
        'If the variant passed to this function is an array, the function will return True;
        'otherwise it will return False
        On Error Resume Next
        IsBounded = IsNumeric(UBound(vArray))
    
    End Function
    
    
    Function GetFullNetworkPrinterName(strNetworkPrinterName As String) As String
    ' returns the full network printer name
    ' returns an empty string if the printer is not found
    ' e.g. GetFullNetworkPrinterName("HP LaserJet 8100 Series PCL")
    ' might return "HP LaserJet 8100 Series PCL on Ne04:"
    Dim strCurrentPrinterName As String, strTempPrinterName As String, i As Long
        strCurrentPrinterName = Application.ActivePrinter
        i = 0
        Do While i < 100
            strTempPrinterName = strNetworkPrinterName & " on Ne" & Format(i, "00") & ":"
            On Error Resume Next ' try to change to the network printer
            Application.ActivePrinter = strTempPrinterName
            On Error GoTo 0
            If Application.ActivePrinter = strTempPrinterName Then
                ' the network printer was found
                GetFullNetworkPrinterName = strTempPrinterName
                i = 100 ' makes the loop end
            End If
            i = i + 1
        Loop
        ' remove the line below if you want the function to change the active printer
        'Application.ActivePrinter = strCurrentPrinterName ' change back to the original printer
    End Function
    

    示例代码:

    Option Explicit
    
    Const PRINTER_ENUM_CONNECTIONS = &H4
    Const PRINTER_ENUM_LOCAL = &H2
    
    Private Declare Function EnumPrinters Lib "winspool.drv" Alias "EnumPrintersA" _
            (ByVal flags As Long, ByVal name As String, ByVal Level As Long, _
            pPrinterEnum As Long, ByVal cdBuf As Long, pcbNeeded As Long, _
            pcReturned As Long) As Long
    
    Private Declare Function PtrToStr Lib "kernel32" Alias "lstrcpyA" _
            (ByVal RetVal As String, ByVal Ptr As Long) As Long
    
    Private Declare Function StrLen Lib "kernel32" Alias "lstrlenA" _
           (ByVal Ptr As Long) As Long
    
    
    Public Function ListPrinters() As Variant
    
    Dim bSuccess As Boolean
    Dim iBufferRequired As Long
    Dim iBufferSize As Long
    Dim iBuffer() As Long
    Dim iEntries As Long
    Dim iIndex As Long
    Dim strPrinterName As String
    Dim iDummy As Long
    Dim iDriverBuffer() As Long
    Dim StrPrinters() As String
    
    iBufferSize = 3072
    
    ReDim iBuffer((iBufferSize \ 4) - 1) As Long
    
    'EnumPrinters will return a value False if the buffer is not big enough
    bSuccess = EnumPrinters(PRINTER_ENUM_CONNECTIONS Or _
            PRINTER_ENUM_LOCAL, vbNullString, _
            1, iBuffer(0), iBufferSize, iBufferRequired, iEntries)
    
    If Not bSuccess Then
        If iBufferRequired > iBufferSize Then
            iBufferSize = iBufferRequired
            Debug.Print "iBuffer too small. Trying again with "; _
            iBufferSize & " bytes."
            ReDim iBuffer(iBufferSize \ 4) As Long
        End If
        'Try again with new buffer
        bSuccess = EnumPrinters(PRINTER_ENUM_CONNECTIONS Or _
                PRINTER_ENUM_LOCAL, vbNullString, _
                1, iBuffer(0), iBufferSize, iBufferRequired, iEntries)
    End If
    
    If Not bSuccess Then
        'Enumprinters returned False
        MsgBox "Error enumerating printers."
        Exit Function
    Else
        'Enumprinters returned True, use found printers to fill the array
        ReDim StrPrinters(iEntries - 1)
        For iIndex = 0 To iEntries - 1
            'Get the printername
            strPrinterName = Space$(StrLen(iBuffer(iIndex * 4 + 2)))
            iDummy = PtrToStr(strPrinterName, iBuffer(iIndex * 4 + 2))
            StrPrinters(iIndex) = strPrinterName
        Next iIndex
    End If
    
    ListPrinters = StrPrinters
    
    End Function
    
    
    'You could call the function as follows:
    
    
    
    
    Sub Test()
    
    Dim StrPrinters As Variant, x As Long
    Dim strPrinterName As String
    
    StrPrinters = ListPrinters
    
    'Fist check whether the array is filled with anything, by calling another function, IsBounded.
    If IsBounded(StrPrinters) Then
        For x = LBound(StrPrinters) To UBound(StrPrinters)
            Debug.Print StrPrinters(x)
    
         ' Message out Printer name
            strPrinterName = StrPrinters(x)
    
          ' Message otu Active Printer
            Application.ActivePrinter = GetFullNetworkPrinterName(strPrinterName)
    
    
        Next x
    Else
        Debug.Print "No printers found"
    End If
    
    End Sub
    
    
    
    
    Public Function IsBounded(vArray As Variant) As Boolean
    
        'If the variant passed to this function is an array, the function will return True;
        'otherwise it will return False
        On Error Resume Next
        IsBounded = IsNumeric(UBound(vArray))
    
    End Function
    
    
    Function GetFullNetworkPrinterName(strNetworkPrinterName As String) As String
    ' returns the full network printer name
    ' returns an empty string if the printer is not found
    ' e.g. GetFullNetworkPrinterName("HP LaserJet 8100 Series PCL")
    ' might return "HP LaserJet 8100 Series PCL on Ne04:"
    Dim strCurrentPrinterName As String, strTempPrinterName As String, i As Long
        strCurrentPrinterName = Application.ActivePrinter
        i = 0
        Do While i < 100
            strTempPrinterName = strNetworkPrinterName & " on Ne" & Format(i, "00") & ":"
            On Error Resume Next ' try to change to the network printer
            Application.ActivePrinter = strTempPrinterName
            On Error GoTo 0
            If Application.ActivePrinter = strTempPrinterName Then
                ' the network printer was found
                GetFullNetworkPrinterName = strTempPrinterName
                i = 100 ' makes the loop end
            End If
            i = i + 1
        Loop
        ' remove the line below if you want the function to change the active printer
        'Application.ActivePrinter = strCurrentPrinterName ' change back to the original printer
    End Function
    
    选项显式
    常量打印机\u枚举\u连接=&H4
    常量打印机\u枚举\u本地=&H2
    私有声明函数EnumPrinters Lib“winspool.drv”别名“EnumPrintersA”_
    (ByVal标志为长,ByVal名称为字符串,ByVal级别为长_
    ppinterenum为Long,ByVal cdBuf为Long,pcb为Long_
    (返回的时间)尽可能长
    私有声明函数PtrToStr Lib“kernel32”别名“lstrcpyA”_
    (ByVal RetVal作为字符串,ByVal Ptr作为长字符串)作为长字符串
    私有声明函数StrLen Lib“kernel32”别名“lstrlenA”_
    (ByVal Ptr As Long)尽可能长
    公共函数ListPrinters()作为变量
    Dim bsucces作为布尔值
    我需要的时间和你的一样长
    昏暗的咖啡色和咖啡色一样长
    Dim iBuffer()的长度与
    朦胧如长
    尺寸索引与长度相同
    Dim strpName作为字符串
    暗淡的我和你一样长
    Dim iDriverBuffer()的长度为
    Dim STRPINTS()作为字符串
    iBufferSize=3072
    ReDim iBuffer((iBufferSize\4)-1)的长度为
    '如果缓冲区不够大,EnumPrinters将返回值False
    bSuccess=枚举打印机(打印机\u枚举\u连接或_
    打印机\u枚举\u本地,vbNullString_
    1、iBuffer(0)、iBufferSize、iBufferRequired、iEntries)
    如果不成功,那么
    如果iBufferRequired>iBufferSize,则
    iBufferSize=需要IBuffer
    调试。打印“iBuffer太小,请重试”_
    iBufferSize&“字节”
    将iBuffer(iBufferSize\4)的长度
    如果结束
    '使用新缓冲区重试
    bSuccess=枚举打印机(打印机\u枚举\u连接或_
    打印机\u枚举\u本地,vbNullString_
    1、iBuffer(0)、iBufferSize、iBufferRequired、iEntries)
    如果结束
    如果不成功,那么
    '返回False
    MsgBox“枚举打印机时出错。”
    退出功能
    其他的
    'Enumprinters返回True,请使用找到的打印机填充数组
    ReDim Strprites(客户群-1)
    对于iIndex=0到iEntries-1
    “给我拿打印机
    strpInterName=Space$(StrLen(iBuffer(iIndex*4+2)))
    iDummy=PtrToStr(strpintername,iBuffer(iIndex*4+2))
    STRPINTS(iIndex)=STRPINTERNAME
    下一个指数
    如果结束
    ListPrinters=strPrints
    端函数
    '您可以按如下方式调用该函数:
    子测试()
    尺寸为变型,x为长型
    Dim strpName作为字符串
    strprints=列表打印机
    '首先通过调用另一个函数,检查数组是否填充了IsBounded。
    如果是有界的(strpints),那么
    对于x=LBound(strplants)到UBound(strplants)
    调试。打印strpints(x)
    '消息输出打印机名称
    strPrinterName=StrPrinters(x)
    '消息otu活动打印机
    Application.ActivePrinter=GetFullNetworkPrinterName(strpInterName)
    下一个x
    其他的
    调试。打印“未找到打印机”
    如果结束
    端接头
    公共函数作为布尔值有界(vArray作为变量)
    '如果传递给此函数的变量是数组,则函数将返回True;
    否则将返回False
    出错时继续下一步
    IsBounded=IsNumeric(UBound(vArray))
    端函数
    函数GetFullNetworkPrinterName(strNetworkPrinterName作为字符串)作为字符串
    '返回完整的网络打印机名称
    '如果找不到打印机,则返回空字符串
    例如GetFullNetworkPrinterName(“HP LaserJet 8100系列PCL”)
    '可能返回“Ne04上的HP LaserJet 8100系列PCL:”
    Dim strCurrentPrinterName作为字符串,STRTEMPPINTERNAME作为字符串,i作为长度
    strCurrentPrinterName=Application.ActivePrinter
    i=0
    当我<100时做
    strTempPrinterName=strNetworkPrinterName&“在Ne上”&格式(i,“00”)&”:
    出现错误时“继续下一步”尝试更改为网络打印机
    Application.ActivePrinter=strtemprintername
    错误转到0
    如果Application.ActivePrinter=strTempPrinterName,则
    '找到网络打印机
    GetFullNetworkPrinterName=strTempPrinterName
    i=100'表示循环结束
    如果结束
    i=i+1
    环
    '如果希望函数更改活动打印机,请删除下面的行
    “Application.ActivePrinter=strCurrentPrinterName”更改回原始打印机
    端函数
    
    可能是安全问题吗?用户是否正确设置了访问网络打印机的安全权限

    我们最终在Word之外解决了这个问题。我们现在直接使用Win32 API,然后调用word只进行打印。

    我在Windows 2008框中以管理员身份运行代码。手动执行Word 2003和2007中的步骤时,我可以很好地连接到打印机。我建议您在Word中录制宏并执行设置活动打印机的步骤。看