How to not change Word's last selected printer

R

Ryan Taylor

Hello,

We have a Word add-in developed in C# and are having an issue with how Word
changes its last selected printer. We would like to be able to print behind
the scenes using the Word COM object, and NOT change the last used printer in
Word.

Background info on our add-in:
When the user clicks our custom button, a print job is performed in the
background to one of the printers (it is always the same printer).

Also, in our code we account for the ActivePrinter issue, so that the
default printer on the system level is unchanged.

Example:
Customer has 3 printers: printerSystemDefault, printerUserChanged, and
printerOurCustom.

When the user first opens Word and selects print, Word will select the
printerSystemDefault printer by default. Close the print window.

Now, use our add-in by clicking the button. A print job is done behind the
scenes to the printerOurCustom printer. If the user now tries to do a
regular Word print (Ctrl P, etc) Word defaults to our custom printer,
presumably because it was the last printer used from within Word.

How can this be avoided? I tried storing the current ActivePrinter before
doing the behind the scenes, add-in print job, then setting the ActivePrinter
back, however that doesn't seem like the appropriate way to handle this and
it doesn't seem to work correctly. (it doesnt work correctly if the user
changes their printer to printerUserChanged. If they perform a print job to
printerUserChanged, then word will default to that printer. If I, in the
add-in code, store the ActivePrinter, it will in fact store this last
selected printer. When I set ActivePrinter back, however, it changes the
System Default Printer!)

Any help that you guys can provide would be greatly appreciated!

Thanks,
Ryan
 
R

Ryan Taylor

Hi Doug,

Thank you for your reply. I am able to print in word without changing the
system default printer. My question is along the lines of printing in word
and not changing WORD's last used printer. In other words, I do not want
Word to change its internally held, last selected printer.

Example:

Default printer = printer 1
My Printer = printer 2
another printer = printer 3

When word is opened for the first time, the default selected printer in word
is printer 1. If the user manual does a print job on printer 3, then very
next time the user does a manual print job, word defaults their selection to
printer 3. This is OK if they are manually doing the print job.

With my add-in however, I do not want change Word's selection. So, If the
user does a manual print job to printer 3, the next they do a print, Word
defaults them to printer 3. Now, if they use my add-in, behind the scenes a
print job is done to printer 2. If the user now does another manual print
job, Word defaults it to printer 2 instead of printer 3. (Note, the system
default is not affected, I have handled that situation in a manner similar to
how you have described in your link).

Thanks for your continued help,
Ryan
 
D

Doug Robbins - Word MVP

Use

Application.ActivePrinter

to get the name of the active printer and then set it back to that after
doing your thing.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

Ryan Taylor

Gentlemen,

Thank you for your continued help on this matter. I have tried both of your
suggestions however the situation that I have outlined still occurs.

The problem seems to be with ActivePrinter. If the user prints to printer
3, ActivePrinter will now show printer 3, that is fine. Now, if I use my
add-in, which prints to printer 2, ActivePrinter will now show printer 2.
As you guys have noted, I could store the current ActivePrinter, do my
printing, then set the ActivePrinter back, however this will not work due to
the nature of ActivePrinter. As soon as I set ActivePrinter, it doesn't just
set the printer for Word, it sets it for the whole system!

So, when retrieving ActivePrinter, I will always get Words default printer,
if the user changes the printer to printer 3 in word only, ActivePrinter will
return me printer 3, even though system default printer is still printer 1.
If I SET ActivePrinter, Word's default printer will be set AND the system
default printer will be set.

So, is there anyway to set ONLY Words last used printer, since setting
ActivePrinter will result in changing the system default printer.

Thanks,
Ryan
 
G

Graham Mayor

The macros set the Active printer back to what it was previously as soon as
the print is complete.
However you can set the active printer without changing the default
printer -

Sub MyPrint()
Dim sPrinter As String
With Dialogs(wdDialogFilePrintSetup)
sPrinter = .Printer
.Printer = "Printername"
.DoNotSetAsSysDefault = True
.Execute
Application.PrintOut
.Printer = sPrinter
.Execute
End With
End Sub

which is a slight variation on the method shown on my web site.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
R

Ryan Taylor

Thank you for your help gentlemen, everything now works as expected. I just
needed to realize that it is possible to set the Printer without actually
using it. So, I essentially store the current ActivePrinter, change the
printer to my printer using the tricks mentioned above, do my printing behind
the scenes, and then try to set the ActivePrinter back using the tricks
mentioned above again.

Thank you guys for your help.

Best Regards,
Ryan Taylor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top