Results 1 to 5 of 5
  1. #1
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Chicago, Illinois, USA
    Posts
    185
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Reset printer to default using VBA (Word 2000)

    The situation: A user has printed something to a printer (I'll call it Print2) *other* than their default printer (Print1). Word retains the Print2 as the active printer until the user switches back.

    I need to create a button that will set the active printer back to the default printer. The default printer is not the same for all users, I can't hardcode that--I essentially need something that sets the active printer to the default printer.

    Why can't users just change this themselves? I do not know. Apparently they are too well paid to be expected to understand how to check their printer settings. (Grr. Sorry. Just venting.)

    In any event, any thoughts would be appreciated....thanks.

    Jen

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Reset printer to default using VBA (Word 2000)

    Well, this isn't as well documented as you might think. Here's the answer if you're a C programmer

  3. #3
    2 Star Lounger
    Join Date
    Nov 2001
    Location
    London, UK, England
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reset printer to default using VBA (Word 2000)

    I had a similar problem in a project that I am developing - I needed to switch to a different printer before opening Crystal reports (which picks up the default printer and doesn't let you change before printing!).

    Couple of functions here might help...

    ' // To notify applications the printer has changed
    Private Const HWND_BROADCAST As Long = &HFFFF
    Private Const WM_WININICHANGE As Long = &H1A

    Private 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

    Private Declare Function WriteProfileString Lib "kernel32" _
    Alias "WriteProfileStringA" _
    (ByVal lpszSection As String, _
    ByVal lpszKeyName As String, _
    ByVal lpszString As String) As Long

    Private Declare Function SendMessage Lib "user32" _
    Alias "SendMessageA" _
    (ByVal hwnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lparam As Any) As Long

    ' // ************************************************** ****************************
    ' // FUNCTION
    ' // SubDefaultPrinter(ByVal PrinterName As String,
    ' // ByVal DriverName As String,
    ' // ByVal PrinterPort As String)
    ' //
    ' // PARAMETERS
    ' // PrinterName - The name of the object printer
    ' // Drivername - The name of the driver for the objet printer
    ' // PrinterPort - The name of the port to action the printer on
    '
    ' // RETURN VALUE
    ' // None
    ' //
    ' // COMPONENTS USED
    ' // Windows Registry
    ' //
    ' // NOTES
    ' // You can change the 'default' printer programatically by using the Win32-API. Used
    ' // to switch from a default printer to 'Zetafax' without any user interction
    ' // ************************************************** ****************************

    Private Sub SetDefaultPrinter(ByVal PrinterName As String, _
    ByVal DriverName As String, _
    ByVal PrinterPort As String)
    Dim DeviceLine As String

    'rebuild a valid device line string
    DeviceLine = PrinterName & "," & DriverName & "," & PrinterPort

    'Store the new printer information in the
    '[WINDOWS] section of the WIN.INI file for
    'the DEVICE= item
    Call WriteProfileString("windows", "Device", DeviceLine)

    'Cause all applications to reload the INI file
    Call SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, ByVal "windows")

    End Sub

    ' // ************************************************** ****************************
    ' // FUNCTION
    ' // GetDefaultPrinter()
    ' //
    ' // PARAMETERS
    ' // None
    '
    ' // RETURN VALUE
    ' // String representation of the current default printer
    ' //
    ' // COMPONENTS USED
    ' // Windows Registry
    ' //
    ' // NOTES
    ' // Returns the value held in the registry that represents the users default printer
    ' // ************************************************** ****************************

    Public Function GetDefaultPrinter() As String

    Dim sReturnString As String

    ' // Allocate string space
    sReturnString = Space(1024)

    ' // Get the currently active (default) printer held in the registry
    GetDefaultPrinter = Left(sReturnString, GetProfileString("windows", "Device", "", sReturnString, Len(sReturnString)))

    ' // Cause all applications to reload the INI file
    Call SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, ByVal "windows")

    End Function

    Test out the 'GetDefaultPrinter' function which should return the print string that is stashed in the registry

  4. #4
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Chicago, Illinois, USA
    Posts
    185
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reset printer to default using VBA (Word 2000)

    jscher2000 and Dylan Morley: Thanks for your thoughts. I shall give both of these options a try and let you know what I figure out.

    I'm surprised that it isn't easier, though...like a .defaultprinter setting as compared to a .activeprinter setting, or something like that.

  5. #5
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Chicago, Illinois, USA
    Posts
    185
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reset printer to default using VBA (Word 2000)

    <P ID="edit" class=small>Edited by klyjen on 20-Dec-01 13:28.</P>Dylan--that did it! Thanks! I used your code, and then added a "ReSetDefaultMacro sub that looks like this...I had to do that because these are network printers and returning the string as your function returned it couldn't be read by the application.activeprinter setting.


    Public Sub ReSetDefaultPrinter()
    Dim sDefaultPrinter As String
    Dim First
    Dim Last
    Dim Total
    Dim PrinterName
    Dim PrinterEnd
    Dim counter As Integer

    'runs the GetDefaultPrinter function (above) and returns the value
    'into a string called sDefaultPrinter--this returns a string that
    'looks like (DC-7B1,winspool,NE74 <img src=/S/smile.gif border=0 alt=smile width=15 height=15>
    sDefaultPrinter = GetDefaultPrinter()
    'clears the active printer
    Application.ActivePrinter = ""
    'determines the position of the first comma in the string
    First = InStr(1, sDefaultPrinter, ",")
    'determines the position of the last comma in the string
    Last = InStrRev(sDefaultPrinter, ",")
    'determines the total length of the string
    Total = Len(sDefaultPrinter)
    'determines the printer name (the characters up to the first
    'comma in the string)
    PrinterName = Left(sDefaultPrinter, First - 1)
    'determines the code at the end of the string)
    PrinterEnd = Right(sDefaultPrinter, Total - Last)
    'resets the active printer to the default, setting the string to
    'look like it should (i.e., "DC-7B1 on NE74:")
    Application.ActivePrinter = PrinterName & " on " & PrinterEnd
    'creates a message box letting the user know the printer has
    'been reset and what its value is now
    response = MsgBox(("Your printer has been reset to " & PrinterName & "."), vbOKOnly)

    End Sub

    Thanks very much for your help. I will definitely keep your code for future use!

    Jen

    EDIT: That shouldn't be a smiley, it should be a : followed by a ) I can't figure out how to turn off the smileys in here!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •