Ken Slovak - [MVP - Outlook]

Outlook reads the default system printer on startup and the only print

method you have is to print. No arguments, no settings, just print. That's

it.

"Victor Delta" <none@nospam.com> wrote in message

news:eqFJCyaoJHA.3572@TK2MSFTNGP05.phx.gbl...
> Can anyone please tell me how to record a macro on Outlook please (I'm
> using Office 2003). I was expecting the same sort of 'record new macro'
> facility as exists in Word and Excel, but it doesn't seem to be there.

> All I'm trying to do is create a macro which changes the active printer,
> prints the current email, and then resets the active printer to the
> default printer.

> Without the usual macro 'wizard', so far I've only got as far as '
> ActivePrinter "Printer2" '.

Victor Delta

news:OjzgqBcoJHA.504@TK2MSFTNGP06.phx.gbl...
> It's not possible at all in Outlook. If you want to format your printout,
> be selective in what's printed or print to a specific printer or use
> background printing most of us use Word and take the data we want and put
> it into a Word template and then do the printing using the Word object
> model.

Many thanks for that, although I have to say I am very surprised and

disappointed that VBA in Outlook is so limited. I gather you cannot even set

the active printer as in other Office applications.

However, I haven't given up quite yet and wondered if anyone knows if there

are any third party applications that would help? For example, there used to

be a nifty freeware programme available that enabled one to automate certain

Victor Delta

news:uOJOi%231oJHA.3984@TK2MSFTNGP02.phx.gbl...
> This can be done by changing the default printer using vba but it takes a
> little bit of code. I will do my best to explain.
> First create a new module in the vba editor in outlook (VbaProject.OTM),
> you
> can set the name to anything you would like
> at the very top (just below Option Explicit if it's there) paste the
> following

> You can now create a button and assign the PrintGreen macro to it and it
> should print any emails you have selected to your specified printer. I
> did
> test this on my computer and found there is a several second (ok like 10)
> delay while printing. You may or may not have this issue. Anyway, post
> back if you have any questions.
> Paul D

Paul

Many thanks - that's very interesting. I'll give it a try and let you know

how I get on.

V

V

Victor Delta

>> You can now create a button and assign the PrintGreen macro to it and it
> > should print any emails you have selected to your specified printer. I
> > did
> > test this on my computer and found there is a several second (ok like 10)
> > delay while printing. You may or may not have this issue. Anyway, post
> > back if you have any questions.
> > Paul D

Paul

Just a thought. In view of the delay, would it be better to write the code

Victor Delta

news:uAOTVAkpJHA.5280@TK2MSFTNGP02.phx.gbl...
> Victor,
> That could easily be done, the only issue I see is if you leave outlook
> open
> all the time (like I do), then the default printer will be incorrect for
> other programs until you close outlook. Maybe not an issue for you, so if
> that's the case you would have to add some event code to
> ThisOutlookSession:

Victor Delta

news:uOJOi%231oJHA.3984@TK2MSFTNGP02.phx.gbl...
> This can be done by changing the default printer using vba but it takes a
> little bit of code. I will do my best to explain.
> First create a new module in the vba editor in outlook (VbaProject.OTM),
> you
> can set the name to anything you would like
> at the very top (just below Option Explicit if it's there) paste the
> following

> You can now create a button and assign the PrintGreen macro to it and it
> should print any emails you have selected to your specified printer. I
> did
> test this on my computer and found there is a several second (ok like 10)
> delay while printing. You may or may not have this issue. Anyway, post
> back if you have any questions.
> Paul D

Paul

I followed your instructions and tried your original code today.

However, when I tried to run it, a compile error message came up -

'WshNetwork As WshNetwork' was highlighted and the message said

'User-defined type not defined'. Any idea how to resolve this?

V

V

Victor Delta

news:ugA5UAzpJHA.5832@TK2MSFTNGP06.phx.gbl...
> "PaulD" <nospam> wrote in message
> news:uOJOi%231oJHA.3984@TK2MSFTNGP02.phx.gbl...
> > This can be done by changing the default printer using vba but it takes a
> > little bit of code. I will do my best to explain.
> > First create a new module in the vba editor in outlook (VbaProject.OTM),
> > you
> > can set the name to anything you would like
> > at the very top (just below Option Explicit if it's there) paste the
> > following
>

>> You can now create a button and assign the PrintGreen macro to it and it
> > should print any emails you have selected to your specified printer. I
> > did
> > test this on my computer and found there is a several second (ok like 10)
> > delay while printing. You may or may not have this issue. Anyway, post
> > back if you have any questions.
> > Paul D

> Paul

> I followed your instructions and tried your original code today.

> However, when I tried to run it, a compile error message came up -
> 'WshNetwork As WshNetwork' was highlighted and the message said
> 'User-defined type not defined'. Any idea how to resolve this?

> V

Paul

After a bit of experimentation, I changed the line to

Dim WshNetwork As Object

and the macro now runs. However, it just seems to still send documents to

the default printer - even through I changed "ENTER YOUR PRINTER NAME HERE!"

Ken Slovak - [MVP - Outlook]

I haven't tested that in a few years, unless something changed that's the

way it always was. It's possible that MS changed things to read the current

printer when PrintOut() is called however.

"PaulD" <nospam> wrote in message

news:ucM1CD%23pJHA.4516@TK2MSFTNGP02.phx.gbl...
> Ken,
> Intesting comment since I tested this using Outlook 2003 and it does work.
> Have you tested this yourself ? Without any code you can test this by
> printing an email, then going into your printers settings, changing your
> default printer then printing the email again.
> Paul D

V

Victor Delta

news:u8sUMG%23pJHA.5880@TK2MSFTNGP05.phx.gbl...
> Victor,
> My mistake for not telling you to set the appropriate reference. Anyway,
> I
> would assume you may not have the correct printer name being used. To
> verify this set your "GreenPrint" as the default printer first, then set a
> break point in the PrintGreen sub at the line
> WshNetwork.SetDefaultPrinter ("ENTER YOUR PRINTER NAME HERE!")
> Now run the code to your breakpoint and verify what text is being stored
> in
> the strDefault string. This text is the exact text you need to used in
> the
> "ENTER YOUR PRINTER..." area
> Paul D

Paul

Many thanks for that. However, I'm still having problems.

I followed your advice above and found that the correct printer name is

GREENPRINT so inserted this in the code which now looks like this (by the

way, was I correct about changing to 'Dim WshNetwork As Object'?):

Public Declare Function GetProfileString Lib "kernel32" _

Alias "GetProfileStringA" _

(ByVal lpAppName As String, _

ByVal lpKeyName As String, _

ByVal lpDefault As String, _

ByVal lpReturnedString As String, _

ByVal nSize As Long) As Long

Sub PrintGreen()

Dim strDefault As String

Dim WshNetwork As Object

Dim i As Integer

Dim myOlApp As Outlook.Application

Dim mySelection As Selection

Set myOlApp = Application

Set mySelection = myOlApp.ActiveExplorer.Selection

Set WshNetwork = CreateObject("WScript.Network")

strDefault = DefaultPrinter

WshNetwork.SetDefaultPrinter ("GREENPRINT")

For i = mySelection.Count To 1 Step -1

If mySelection.Item(i).Class = olMail Then mySelection.Item(i).PrintOut

Next i

WshNetwork.SetDefaultPrinter (strDefault) ' restore default

End Sub

Public Function DefaultPrinter() As String

Dim strReturn As String

Dim intReturn As Integer

strReturn = Space(255)

intReturn = GetProfileString("Windows", ByVal "device", "", _

strReturn, Len(strReturn))

If intReturn Then

strReturn = UCase(Left(strReturn, InStr(strReturn, ",") - 1))

End If

DefaultPrinter = strReturn

End Function

However, when I run this with the original default printer set, the print

goes to the default printer not Green Print! Have I made a mistake

somewhere?

Obviously it's working at your end - have you made any other changes

perhaps?

I'm glad this is possible because although I'm sure Ken is very expert in

such matters, it must be possible to change the Outlook printer otherwise

one wouldn't be able to do it manually so easily (couple of clicks) via the

Victor Delta

news:%23cCpZtJqJHA.1172@TK2MSFTNGP05.phx.gbl...
> Victor,
> You were correct to change 'Dim WshNetwork As Object', that should work.
> Since you could follow my directions before, I would recommened you try
> this: set a break point on the line
> For i = mySelection.Count To 1 Step -1
> in the PrintGreen sub and then run the code. You should be able to hover
> over the strDefault variable to see if it was set, then open your printers
> and faxes window and see if the default printer was switched. To make
> sure
> I have the same code as you I copied the code from your post below into a
> new module and ran it to make sure it worked. I got an error saying it
> couldn't find the printer "GREENPRINT" which is good since I don't have
> that
> printer. I changed it to a network printer I do have (CutePDF) and it
> worked. Investigae some more and post back with your results
> Paul D

Paul

Thanks. This is most odd. Followed your instructions and sure enough, using

the break point, the default printer does change to Green Print and then

changes back. However, the print out still seems to go through to the wrong

printer, although on a couple of occasions when using Step into (F8) I got

it to go to Green Print. I've been trying again and for the last 10 minutes

it will only go to the wrong printer!

I'll do some more experimentation but not quite sure why it should be so

inconsistent! Any ideas? We're so nearly there...

V

V

Victor Delta

news:OKENscNqJHA.5832@TK2MSFTNGP06.phx.gbl...
> "PaulD" <nospam> wrote in message
> news:%23cCpZtJqJHA.1172@TK2MSFTNGP05.phx.gbl...
> > Victor,
> > You were correct to change 'Dim WshNetwork As Object', that should work.
> > Since you could follow my directions before, I would recommened you try
> > this: set a break point on the line
> > For i = mySelection.Count To 1 Step -1
> > in the PrintGreen sub and then run the code. You should be able to hover
> > over the strDefault variable to see if it was set, then open your
> > printers
> > and faxes window and see if the default printer was switched. To make
> > sure
> > I have the same code as you I copied the code from your post below into a
> > new module and ran it to make sure it worked. I got an error saying it
> > couldn't find the printer "GREENPRINT" which is good since I don't have
> > that
> > printer. I changed it to a network printer I do have (CutePDF) and it
> > worked. Investigae some more and post back with your results
> > Paul D

> Paul

> Thanks. This is most odd. Followed your instructions and sure enough,
> using
> the break point, the default printer does change to Green Print and then
> changes back. However, the print out still seems to go through to the
> wrong
> printer, although on a couple of occasions when using Step into (F8) I got
> it to go to Green Print. I've been trying again and for the last 10
> minutes
> it will only go to the wrong printer!

> I'll do some more experimentation but not quite sure why it should be so
> inconsistent! Any ideas? We're so nearly there...

Have done some more experimentation and believe I have now found the

problem - and it's very interesting.

To aid clarity lets call the two printers Printer A (normal default) and

Printer Green. Once Outlook is open, if you change the default printer

(either manually or via your macro with the break point on the "For i = ..."

line) if you hover over the Print button at the top of any email, you will

see the name of the new printer (in my case Printer Green). This looks very

promising...

However, there's a big gotcha! If you now open the Outlook Print dialogue

box (Ctrl +P), although you will see the little black tick against the name

of the new default printer, the original printer (Printer A) is actually

still selected (highlighted in blue). So if you now print, either by

clicking the button or running the second part of your code, the email goes

to the original printer. I guess this is what Ken was telling us all along!

If only there were a way to 'refresh' the printer selection to the new

default printer it would work but, so far, I haven't thought of one. Any

ideas?

The only surprise now is that you've managed to make it work at your end.

I've been using Windows XP with Outlook XP/2002 and, on another machine,