Automated Attachment Printing Outlook 2010
Hi All
My problem is this, i'm trying to find a solution for our telesales department in automated email/attachment printing. They recieve orders via email, most of which come with attachments and we need to find a way of printing these automatically as they
come in as it is now becoming a fulltime job. I have looked at addins etc but feel a macro might be the way forward, problem is my programming skills are next to zero.
Can anybody suggest something?
edit* it might be worth noting that the attachments come in a variety of file types = .pdf, .rtf, .xls, .xlsx, .html, .jpg
July 20th, 2011 12:20pm
You could use VBA+a rule.
Add the following code to ThisOutlookSession module.
Sub LSPrint(Item As Outlook.MailItem)
On Error GoTo OError
'detect Temp
Dim oFS As FileSystemObject
Dim sTempFolder As String
Set oFS = New FileSystemObject
'Temporary Folder Path
sTempFolder = oFS.GetSpecialFolder(TemporaryFolder)
'creates a special temp folder
cTmpFld = sTempFolder & "\OETMP" & Format(Now, "yyyymmddhhmmss")
MkDir (cTmpFld)
'save & print
Dim oAtt As Attachment
For Each oAtt In Item.Attachments
FileName = oAtt.FileName
FullFile = cTmpFld & "\" & FileName
'save attachment
oAtt.SaveAsFile (FullFile)
'prints attachment
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(0)
Set objFolderItem = objFolder.ParseName(FullFile)
objFolderItem.InvokeVerbEx ("print")
Next oAtt
'Cleanup
If Not oFS Is Nothing Then Set oFS = Nothing
If Not objFolder Is Nothing Then Set objFolder = Nothing
If Not objFolderItem Is Nothing Then Set objFolderItem = Nothing
If Not objShell Is Nothing Then Set objShell = Nothing
OError:
If Err <> 0 Then
MsgBox Err.Number & " - " & Err.Description
Err.Clear
End If
Exit Sub
End Sub
Add a reference to Microsoft Scripting Runtime from Tools\References.
Now, create a rule for all incoming messages from a certain person (or from who you receive those attachments) and choose run a script action.
-
Marked as answer by
William Zhou CHNModerator
Thursday, July 28, 2011 2:10 AM
-
Edited by
Alexandru Dionisie
Wednesday, October 23, 2013 1:49 PM
Added code tags
July 21st, 2011 6:38am
You could use VBA+a rule.
Add the following code to ThisOutlookSession module.
Sub LSPrint(Item As Outlook.MailItem)
On Error GoTo OError
'detect Temp
Dim oFS As FileSystemObject
Dim sTempFolder As String
Set oFS = New FileSystemObject
'Temporary Folder Path
sTempFolder = oFS.GetSpecialFolder(TemporaryFolder)
'creates a special temp folder
cTmpFld = sTempFolder & "\OETMP" & Format(Now, "yyyymmddhhmmss")
MkDir (cTmpFld)
'save & print
Dim oAtt As Attachment
For Each oAtt In Item.Attachments
FileName = oAtt.FileName
FullFile = cTmpFld & "\" & FileName
'save attachment
oAtt.SaveAsFile (FullFile)
'prints attachment
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(0)
Set objFolderItem = objFolder.ParseName(FullFile)
objFolderItem.InvokeVerbEx ("print")
Next oAtt
'Cleanup
If Not oFS Is Nothing Then Set oFS = Nothing
If Not objFolder Is Nothing Then Set objFolder = Nothing
If Not objFolderItem Is Nothing Then Set objFolderItem = Nothing
If Not objShell Is Nothing Then Set objShell = Nothing
OError:
If Err <> 0 Then
MsgBox Err.Number & " - " & Err.Description
Err.Clear
End If
Exit Sub
End Sub
Add a reference to Microsoft Scripting Runtime from Tools\References.
Now, create a rule for all incoming messages from a certain person (or from who you receive those attachments) and choose run a script action.
-
Marked as answer by
William Zhou CHNModerator
Thursday, July 28, 2011 2:10 AM
-
Edited by
Alexandru Dionisie
Wednesday, October 23, 2013 1:49 PM
Added code tags
July 21st, 2011 6:38am
You could use VBA+a rule.
Add the following code to ThisOutlookSession module.
Sub LSPrint(Item As Outlook.MailItem)
On Error GoTo OError
'detect Temp
Dim oFS As FileSystemObject
Dim sTempFolder As String
Set oFS = New FileSystemObject
'Temporary Folder Path
sTempFolder = oFS.GetSpecialFolder(TemporaryFolder)
'creates a special temp folder
cTmpFld = sTempFolder & "\OETMP" & Format(Now, "yyyymmddhhmmss")
MkDir (cTmpFld)
'save & print
Dim oAtt As Attachment
For Each oAtt In Item.Attachments
FileName = oAtt.FileName
FullFile = cTmpFld & "\" & FileName
'save attachment
oAtt.SaveAsFile (FullFile)
'prints attachment
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(0)
Set objFolderItem = objFolder.ParseName(FullFile)
objFolderItem.InvokeVerbEx ("print")
Next oAtt
'Cleanup
If Not oFS Is Nothing Then Set oFS = Nothing
If Not objFolder Is Nothing Then Set objFolder = Nothing
If Not objFolderItem Is Nothing Then Set objFolderItem = Nothing
If Not objShell Is Nothing Then Set objShell = Nothing
OError:
If Err <> 0 Then
MsgBox Err.Number & " - " & Err.Description
Err.Clear
End If
Exit Sub
End Sub
Add a reference to Microsoft Scripting Runtime from Tools\References.
Now, create a rule for all incoming messages from a certain person (or from who you receive those attachments) and choose run a script action.
-
Marked as answer by
William Zhou CHNModerator
Thursday, July 28, 2011 2:10 AM
-
Edited by
Alexandru Dionisie
Wednesday, October 23, 2013 1:49 PM
Added code tags
July 21st, 2011 6:38am
I will give this a go, thank you!
July 21st, 2011 9:12am
Hi Alexandru,
how do i use that vba printing current page. i dont want to print whole dokument
October 4th, 2011 1:51pm
For a few VBA scripts are nothing new, However, this should be an available add-in from Microsoft. This script has too many uses for businesses to mention. Whether its creating a hard-copy for shipping, order handling or a digital copy in a network folder,
automatically printing attachments from trusted sources would be very helpful. Microsoft needs to answer the question "Is Microsoft's software designed to be usefule and/or helpful?"
July 26th, 2012 7:45pm
"You could use VBA+a rule.
Add the following code to ThisOutlookSession module."
Where to you go to add VBA script?
July 26th, 2012 7:48pm
In ThisOutlookSession module.
July 27th, 2012 5:58am
Another one of the functions not in the default menu.... arrgg!
Got the module installed and set for the desired emails. I must have read it wrong at the OP's post. I was hoping this would only print the attachment. The body of the email's I am receiving is a generic cover that I do not need at all.
Is there a different VBA or mod so that Only the attachment will print?
August 14th, 2012 9:35pm
Thanks. I have already it tried it, but no success. I have some things to take care that I can't set aside some real time for a week or two to investigate what's going on for me.
My Environment is W7Pro, Office 2010. I know there have been some underlying changes by MS that may be the issue, and your posting (linked page) is dated from 2006...
Also, the main document type I am looking to have print are PDF's....
August 15th, 2012 1:58pm
Page age is not a big deal, many macros written for old versions still work with little or no tweaking. That macro has one limitation though - it's for 32bit only (the first couple of lines need updated for 64bit).
For pdf and other file types, as long as you have an application installed, you just need to (or change) the file types in the Case line.
August 15th, 2012 8:46pm
As an FYI, the code sample at vboffice.net works with Outlook 2013 32-bit, so it should work just fine with Outlook 2010 32-bit. (For 4 character extensions, you need to change the 4 to a 5 where it checks the filename.)
August 15th, 2012 9:06pm
You could use VBA+a rule.
Add the following code to ThisOutlookSession module.
VBA:
Sub LSPrint(Item As Outlook.MailItem)
On Error GoTo OError
'detect Temp
Dim oFS As FileSystemObject
Dim sTempFolder As String
Set oFS = New FileSystemObject
'Temporary Folder Path
sTempFolder = oFS.GetSpecialFolder(TemporaryFolder)
'creates a special temp folder
cTmpFld = sTempFolder & "\OETMP" & Format(Now, "yyyymmddhhmmss")
MkDir (cTmpFld)
'save & print
Dim oAtt As Attachment
For Each oAtt In Item.Attachments
FileName = oAtt.FileName
FullFile = cTmpFld & "\" & FileName
'save attachment
oAtt.SaveAsFile (FullFile)
'prints attachment
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(0)
Set objFolderItem = objFolder.ParseName(FullFile)
objFolderItem.InvokeVerbEx ("print")
Next oAtt
'Cleanup
If Not oFS Is Nothing Then Set oFS = Nothing
If Not objFolder Is Nothing Then Set objFolder = Nothing
If Not objFolderItem Is Nothing Then Set objFolderItem = Nothing
If Not objShell Is Nothing Then Set objShell = Nothing
OError:
If Err <> 0 Then
MsgBox Err.Number & " - " & Err.Description
Err.Clear
End If
Exit Sub
End Sub
Add a reference to Microsoft Scripting Runtime from Tools\References.
Now, create a rule for all incoming messages from a certain person (or from who you receive those attachments) and choose run a script action.
This is a fantastic solution, I followed your instructions and found its worked a dream, well done thank you.
additional help for setting up VBA Scripting:
http://www.slipstick.com/developer/how-to-use-outlooks-vba-editor/
Regards,
Keyth
October 22nd, 2013 7:27am
I'm glad that it worked. :)
October 23rd, 2013 1:49pm
I'm glad that it worked. :)
i know this was answered and this is a amazing script. i use it myself also now. but i need it sightly modified if possible. i print pdf's alot however when they are scanned they are on 8 1/2 by 13 paper and i am only able to print normal
8 1/2 by 11. so when it prints it is losing 2 inches of the doc. is there a way that it can shrink to fit?
October 31st, 2013 12:42am
Did the AcrobatPrint macro not work?
October 31st, 2013 1:20am
No, i couldn't get it to work, i tried but it wanted to do every email. i need to be able to use a rule and tell it to use certain subjects and this script is more convient for it. but im still open for ideas
October 31st, 2013 11:23am
You need to merge the two - get the
AcrobatePrint sub then tweak the code above to something like this - replace the block between saving the file and moving on to the next attachment.
' at top
Dim sFileType As String
' replace code block
oAtt.SaveAsFile (FullFile)
sFileType = LCase$(right$(oAtt.FileName, 4))
Select Case sFileType
' Add additional file types below
' use shell to print using default app
Case ".xls", ".doc", "docx"
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(0)
Set objFolderItem = objFolder.ParseName(FullFile)
objFolderItem.InvokeVerbEx ("print")
' Print PDF using AcrobatPrint
Case ".pdf"
AcrobatPrint FullFile, "All"
End Select
Next oAtt
October 31st, 2013 5:09pm
here is my updated code:
Sub LSPrint(Item As Outlook.MailItem)
On Error GoTo OError
'detect Temp
Dim oFS As FileSystemObject
Dim sTempFolder As String
Dim sFileType As String
Set oFS = New FileSystemObject
'Temporary Folder Path
sTempFolder = oFS.GetSpecialFolder(TemporaryFolder)
'creates a special temp folder
cTmpFld = sTempFolder & "\OETMP" & Format(Now, "yyyymmddhhmmss")
MkDir (cTmpFld)
'save & print
Dim oAtt As Attachment
For Each oAtt In Item.Attachments
FileName = oAtt.FileName
fullfile = cTmpFld & "\" & FileName
'save attachment
sFileType = LCase$(Right$(oAtt.FileName, 4))
Select Case sFileType
' Add additional file types below
' use shell to print using default app
Case ".xls", ".doc", "docx"
'prints attachment
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(0)
Set objFolderItem = objFolder.ParseName(fullfile)
objFolderItem.InvokeVerbEx ("print")
' Print PDF using AcrobatPrint
Case ".pdf"
AcrobatPrint fullfile, "All"
End Select
Next oAtt
'Cleanup
If Not oFS Is Nothing Then Set oFS = Nothing
If Not objFolder Is Nothing Then Set objFolder = Nothing
If Not objFolderItem Is Nothing Then Set objFolderItem = Nothing
If Not objShell Is Nothing Then Set objShell = Nothing
OError:
If Err <> 0 Then
MsgBox Err.Number & " - " & Err.Description
Err.Clear
End If
Exit Sub
End Sub
when i run it i am getting a compile error:
ByRef argument type mismatch
can you please help again im sorry
October 31st, 2013 5:57pm
sorry about this,
the compile error is on this line
AcrobatPrint fullfile, "All
and it highlights fullfile
October 31st, 2013 5:58pm
oAtt.SaveAsFile (FullFile) is missing
and the acrobat sub uses filename - your macro uses FullFile - they need to match. Change filename to FullFile - i see it twice in the Acrobat code.
and finally, it worked when i added Dim FullFile As String to the top of the LSPrint macro.
October 31st, 2013 7:25pm
Thank you for this code.. however when I follow the steps I get "Object Required (Error 424)" am I missing something? This is exactly what I need to happen in my office.. our environment is Win 7 Pro, Office 2010 32-bit SP2
Thank you in advance..
November 20th, 2013 6:20pm
I have tried the code and it works great however in my case I would want to the script to auto print attachments from a Shared Mailbox setup in my outlook. I tried setting up the Shared Mailbox as a second email account and configured the rule to select
the 2nd email account but it doesn't work. Any suggestions?
December 10th, 2013 5:42pm
I have the same problem - same environment.
Thanks
December 19th, 2013 6:43pm
Thanks Alexandru. This was exactly what I was looking for in OL2003. No need for any middleware now.
Take care,
Chris
Blue Book Services, Inc.
June 19th, 2014 2:05pm
Just take Print Tools for Outlook from MAPILab: http://www.mapilab.com/outlook/print_tools/
WBR, Aleksandr
July 7th, 2014 7:53am
I keep getting the error
Compile error:
User-defined type not defined
What am i doing wrong??
September 11th, 2014 8:31am
I'm guessing the error is because of this:
Dim oFS As FileSystemObject
You need to set a reference to scripting runtime in Tools, References or use this to replace the dim ofs and set ofs lines in the code
Dim oFS As Object
Set oFS = CreateObject("Scripting.FileSystemObject")
September 11th, 2014 3:25pm
Diane,
Hi, I was hoping you could help give me a tip on where to add the ptrdeclare into the script to make it work with 64 bit outlook? I have been reading up on it but just cant work it out, i am new to VB Scripting (evidently)
Thanks
Roy
August 3rd, 2015 6:19am
replace the word Declare with Declare PtrSafe (at the top of Michael's code sample)
August 3rd, 2015 2:58pm
Hello Alexandru,
Thank you for sharing this great piece of code. I am having problems where the code is only printing the message from the e-mail but not the "attachment". I am interested in printing the attachment every time it is sent to me. Can you help?
August 11th, 2015 7:44am
Have you done this:
"Now, create a rule for all incoming messages from a certain person (or from who you receive those attachments) and choose run a script action.Now, create a rule for all incoming messages from a certain person (or from who you receive those attachments)
and choose run a script action." ?
Without that rule you can't print the attachment.
August 11th, 2015 11:25am
I am using XP, it will launch Adobe and print the PDF attachment but also launches Windows Print program. Any way to prevent this?
August 22nd, 2015 3:01pm