Outlook macro help please

Status
Not open for further replies.
V

Victor Delta

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" '.

Can anyone help please?

TIA

V
 
No macro recorder in Outlook, never was, never will be.

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" '.

> Can anyone help please?

> TIA

> V
>
 
" - " <kenslovak@mvps.org> wrote in message

news:utMuEBboJHA.4372@TK2MSFTNGP02.phx.gbl...
> No macro recorder in Outlook, never was, never will be.

> 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.


Thanks, Ken. So, if I understand you correctly, you're saying that the

simple task I am attempting is just not possible (using a macro anyway)? Is

that so?

V
 
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.

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

news:%237Vt7mboJHA.1172@TK2MSFTNGP05.phx.gbl...
> " - " <kenslovak@mvps.org> wrote in message
> news:utMuEBboJHA.4372@TK2MSFTNGP02.phx.gbl...
> > No macro recorder in Outlook, never was, never will be.
>

>> 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.


> Thanks, Ken. So, if I understand you correctly, you're saying that the
> simple task I am attempting is just not possible (using a macro anyway)?
> Is that so?

> V
 
" - " <kenslovak@mvps.org> wrote in message

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

routine tasks but I can't remember it's details. Certainly changing the

printer is not terribly complicated in terms of key strokes so I don't know

if this would be a possibility?

Incidentally the reason I am trying to do this is to use Green Print with

Outlook (only). Green Print can be downloaded from www.printgreener.com (the

'world' version is free) and it works between applications and one's printer

and enables one to easily stop blank or unimportant pages being printed. So

for Outlook it is ideal - and saves what you describe about having to copy

and paste data to Word etc for controlled printing. However, for Word etc it

just slows things down (an unnecessary extra stage in the printing process)

and so I don't want it to be the default printer.

V

PS Suppose one way round this would be set Green Print as the default

printer and then write macros for all the other Office programmes to set

their active printers to the 'real' printer? A laboriously long way round...
 
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

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 WshNetwork

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 ("ENTER YOUR PRINTER NAME HERE!")

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

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

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

news:uUteDDpoJHA.504@TK2MSFTNGP06.phx.gbl...

: " - " <kenslovak@mvps.org> wrote in message

: 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

: routine tasks but I can't remember it's details. Certainly changing the

: printer is not terribly complicated in terms of key strokes so I don't

know

: if this would be a possibility?

:

: Incidentally the reason I am trying to do this is to use Green Print with

: Outlook (only). Green Print can be downloaded from www.printgreener.com

(the

: 'world' version is free) and it works between applications and one's

printer

: and enables one to easily stop blank or unimportant pages being printed.

So

: for Outlook it is ideal - and saves what you describe about having to copy

: and paste data to Word etc for controlled printing. However, for Word etc

it

: just slows things down (an unnecessary extra stage in the printing

process)

: and so I don't want it to be the default printer.

:

: V

:

: PS Suppose one way round this would be set Green Print as the default

: printer and then write macros for all the other Office programmes to set

: their active printers to the 'real' printer? A laboriously long way

round...

:
 
"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

> 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 WshNetwork
> 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 ("ENTER YOUR PRINTER NAME HERE!")
> 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

> 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
 
> "PaulD" <nospam> wrote in message
>

>> 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

so that the printer is changed when Outlook opens?

V
 
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:

Option Explicit

Dim strDefaultPrinter As String

Private Sub Application_Startup()

Dim WshNetwork As WshNetwork

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

strDefaultPrinter = strReturn 'save default printer

Set WshNetwork = CreateObject("WScript.Network")

WshNetwork.SetDefaultPrinter ("YOUR PRINTER NAME HERE")

Set WshNetwork = Nothing

End Sub

Private Sub Application_Quit()

Dim WshNetwork As WshNetwork

Set WshNetwork = CreateObject("WScript.Network")

WshNetwork.SetDefaultPrinter (strDefaultPrinter) ' restore default

Set WshNetwork = Nothing

End Sub

I would leave the Public Declare function GetProfileString in its own module

called Declarations and delete the sub PrintGreen and function

DefaultPrinter

Paul D

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

news:On9$IsBpJHA.4448@TK2MSFTNGP05.phx.gbl...

:> "PaulD" <nospam> wrote in message

: >
: >> 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

: so that the printer is changed when Outlook opens?

:

: V

:
 
"PaulD" <nospam> wrote in message

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:

> Option Explicit
> Dim strDefaultPrinter As String

> Private Sub Application_Startup()
> Dim WshNetwork As WshNetwork
> 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
> strDefaultPrinter = strReturn 'save default printer
> Set WshNetwork = CreateObject("WScript.Network")
> WshNetwork.SetDefaultPrinter ("YOUR PRINTER NAME HERE")
> Set WshNetwork = Nothing
> End Sub

> Private Sub Application_Quit()
> Dim WshNetwork As WshNetwork

> Set WshNetwork = CreateObject("WScript.Network")
> WshNetwork.SetDefaultPrinter (strDefaultPrinter) ' restore default
> Set WshNetwork = Nothing
> End Sub

> I would leave the Public Declare function GetProfileString in its own
> module
> called Declarations and delete the sub PrintGreen and function
> DefaultPrinter
> Paul D


Thanks, good point!

V
 
"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

> 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 WshNetwork
> 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 ("ENTER YOUR PRINTER NAME HERE!")
> 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

> 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
 
"Victor Delta" <none@nospam.com> wrote in message

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
>

>> 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 WshNetwork
> > 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 ("ENTER YOUR PRINTER NAME HERE!")
> > 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
>

>> 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!"

to "GreenPrint"?

V
 
Outlook only reads the printer to use when it starts up. You can change the

system printer as often as you want and it won't matter if you don't do it

before Outlook starts. There is no way, using the Outlook item.PrintOut()

method to select or change the printer being used.

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

news:eqxffV0pJHA.3380@TK2MSFTNGP04.phx.gbl...

<snip>
> 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!" to "GreenPrint"?

> V
 
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
<kenslovak@mvps.org> wrote in message

news:u1c9km8pJHA.5880@TK2MSFTNGP05.phx.gbl...

: Outlook only reads the printer to use when it starts up. You can change

the

: system printer as often as you want and it won't matter if you don't do it

: before Outlook starts. There is no way, using the Outlook item.PrintOut()

: method to select or change the printer being used.

:

:

:

:

:

:

:

:

:

:

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

: news:eqxffV0pJHA.3380@TK2MSFTNGP04.phx.gbl...

: <snip
: > 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!" to "GreenPrint"?

:

: > V

:
 
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

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

news:eqxffV0pJHA.3380@TK2MSFTNGP04.phx.gbl...

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

: 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

: >
: >> 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 WshNetwork

: >> 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 ("ENTER YOUR PRINTER NAME HERE!")

: >> 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

: >
: >> 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!"

: to "GreenPrint"?

:

: V

:
 
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
 
"PaulD" <nospam> wrote in message

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

print dialogue box. I just couldn't understand this before.

Thanks again for all your help...

V
 
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

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

news:err7j%23AqJHA.2392@TK2MSFTNGP04.phx.gbl...

: "PaulD" <nospam> wrote in message

: 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

: print dialogue box. I just couldn't understand this before.

:

: Thanks again for all your help...

:

: V

:
 
"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...

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

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,

Outlook 2003. Same as you?

V
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
J Help Please!!! Outlook 2016 - VBA Macro for replying with attachment in meeting invite Outlook VBA and Custom Forms 9
C Newbie needs help with Outlook Macro Outlook VBA and Custom Forms 3
A newb outlook macro help Outlook VBA and Custom Forms 1
F Help with Outlook 2007 Macro Please! Using Outlook 4
S Outlook macro help Using Outlook 10
S Outlook 2003: Help with setting up a macro to reply to selected emails Using Outlook 2
L Help for writing an Outlook 2007 macro Outlook VBA and Custom Forms 7
X Custom icon (not from Office 365) for a macro in Outlook Outlook VBA and Custom Forms 1
C Outlook - Macro to block senders domain - Macro Fix Outlook VBA and Custom Forms 1
J Outlook 365 Outlook Macro to Sort emails by column "Received" to view the latest email received Outlook VBA and Custom Forms 0
M Outlook Macro to save as Email with a file name format : Date_Timestamp_Sender initial_Email subject Outlook VBA and Custom Forms 0
D Outlook 2016 Creating an outlook Macro to select and approve Outlook VBA and Custom Forms 0
S Outlook Macro for [Date][Subject] Using Outlook 1
E Outlook - Macro - send list of Tasks which are not finished Outlook VBA and Custom Forms 3
S Macro for Loop through outlook unread emails Outlook VBA and Custom Forms 2
Witzker Macro to move @domain.xx of a Spammail to Blacklist in Outlook 2019 Outlook VBA and Custom Forms 7
S Macro for other actions - Outlook 2007 Outlook VBA and Custom Forms 23
S Macro to move “Re:” & “FWD:” email recieved the shared inbox to a subfolder in outlook Outlook VBA and Custom Forms 0
S Outlook Macro to send auto acknowledge mail only to new mails received to a specific shared inbox Outlook VBA and Custom Forms 0
S Outlook Macro to move reply mail based on the key word in the subjectline Outlook VBA and Custom Forms 0
M Outlook macro to automate search and forward process Outlook VBA and Custom Forms 6
R Macro Schedule every day in Outlook Using Outlook 0
L Moving emails with similar subject and find the timings between the emails using outlook VBA macro Outlook VBA and Custom Forms 1
N How can I increase/faster outlook VBA Macro Speed ? Using Outlook 2
A Outlook macro to create search folder with mail categories as criteria Outlook VBA and Custom Forms 3
V Outlook Macro to show Flagged messages Outlook VBA and Custom Forms 2
S Macro using .SendUsingAccount only works the first time, after starting Outlook Outlook VBA and Custom Forms 4
M Slow VBA macro in Outlook Outlook VBA and Custom Forms 5
A Forward Outlook Email by Filtering using Macro Rule Outlook VBA and Custom Forms 44
Tanja Östrand Outlook 2016 - Create Macro button to add text in Subject Outlook VBA and Custom Forms 1
D Outlook macro with today's date in subject and paste clipboard in body Outlook VBA and Custom Forms 1
C Outlook Subject Line Macro Outlook VBA and Custom Forms 0
D Macro sending outlook template from Excel list Outlook VBA and Custom Forms 6
P Macro to attach a file in a shared Outlook draft folder Outlook VBA and Custom Forms 2
R Macro to check file name with outlook address book Outlook VBA and Custom Forms 0
Diane Poremsky Use a macro to copy data in Outlook email to Excel workbook Using Outlook 0
W macro to export outlook emails to excel Outlook VBA and Custom Forms 6
J Outlook Macro to Update Sharepoint Excel File Using Outlook 1
Patrick van Berkel Best way to share (and keep up-to-date) Macro's in Outlook 2010 Outlook VBA and Custom Forms 6
L Outlook 2007 - Macro Re Search Using Outlook 16
L Outlook 2007 Macro to Contact From a Field Using Outlook 3
Diane Poremsky Macro to Bulk Import Contacts and vCards into Outlook Using Outlook 0
S Outlook 7 VBA macro for multiple filing Outlook VBA and Custom Forms 1
S Macro in an excel for outlook. Outlook VBA and Custom Forms 2
A Outlook Macro Causing Excel Error Outlook VBA and Custom Forms 0
Diane Poremsky Use a macro to copy data in Outlook email to Excel workbook Using Outlook 0
S Outlook Macro Reply to a message Outlook VBA and Custom Forms 1
Diane Poremsky Macro to Bulk Import vCards into Outlook Using Outlook 0
M Outlook VBA Macro that could retrieve/display the (From, Subject, Date Received) Outlook VBA and Custom Forms 2
Philip Rose Recreating a WORD Editing Macro to use in Outlook VBA Outlook VBA and Custom Forms 4

Similar threads

Back
Top