Solved

Printing question

Posted on 2002-04-19
7
158 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
7 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
0
 
LVL 17

Expert Comment

by:inthedark
Comment Utility
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
Comment Utility
I expect in your form load/Sub Main you have:

Printer.TrackDefault = True
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 17

Accepted Solution

by:
inthedark earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now