views:

385

answers:

2

I am trying to use some Word 2007 automation where we give the user the ability to select a printer to print to and compile a word document. We give the ability to print locally or on a network printer. Network printers are specified in the code by their fully qualified path (printername + port if there is one).

The problem is in Windows 2008 Terminal Server changing printers does not work when the default is a network printer. It works fine when the original default is a local printer.

We print the document by:

  1. Change the default printer to the one the user wants. (Accomplished by Application.ActivePrinter)
  2. Print the document.
  3. Change the default printer back to the original default.

When I set the default printer to a network printer (redirected printer) in Word 2007 it does not change the printer, however in word 2003 it does work, but that is not an option in my case. I have to get this working with Word 2007.

Is there a better solution or is there something I'm doing particularly wrong?

The sample code is below, I've tried to debug it by using breakpoints on ActivePrinter and strPrinterName changes.

References for the Sample Code:

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

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

Sample Code:

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
A: 

Could it be security? Does the user has security permission set correctly to access the network printer?

Fadrian Sudaman
I am running the code as Administrator on a Windows 2008 box. I am able to connect to the printers fine when I manually perform the steps in Word 2003 and 2007.
Jeremy Edwards
Can I suggest that you record a macro in Word and perform the step of setting active printer. Look at the macro and see what Word does before and after setting the ActivePrinter if any, also pay special notice on the printer name. Debug your C++ code and check whether or not your GetNetworkPrinterName function returns the exact same string as what word does.
Fadrian Sudaman
A: 

We ended up fixing this problem outside of Word. We now use the Win32 API directly and then call word to do the printing only.

Jeremy Edwards