PDA

View Full Version : Printing to a specific printer in OL2003


PhoenixTekBC
December 17th, 2005, 11:03 PM
I have a client who wants to have a seperate button in his Outlook 2003 that when depressed, prints an email to a specific printer, not his default printer. So in essence, in addition to the printer icon that is set to print to the default printer, he wants an additional printer icon that prints to another printer on the network.

I have successfuly accomplished this in Word with a macro, but in Office it does not work. I have read other people looking to do this and also having the same issue I am having.

There's something in the way that Outlook handles printing. I've heard people say "export to a word doc" and some other ideas, but that's hogwash. Is there a way to do this? Is there macro code that I'm not aware of perhaps? Has anyone been successful in implementing something like this in Outlook?

PhoenixTekBC
December 18th, 2005, 08:45 PM
anyone? oracle128? :)

oracle128
December 19th, 2005, 04:49 AM
I read this one before, but I didn't want to say anything yet unless anyone had an answer; I don't think Outlook's VB interpreter is as powerful as the other Office applications. I don't know why, I just remember reading that somewhere. But I'll try working through this anyway.

Do you get any errors from the code? Also, ensure you have the Microsoft Outlook Object Library selected in References (in the VB Editor, Tools->References).

PhoenixTekBC
December 20th, 2005, 02:10 AM
K Cool. If you can help that'd be great!

oracle128
December 20th, 2005, 02:05 PM
K Cool. If you can help that'd be great!OK, post your code if you want, and explain what errors you get and what line it points to, or what it does wrong/not at all.

PhoenixTekBC
December 20th, 2005, 02:51 PM
http://mondaynightmayhemarchives.com/images/img/macro_error.jpg

oracle128
December 20th, 2005, 05:10 PM
OK, after a lot of online research, I had a look in the Outlook VBA reference help file, and I totally found the problem. Outlook's 'Application' object isn't like the other Office apps. It doesn't contain the PrintOut method, or a lot of others that the other Office apps do have. In fact, it's pretty much useless. What you need to do is use the MailItem object to print from (this isn't the code to use, keep reading):Dim sCurrentPrinter As String
Dim objItem As Outlook.MailItem
sCurrentPrinter = ActivePrinter
ActivePrinter = "HP Laserjet 4L"
Set objItem = Outlook.ActiveInspector.CurrentItem
objItem.PrintOut
ActivePrinter = sCurrentPrinterNote that it prints from the currently active MailItem object, and also that there is no longer a Filename parameter for the PrintOut method (in fact, all of Outlook's object's PrintOut methods do not accept any parameters). This works perfectly for emails, but it requires a slight modification if your client wants to print more than just emails (eg. contacts or calendar info), use this (now compacted):Dim sCurrentPrinter As String
sCurrentPrinter = ActivePrinter
ActivePrinter = "HP Laserjet 4L"
Outlook.ActiveInspector.CurrentItem.PrintOut
ActivePrinter = sCurrentPrinterSince, as far as I can tell, all of Outlooks regular objects have the PrintOut method (emails, contacts, appointments, tasks, etc), this will work with all of those objects, however you will probably want to put in some error handling, in case the active object type isn't one that has the method. I'll leave that part to you. Also ensure you have some error handling in case that printer isn't found; VB errors are not user friendly at all.

oracle128
December 20th, 2005, 06:01 PM
I have read other people looking to do this and also having the same issue I am having.
...
I've heard people say "export to a word doc" and some other ideas, but that's hogwash.BTW, feel free to link to this thread on those sites with similar problems.

PhoenixTekBC
December 21st, 2005, 12:14 AM
On this line:

"Outlook.ActiveInspector.CurrentItem.PrintOut"

I get an error that says

"Object Variable or With Block variable not set" on the last macro code you posted.

I'm no programmer, I need help with this stuff becuase I don't know how to do any of it..

oracle128
December 21st, 2005, 05:51 AM
Are you running the macro code without having an email, contact, appointment etc open? Because that error is what you get when an object type attempts to use the PrintOut method when it doesn't have it - which is why you should put in error handling. Try this:On Error GoTo errorhandler

Dim sCurrentPrinter As String
Dim sNewPrinter As String
sCurrentPrinter = ActivePrinter
sNewPrinter = "HP Laserjet 4L"
ActivePrinter = sNewPrinter
Outlook.ActiveInspector.CurrentItem.PrintOut
ActivePrinter = sCurrentPrinter
Exit Sub

errorhandler:
Select Case Err.Number
Case 91
MsgBox ("Select an Email, Task, Contact or Appointment to print from.")
Case 5216
MsgBox ("Printer " & sNewPrinter & " could not be found.")
Case Else
MsgBox ("Error, could not print.")
End Select

PhoenixTekBC
December 21st, 2005, 02:52 PM
Cool - that prints to my default printer, not to another one however that I specify in "sNewPrinter".

It just keeps printing to the default.

oracle128
December 21st, 2005, 05:34 PM
Well, it's printing at least, that's a start...
Upon further inspection, it turns out the Outlook Application object doesn't have the ActivePrinter property, like the other Office apps do. I know, stupid right? I've searched all over, and I'm making enquiries, but so far I've got nothing; no explanation on why it doesn't have it, not even any mention of the fact it's mysteriously not present only in Outlook. I did however find another way of setting the system's default printer, and have integrated that into the code:On Error GoTo errorhandler

Dim sCurrentPrinter As String
Dim sNewPrinter As String
Dim wrdApp As Object
Dim w As New WshNetwork

Set wrdApp = CreateObject("Word.Application")

sCurrentPrinter = wrdApp.activeprinter
sCurrentPrinter = Left$(sCurrentPrinter, InStr(sCurrentPrinter, " on ") - 1)
wrdApp.Quit

sNewPrinter = "HP Laserjet 4L"
w.SetDefaultPrinter (sNewPrinter)
Outlook.ActiveInspector.CurrentItem.PrintOut
w.SetDefaultPrinter (sCurrentPrinter)
Exit Sub

errorhandler:
Select Case Err.Number
Case 91
MsgBox ("Select an Email, Task, Contact or Appointment to print from.")
Case -2147352567
MsgBox ("Printer " & sNewPrinter & " could not be found.")
Case Else
MsgBox ("Error, could not print.")
End SelectHopefully that works now...

You also need to create a reference to "Windows Script Host Object Model".

As you can see, it creates an instance of Word.Application to get the current printer; I couldn't find any code which would do so anywhere near as easily, if at all (remembering that VB for Applications isn't as powerful as regular VB). I'm guessing the user of this macro would probably need Word installed for it to work (or Excel, if you change the object reference where it's obvious), but if they have Outlook installed, there's a good chance they'd have Word to, so I figure it's an acceptable side-affect.

PhoenixTekBC
December 21st, 2005, 07:14 PM
Wow - thanks so much for your time. I will try later. Where do you get all this time to do this for me??

I feel bad your spending so much time on this. :hmm:

PhoenixTekBC
December 21st, 2005, 10:39 PM
I'm getting a compile error on "Dim w As New WshNetwork". No big deal though, I'm about to tell the customer it's not possible. It's not worth all the time we're (you) putting into this.

oracle128
December 22nd, 2005, 04:54 AM
You also need to create a reference to "Windows Script Host Object Model".That would be why you're getting the error on that line. As far as I can tell, it definitely changes the default printer, no question. However, it still doesn't print from the correct printer. Either it's too fast that it doesn't register the change of printer default and sends it to the initial default printer (I tried adding a time delay with an empty for loop, but it wasn't delaying much at all and made no difference to the outcome), or changing the system default printer is not the same as changing the active printer within the app, and thus the default printer is changed, but Outlook's active printer remains the same. *sigh*

It looks like it really is impossible, which is unfortunate. I'm sure it would be possible to programmatically export to an instance of a Word document, print that, then erase it. Setting the active printer and printing would be easier to do that way, by manipulating the Word.Application reference, but as far as exporting the printable content from the Outlook object (which would vary in layout from email, appointment, task, etc), that may be just as impossible, if not extremely difficult.

Maybe Office 12.0 will introduce the same VBA functionality in Outlook as is accessible in the other Office apps; I don't see any reason why it should miss out on some of the basic features the others have.

Sorry to let you down. And yes, I have way too much time on my hands.