Wednesday 28 April 2010

Switching Default Printer in Desktop & Citrix Environment using VBA

Here is code snippet which quite challenged me for a while and found a solution lately for the defualt switch of printer within the desktop and citrix environment and would like to share with all of you.

'*Note - this code work semalessly with the Citrix and
'the native desktop environement.
'**********************************************
'Declaration Required:
'this is required for the following functions to operate 
'normally, and it should be declared at the top
'of the module above all the declared functions/subs in the 'module.


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



'SetPDFPrinter - This function returns the active defualt
'printer name as string and sets the required printer 
'specified in the code.
'Return the active defualt printer

Private Function SetPDFPrinter() As String
    'Save the old defualt printer and return as string
    Const BUFFSIZE As Long = 254
    Dim strBuffer As String * BUFFSIZE
    Dim lngRetVal As Long
    lngRetVal = GetProfileString("windows", "device", ",,,",    strBuffer, BUFFSIZE)
    SetPDFPrinter = Left(strBuffer, (InStr(strBuffer, ",") - 1))
    'Reset the printer if its not already selected 
    'to \\Prism\Apple Driver
    If SetPDFPrinter <> "\\Prism\Apple Color LW 12/660 PS" Then
        Dim objNetwork As Object
        Set objNetwork = CreateObject("Wscript.Network")
        objNetwork.SetDefaultPrinter "\\Prism\Apple Color LW 12/660 PS"
        Set objNetwork = Nothing
    End If
End Function


'ReSetOrgPrinter - This function is used to rest the active
'default printer to the named sent in the passed string to
'the function

Private Function ReSetOrgPrinter(printDrv As String)
'Retrive the current print driver string name 
'for comparision.
    Const BUFFSIZE As Long = 254
    Dim strBuffer As String * BUFFSIZE
    Dim lngRetVal As Long
    Dim actPrinter As String
    lngRetVal = GetProfileString("windows", "device", ",,,", strBuffer, BUFFSIZE)
    actPrinter = Left(strBuffer, (InStr(strBuffer, ",") - 1))
    If actPrinter <> printDrv Then
        Dim objNetwork As Object
        Set objNetwork = CreateObject("Wscript.Network")
        On Error Resume Next
        objNetwork.SetDefaultPrinter printDrv
        If Err.Number <> 0 Then
            MsgBox Err.Description & " :: " & Err.Number & vbCr & "The active/defualt printer would be: " & actPrinter, vbCritical
            On Error GoTo 0
            Err.Clear
        End If
        Set objNetwork = Nothing
    End If
End Function



'Usage in the actual procdeure.
Dim actDefualtPrinter As String  

'declare the string to hold  on the original default printer
actDefualtPrinter = SetPDFPrinter  

'Set the pdf printer to \\prism\apple 
'printer as default saving
'the old settings

'Reset back the defualt printer to the original settings.
ReSetOrgPrinter (actDefualtPrinter)





These procedures are primarily useful to switch the defualt printer seamlessly in native windows/citrix environment.

No comments: