A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Outlook » General Discussion
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Outlook macro help please



 
 
Thread Tools Display Modes
  #11  
Old March 17th, 2009, 06:42 PM posted to microsoft.public.outlook.general
Victor Delta[_2_]
external usenet poster
 
Posts: 245
Default Outlook macro help please

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

  #12  
Old March 17th, 2009, 09:14 PM posted to microsoft.public.outlook.general
Victor Delta[_2_]
external usenet poster
 
Posts: 245
Default Outlook macro help please

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

  #13  
Old March 18th, 2009, 01:00 PM posted to microsoft.public.outlook.general
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 368
Default Outlook macro help please

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.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Victor Delta" wrote in message
...
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


  #14  
Old March 18th, 2009, 03:45 PM posted to microsoft.public.outlook.general
PaulD
external usenet poster
 
Posts: 25
Default Outlook macro help please

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

"Ken Slovak - [MVP - Outlook]" wrote in message
...
: 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.
:
: --
: Ken Slovak
: [MVP - Outlook]
: http://www.slovaktech.com
: Author: Professional Programming Outlook 2007.
: Reminder Manager, Extended Reminders, Attachment Options.
: http://www.slovaktech.com/products.htm
:
:
: "Victor Delta" wrote in message
: ...
: 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
:


  #15  
Old March 18th, 2009, 03:51 PM posted to microsoft.public.outlook.general
PaulD
external usenet poster
 
Posts: 25
Default Outlook macro help please

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


  #16  
Old March 18th, 2009, 05:15 PM posted to microsoft.public.outlook.general
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 368
Default Outlook macro help please

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.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


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


  #17  
Old March 18th, 2009, 09:22 PM posted to microsoft.public.outlook.general
Victor Delta[_2_]
external usenet poster
 
Posts: 245
Default Outlook macro help please

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

  #18  
Old March 19th, 2009, 02:00 PM posted to microsoft.public.outlook.general
PaulD
external usenet poster
 
Posts: 25
Default Outlook macro help please

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


  #19  
Old March 19th, 2009, 09:10 PM posted to microsoft.public.outlook.general
Victor Delta[_2_]
external usenet poster
 
Posts: 245
Default Outlook macro help please

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

  #20  
Old March 20th, 2009, 06:17 PM posted to microsoft.public.outlook.general
Victor Delta[_2_]
external usenet poster
 
Posts: 245
Default Outlook macro help please

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

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 02:23 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.