Results 1 to 14 of 14
  1. #1
    5 Star Lounger Lou Sander's Avatar
    Join Date
    Jun 2002
    Location
    Pittsburgh, PA
    Posts
    855
    Thanks
    140
    Thanked 10 Times in 9 Posts

    Macro to change default printer (2003)

    <P ID="edit" class=small>(Edited by macropod on 16-Aug-08 12:54. Macro attached (not below). Subsequent 'corrective' post by Lou deleted)</P>The attached macro below is from one of my documents. It works fine, but I want to do somethng more with it.

    THe macro changes the default printer for this document only. When I click the Print icon on my toolbar, the document prints on printer "3 - BIG COLOR" rather than the default printer. When a different document is opened, the print icon causes it to print on the normal default printer.

    There's one more little thing I'd like to do. When the document containing this macro is open, the tool tip for the Print icon still shows the name of the default printer. I'd like to have it show BIG COLOR.

    Who knows how to do that?
    Attached Files Attached Files
    Lou Sander
    Pittsburgh, Pennsylvania
    USA

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Macro to change default printer (2003)

    Hi Lou,

    With the code set up the way it is, the active printer name is only changed briefly - when the document is printed - and is then reset. Consuently, the 'other' printer is the active printer at any other time.

    To get the messsage to change, you'd need to change the active printer when the document is opened and, preseumably, change it back when the document is closed. The problem with this is that then every other document you have open during that session would also default to your "3 - BIG COLOR" printer.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    5 Star Lounger Lou Sander's Avatar
    Join Date
    Jun 2002
    Location
    Pittsburgh, PA
    Posts
    855
    Thanks
    140
    Thanked 10 Times in 9 Posts

    Re: Macro to change default printer (2003)

    That would be OK, since only one document would be open at the time. (It's an envelope, and Word is closed after it's printed.)
    Lou Sander
    Pittsburgh, Pennsylvania
    USA

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Macro to change default printer (2003)

    Hi Lou,

    In that case, you could insert the following macros into the Envelope's Document module:

    Private Sub Document_Open()
    ActivePrinter = "3 - BIG COLOR"
    End Sub

    Private Sub Document_Close()
    ActivePrinter = "1 - LASER (HP LaserJet 1300)"
    End Sub

    and delete the corresponding lines from your existing macro.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    5 Star Lounger Lou Sander's Avatar
    Join Date
    Jun 2002
    Location
    Pittsburgh, PA
    Posts
    855
    Thanks
    140
    Thanked 10 Times in 9 Posts

    Re: Macro to change default printer (2003)

    Paul,

    I tried what you suggested, and it works part way. When I open the document, the default printer changes to "3 - BIG COLOR" but it doesn't change back when I close the document.

    I have deleted the other macro entirely, which might possibly have something to do with this.
    Lou Sander
    Pittsburgh, Pennsylvania
    USA

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to change default printer (2003)

    Did you put the Document_Close procedure in the ThisDocument module of the document?

  7. #7
    5 Star Lounger Lou Sander's Avatar
    Join Date
    Jun 2002
    Location
    Pittsburgh, PA
    Posts
    855
    Thanks
    140
    Thanked 10 Times in 9 Posts

    Re: Macro to change default printer (2003)

    No, I hadn't. I've put it there now, and everything seems to work OK.

    Both the open and close macros are now in the ThisDocument module, in the form suggested above. I can see them in the Visual Basic Editor window, but they don't show up in the Tools>Macro>Macros window. Maybe that has something to do with them being Private. (I'm able to muddle along in VBA, but I don't really understand the items in the Project window. )
    Lou Sander
    Pittsburgh, Pennsylvania
    USA

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to change default printer (2003)

    The macros listed in Tools | Macro | Macros are public procedures (subs) in standard modules that have no arguments.

    So all of the following are *not* listed in the Macros dialog:
    - Private procedures.
    - Procedures that have arguments.
    - Functions.
    - Procedures in class modules, in userform modules and in the ThisDocument module.

    Document_Open and Document_Close are so-called event procedures; you use them to specify how Word should react to certain events (in this case, the document being opened and the document being closed).

  9. #9
    5 Star Lounger Lou Sander's Avatar
    Join Date
    Jun 2002
    Location
    Pittsburgh, PA
    Posts
    855
    Thanks
    140
    Thanked 10 Times in 9 Posts

    Re: Macro to change default printer (2003)

    Got it! Thanks. That stuff is hard to keep track of when you only use it occasionally.

    Now there is a new level of complication. I actually did all the above experimentation on a test document. When I moved it to my "real" document, it didn't work, and I have some understanding of why.

    My "real" document is actually a document template that is used to open a temporary document from which I print an envelope. What I want is for the temporary document, when opened, to change the default printer, and when closed, to restore it.

    At the moment, the template swaps printers as desired, but the temporary document opened from it doesn't have the macros for printer swapping. Is there a way to put them in there?
    Lou Sander
    Pittsburgh, Pennsylvania
    USA

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to change default printer (2003)

    Do you create the temporary document from a document or template on disk?

    If so, put the Document_Open and Document_Close event procedures in the ThisDocument module of that document or template.

    If not, create such a document or template now...

  11. #11
    5 Star Lounger Lou Sander's Avatar
    Join Date
    Jun 2002
    Location
    Pittsburgh, PA
    Posts
    855
    Thanks
    140
    Thanked 10 Times in 9 Posts

    Re: Macro to change default printer (2003)

    The temporary document is created from EnvelopeBMK.dot, which has the following code in the ThisDocument module:

    Private Sub Document_Open()
    ' Macro created 8/17/2008 by Louis F. Sander
    ' Switches default printer when the document is opened
    '
    ActivePrinter = "3 - BIG COLOR"
    End Sub
    Private Sub Document_Close()
    ' Macro created 8/17/2008 by Louis F. Sander
    ' Document_Close Macro
    ' Restores original default printer when the document is closed
    '
    ActivePrinter = "1 - LASER (HP LaserJet 1300)"
    End Sub

    When I load EnvelopeBMK.dot, the default printer switches to BIG COLOR. When I close it, the default printer switches back.

    But when a document is created using EnvelopeBMK.dot, the macro code isn't there.

    The document is actually created by clicking a command button in an Access application, which may have something to do with what's not happening. Clicking the command button executes this code:

    Private Sub cmdEnvelope_Click()
    'Create a Word document from the targeted template
    Dim wd As Word.Application
    Set wd = New Word.Application
    Dim sAddress As String, sCity As String

    'The targeted template is an envelope:
    wd.Documents.Add (CurrentProject.Path & "DocumentTemplatesEnvelopeBMK.dot")

    With wd.ActiveDocument.Content.Bookmarks
    .Item("Business").Range = Nz(Business, "")
    .Item("Last").Range = Nz(Me.Last, "")
    .Item("Suf").Range = Nz(Me.Suf, "")
    .Item("Hon").Range = Nz(Me.Hon, "")
    sAddress = IIf(IsNull(Address) = False, Address, "") & _
    IIf(IsNull(Address2) = False, Chr(13) & Address2, "") & _
    IIf(IsNull(Address3) = False, Chr(13) & Address3, "")
    .Item("Address").Range = RTrim(sAddress)
    sCity = Nz(Me.City, "") + IIf(IsNull(State) = False, ", " & Me.State, "") + _
    IIf(IsNull(ZIP) = False, " " & Me.ZIP, "") + IIf(IsNull(Country) = False, Me.Country, "")
    .Item("City").Range = sCity
    ' .Item("Salutation").Range = Nz(Me.Salutation, "")
    End With

    wd.Visible = True
    Set wd = Nothing

    End Sub
    Lou Sander
    Pittsburgh, Pennsylvania
    USA

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to change default printer (2003)

    When you create a document based on a template, the VBA code is *NOT* copied from the template into the document. The code from the template is available to the document, though, and event procedures such as Document_Open and Document_Close should be executed, whether you create the document manually or using code.

    This is a very convenient setup. It means that if you want to modify the code, you don't have to do so in every document based on the template, but only in the template itself. As soon as you save the template, the changed code will be available to all documents based on the template.

  13. #13
    5 Star Lounger Lou Sander's Avatar
    Join Date
    Jun 2002
    Location
    Pittsburgh, PA
    Posts
    855
    Thanks
    140
    Thanked 10 Times in 9 Posts

    Re: Macro to change default printer (2003)

    I hear what you are saying, but the temporary document is NOT changing the default printer. When it opens, the printer icon tool tip shows the original default printer (1 - Laser, etc.). When I click the icon, the temporary document prints on that printer.

    Any idea why?
    Lou Sander
    Pittsburgh, Pennsylvania
    USA

  14. #14
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to change default printer (2003)

    No idea, sorry. If you wish, you can zip (sanitized copies of) the relevant files and attach the zip file to a reply.

Posting Permissions

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