Solved

Printing question

Posted on 2002-04-19
7
165 Views
Last Modified: 2010-05-02
Hi,

I have a method that prints out a picture, and I am using the Printers collection in the code, along with the Printer object, like so:

frmMain.CommonDialog1.ShowPrinter
frmMain.CommonDialog1.Flags = cdlPDReturnDC
 
Dim Y As Printer
For Each Y In Printers
    If Y.hdc = frmMain.CommonDialog1.hdc Then
        ' Set printer as system default.
        Set Printer = Y
        ' Stop looking for a printer.
        Exit For
    End If
Next

The above works fine. Through this code, I aquire the printer that the user selected from the CommonDialog object. My problem is that now "Printer", which used to point to the default printer, now points to another printer, which has now become the default printer. Is there a way to set the default printer back to what it was before I changed it?

I have already tried the following:

"Dim DefaultPrinter as Printer
 Set DefaultPrinter = Printer

 'lines of code to do all my printing stuff
 Set Printer = DefaultPrinter"

This doesn't seem to work, DefaultPrinter never receives a value. Also tried this at the very begining of the method:

    Dim X As Printer
    For Each X In Printers
        If X.hdc = Printer.hdc Then
            Set DefaultPrinter = X
            Exit For
        End If
    Next
This never finds a match either. I am stumped now. Any help would be appreciated.


0
Comment
Question by:jhamlett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 17

Expert Comment

by:inthedark
ID: 6955203
Just store the device name.

Then use code a bit like this:


StorePrinter = Printer.DeviceName

' Do your printing


CAll SetPrinter(StorePrinter)

' Etc....


Sub SetPrinter(DeviceName as string)

Dim Pr as Printer
For Each Printer in Printers
    If Ucase(Pr.DeviceName) = Ucase(DeviceName) Then
        Set Printer = Pr
        Exit Sub
    End If
Next

End Sub



0
 
LVL 17

Expert Comment

by:inthedark
ID: 6955204
I expect in your form load/Sub Main you have:

Printer.TrackDefault = True
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Accepted Solution

by:
inthedark earned 100 total points
ID: 6955206
Woops...
For Each Pr in Printers

Would work better, so its like:

Sub SetPrinter(DeviceName as string)

Dim Pr as Printer
For Each Pr in Printers
   If Ucase(Pr.DeviceName) = Ucase(DeviceName) Then
       Set Printer = Pr
       Exit Sub
   End If
Next

End Sub
0
 

Author Comment

by:jhamlett
ID: 6963520
Both good answers. I tried intherdark's method, since it was the simplest, and I do get back the default device name. I seem like I am setting the printer i want back to default, until I re-enter the function and re-open the common dialog. at that point, it still shows the last printer selected as being default? Since I am getting the selected printer via the .hDc prop of the common dialog, is it likely that the common dialog is setting the default based on its own .hDc?
0
 
LVL 17

Expert Comment

by:inthedark
ID: 6964178
jhamlett, just an after thought.  When I develop an App. I get the op. to select the printers they want to use for invoices and also for reports.  So I always select the right printer for the job, then return it to the default after the report is complete.

The documentation covering the use is the common dialog in respect of printers is very poor.  The documentation expected you to be using the RTF control.....but why would I want to use that when most be use Word for WP?
0
 

Expert Comment

by:torrid333
ID: 11382521
I tried implementing this and it does not selet the current printer...

Anyone have an updated version of this type of code all in one place????
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question