2010-02-25 102 views
2

我想使用一些Word 2007自动化,我们让用户能够选择打印机打印并编译Word文档。我们可以在本地或网络打印机上进行打印。网络打印机在代码中通过完全合格的路径指定(如果有路径,则使用printername +端口)。当默认打印机是网络打印机时,Word 2007 Application.ActivePrinter没有设置

问题是在Windows 2008终端服务器更改打印机不起作用时,默认为网络打印机。当原始默认为本地打印机时,它工作正常。

  1. 更改默认打印机的用户想要的一个:

    我们通过打印文档。 (由Application.ActivePrinter完成)

  2. 打印文档。
  3. 将默认打印机更改回原始默认值。

当我设置默认打印机在Word 2007中的网络打印机(打印机重定向),它不会改变打印机,但是在Word 2003中它的工作,但是这是不是在我的情况选择。我必须得到这与Word 2007的工作。

有没有更好的解决方案,或者有什么我做的特别错误?

示例代码如下,我试图通过使用ActivePrinter和strPrinterName更改上的断点来调试它。

的参考示例代码:

http://www.erlandsendata.no/english/index.php?d=envbaprintchangeprinter

http://www.ozgrid.com/forum/showthread.php?t=68990

示例代码:

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 

回答

0

我们最终修复了Word以外的这个问题。我们现在直接使用Win32 API,然后调用word来执行打印。

1

难道是安全的?用户是否正确设置了安全权限才能访问网络打印机?

+0

我在Windows 2008盒子上以管理员身份运行代码。当我手动执行Word 2003和2007中的步骤时,我可以很好地连接到打印机。 – 2010-02-25 22:23:21

+0

我可以建议您在Word中录制一个宏,并执行设置活动打印机的步骤。查看宏并查看Word在设置ActivePrinter之前和之后如何执行,还要特别注意打印机名称。调试您的C++代码并检查您的GetNetworkPrinterName函数是否返回与单词相同的字符串。 – 2010-02-25 22:30:55

相关问题