2016-11-03 7 views
1

In Word 2010 versuche ich ein Makro zu erstellen, das den aktuellen Drucker auf einen bestimmten Farbdrucker in unserem Netzwerk setzt, ohne diesen Drucker zum Systemstandarddrucker des Benutzers zu machen. Ich habe einen Code aus den Beispielen, die ich im Internet gefunden habe, zusammengehackt. Alles funktioniert, außer das SetColorPrinterEast Sub ändert den Systemstandarddrucker des Benutzers, den ich nicht möchte. Ich vermute, dass der DoNotSetAsSysDefault in diesem Sub nicht wie beabsichtigt funktioniert, aber ich weiß nicht, was ich dagegen tun soll. Weitere Informationen finden Sie in den Kommentaren im Code. Irgendwelche Gedanken werden sehr geschätzt. Danke im Voraus!!!Word 2010 VBA zur Druckerauswahl ohne Systemstandarddrucker zu ändern

'I found the code block below on the web. I don't understand it, but 
'it seems to work properly with the "SetDefaultPrinter" 
'Sub below to get the system default printer.  
Public Declare Function GetProfileString Lib "kernel32" _ 
     Alias "GetProfileStringA" _ 
     (ByVal lpAppName As String, _ 
     ByVal lpKeyName As String, _ 
     ByVal lpDefault As String, _ 
     ByVal lpReturnedString As String, _ 
     ByVal nSize As Long) As Long 

' This code successfully sets the document to print from 
' the system default printer. 
Public Sub SetDefaultPrinter() 

    Dim strReturn As String 
    Dim intReturn As Integer 
    strReturn = Space(255) 
    intReturn = GetProfileString("Windows", ByVal "device", "", _ 
    strReturn, Len(strReturn)) 
    If intReturn Then 
     strReturn = UCase(Left(strReturn, InStr(strReturn, ",") - 1)) 
    End If 

    With Dialogs(wdDialogFilePrintSetup) 
     .Printer = strReturn 
     .DoNotSetAsSysDefault = True 
     .Execute 
    End With 

End Sub 

' This code correctly sets the printer to a specific color printer 
' on our network. The problem is that it makes that printer 
' the user's system default printer. I would think that the 
' .DoNotSetAsSysDefault = True line would solve this problem 
' but still this sub changes the user's system default printer. 

Public Sub SetColorPrinterEast() 

    With Dialogs(wdDialogFilePrintSetup) 
     .Printer = "\\[*NETWORK PATH*]\Color Printer East" 
     .DoNotSetAsSysDefault = True 
     .Execute 
    End With 

End Sub 

Antwort

1

Ich hatte das gleiche Problem vor ein paar Jahren, bekam um mich von dem aktuellen Standard-Druck in einer Variablen zu speichern, um den Standarddruckers auf einem Wechsel ich brauche, Druck, dann den Druckerstandard zurück an Benutzer zu ändern ursprünglicher Standard

Dieses wurde entwickelt und für Word 2003 geschrieben, aber hat weiterhin in Word arbeiten 2010

Hier ist der spezifische Code, den ich verwendet:

'Define Printer to add and printer to delete 
Const PrintPath = "\\prn001l0003\Colour04" 
Const PrintDeletePath = "\\prn001l0003\Colour02" 


' Used to see what printers are set up on the user, and to set a new network printer 
Public 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 
Public Declare Function PtrToStr Lib "kernel32" Alias "lstrcpyA" (ByVal RetVal As String, ByVal Ptr As Long) As Long 
Public Declare Function StrLen Lib "kernel32" Alias "lstrlenA" (ByVal Ptr As Long) As Long 
Const PRINTER_ENUM_CONNECTIONS = &H4 
Const PRINTER_ENUM_LOCAL = &H2 



Public Sub PrintLetter(ByRef LetterBrochures() As String) 
'Print the document 

    Dim STDprinter As String 

    On Error Resume Next 

    Call CheckPrinterLoaded  ' Get users loaded printers, remove any old printers used here, 
           ' and add printer I want to users printers 

    STDprinter = Application.ActivePrinter ' store the current default printer 
    Application.ActivePrinter = PrintPath ' change default printer to want I want 

    On Error GoTo printLetterError 

    Application.DisplayAlerts = wdAlertsNone ' prevent Word showing any alert/warnings etc 

    With ActiveDocument  ' first page is letterhead from tray 2, all others from tray 1, print 
     .PageSetup.FirstPageTray = 3 ' 3 = Tray 2 on MFLaser 
     .PageSetup.OtherPagesTray = 1 ' 1 = Tray 1 on MFLaser 
     .PrintOut Background:=False 
    End With 
    Application.DisplayAlerts = wdAlertsAll  ' enable Word alets/warning etc 
    Application.ActivePrinter = STDprinter 'change back users default printer 

    Exit Sub 
printLetterError: 
    MsgBox "Error printing letter" & vbCrLf & Err.Number & vbCrLf & Err.Description, vbCritical, "Error" 
    ActiveDocument.Close False 
    End 
End Sub 


Public Function CheckPrinterLoaded() 
'get users printers 
'look for and delete defined printer, PrintDeletePath 
'add printer I want to users printers, PrintPath 

    Dim StrPrinters As Variant, x As Long 
    Dim StrSetPrinter As String 
    Dim objNetwork 
    Set objNetwork = CreateObject("WScript.Network") 

    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) 
      If StrPrinters(x) = PrintDeletePath Then 
       objNetwork.RemovePrinterConnection PrintDeletePath 
      End If 
     Next x 
     objNetwork.AddWindowsPrinterConnection PrintPath 
    Else 
     MsgBox "No printers found" 
    End If 

End Function 


Private 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 


Private 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 
Verwandte Themen