Results 1 to 11 of 11

Thread: Change Printer when Printing

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Change Printer when Printing

    I have a common dialog box for printing. It does the normal, pops up the box, choose your printer and your settings then you print it.. Only problem is if you bring up the dialog box again to print without restarting the program, then no matter what printer you select, it always prints it on the first printer you selected the first time you printed something. Is there a way to reset something with this so it will print to a different printer if i select another one at a later time before restarting the program?

  2. #2
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Change Printer when Printing

    Can you show the code?

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Re: Change Printer when Printing

    Code:
    Private Sub PrintNow()
    On Error GoTo ErrorHandler
    Dim rv As Long
    CommonDialog1.ShowPrinter
    
    With Picture2
       .top = Picture1.top
       .left = Picture1.left
       .Width = Picture1.Width
       .Height = Picture1.Height
       .Visible = False
       .BorderStyle = 0
    End With
    'Paint Picture1 to invisible Picture2
    Picture2.AutoRedraw = True
    rv = SendMessage(Picture1.hwnd, WM_PAINT, Picture2.hdc, 0)
    rv = SendMessage(Picture1.hwnd, WM_PRINT, Picture2.hdc, PRF_CHILDREN + PRF_CLIENT + PRF_OWNED)
    
    'Paint the Flexgrid to invisible Picture2
    
    'Print Picture2
    Picture2.Picture = Picture2.Image
    Picture2.AutoRedraw = False
    Call PrintPictureToFitPage(Printer, Picture2.Picture, 0, 0)
    'For the additional Picture3
    'Paint Picture3 to invisible Picture4 and set visibility options
    
    With picLogoInvis
       .top = picLogo.top
       .left = picLogo.left
       .Width = picLogo.Width
       .Height = picLogo.Height
       .Visible = False
       .BorderStyle = 1
    End With
    picLogoInvis.AutoRedraw = True
    
    rv = SendMessage(picLogo.hwnd, WM_PAINT, picLogoInvis.hdc, 0)
    rv = SendMessage(picLogo.hwnd, WM_PAINT, picLogoInvis.hdc, PRF_CHILDREN + PRF_CLIENT + PRF_OWNED)
    
    SmallPrnPicWidth = picLogoInvis.Width * PrnPicWidth / Picture1.Width
    SmallPrnPicHeight = picLogoInvis.Height * PrnPicHeight / Picture1.Height
    SmallPrnPicLeft = picLogoInvis.left * PrnPicWidth / Picture1.Width
    SmallPrnPicTop = picLogoInvis.top * PrnPicHeight / Picture1.Height
    
    picLogoInvis.Picture = picLogoInvis.Image
    picLogoInvis.AutoRedraw = False
    Printer.PaintPicture picLogoInvis, SmallPrnPicLeft, SmallPrnPicTop, SmallPrnPicWidth, SmallPrnPicHeight
    
    Printer.EndDoc
    End Sub

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Change Printer when Printing

    ShowPrinter should change the Printer when the user selects another, just to test: run this to see if it changes:
    Code:
         CommonDialog1.ShowPrinter 
         MsgBox Printer.DeviceName
    Run, see the Printer, Run again and select another Printer, DeviceName should change.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Re: Change Printer when Printing

    Only changes if i cancel the print.. But once ive printed 1 thing, no matter what i do it wont change..

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Re: Change Printer when Printing

    ok im trying something different... Im trying to declare the variable 'OldPrinter As Printer' and then set it as the default printer... Then show the common dialog box to change the printer, print the document, and at the end, set the printer as what the default printer used to be... This is what i have at the beginning of my code:

    Code:
    Dim OldPrinter As Printer
    Set OldPrinter = Printer
    CommonDialog1.ShowPrinter
    And at the end after Printer.EndDoc, i have this:

    Code:
    Set Printer = OldPrinter
    But each time it highlights the last line i just showed, and says "Type Mismatch"..... i dont know why, i must be doing something wrong

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Re: Change Printer when Printing

    Anybody have any ideas or any links that could help me fix this issue? I cant find anything in the forums that will solve this issue.

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Change Printer when Printing

    Try this (adapted from the help file)
    Code:
    Dim OldPrinter As String, X As Printer
    OldPrinter = Printer.DeviceName
    CommonDialog1.ShowPrinter
    'do your printing here
    For Each X In Printers
      If X.DeviceName = OldPrinter Then
        Set Printer = X
        Exit For
      End If
    Next X
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Re: Change Printer when Printing

    ok i stepped through the code all the way... and everything is holding the correct values, but its not setting the printer back to what it originally was... it changes it to the one the user selected and doesnet change it back

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Change Printer when Printing

    You're right. It's a little more complicated. Look at this link - scroll down almost to the bottom and look for Set Default Printer. I know that one works - I just put it into a program I'm using and it switches the Windows default printer nicely. The only problem is that the process is slow if you have a lot of programs running.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Re: Change Printer when Printing

    Quote Originally Posted by Al42
    You're right. It's a little more complicated. Look at this link - scroll down almost to the bottom and look for Set Default Printer. I know that one works - I just put it into a program I'm using and it switches the Windows default printer nicely. The only problem is that the process is slow if you have a lot of programs running.
    ok, basically what im doing now is im using the common dialog box to show the printer list, and since its not working to actually change the printer like its supposed too, im throwing the code in from that site you showed me. But here's the problem.

    Code:
    Private Sub SetDefaultPrinter(ByVal PrinterName As String, _
    ByVal DriverName As String, ByVal PrinterPort As String)
    Dim DeviceLine As String
    Dim r As Long
    Dim l As Long
    DeviceLine = PrinterName & "," & DriverName & "," & PrinterPort
    ' Store the new printer information in the [WINDOWS] section of
    ' the WIN.INI file for the DEVICE= item
    r = WriteProfileString("windows", "Device", DeviceLine)
    ' Cause all applications to reload the INI file:
    l = SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, "windows")
    End Sub
    The red line returns a type mismatch error. I dont know squat about API so i dont even know where to begin to figure this one out.. Here's how i declare SendMessage:

    Code:
    Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lparam As String) As Long
    Thats declared exactly the same way as the code i copied. When i put this stuff in a new form the way the code says it works, but when i use it in conjunction with my program i get type mismatch on that line. Once it hits that line, it immediatly goes back to the print sub, all the way to the bottom at the error control. Doesnt goto the end sub line after the SendMessage function. Am i doing something wrong?

Posting Permissions

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



Click Here to Expand Forum to Full Width