Peter Loidl
asked on
Auto send receipt in Outlook 2010
Hi there
I auto print attachments in outlook 2010, need to move email where attachments have been printed in to an printed folder and send a receipt for the emails where attachment has been printed and a receipt was requested.
Appreciate some help as I am only an amateur.
I auto print attachments in outlook 2010, need to move email where attachments have been printed in to an printed folder and send a receipt for the emails where attachment has been printed and a receipt was requested.
Appreciate some help as I am only an amateur.
How is the auto-print implemented? Some VBA code in Outlook?
ASKER
Hi Olemo
The code is implemented via VBA in Outlook, the source is the code from Experts Exchange - auto print email attachments in outlook 2010.
Thanks
The code is implemented via VBA in Outlook, the source is the code from Experts Exchange - auto print email attachments in outlook 2010.
Thanks
Best to either post the link to that question, or the code itself, so we can integrate the new requirements.
ASKER
Below is the code I used, I try to save the email to another folder "printed" after the attachment is printed and where a receipt is requested send a receipt.
I really appreciate your help.
I really appreciate your help.
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
Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Sub MessageAndAttachmentProcessor(Item As Outlook.MailItem, _
Optional bolPrintMsg As Boolean, _
Optional bolSaveMsg As Boolean, _
Optional bolPrintAtt As Boolean, _
Optional bolSaveAtt As Boolean, _
Optional bolInsertLink As Boolean, _
Optional strAttFileTypes As String, _
Optional strFolderPath As String, _
Optional varMsgFormat As OlSaveAsType, _
Optional strPrinter As String)
Dim olkAttachment As Outlook.Attachment, _
objFSO As FileSystemObject, _
strMyPath As String, _
strExtension As String, _
strFileName As String, _
strOriginalPrinter As String, _
strLinkText As String, _
strRootFolder As String, _
strTempFolder As String, _
varFileType As Variant, _
intCount As Integer, _
intIndex As Integer, _
arrFileTypes As Variant
Set objFSO = CreateObject("Scripting.FileSystemObject")
strTempFolder = Environ("TEMP") & "\"
If strAttFileTypes = "" Then
arrFileTypes = Array("*")
Else
arrFileTypes = Split(strAttFileTypes, ",")
End If
If bolPrintMsg Or bolPrintAtt Then
If strPrinter <> "" Then
strOriginalPrinter = GetDefaultPrinter()
SetDefaultPrinter strPrinter
End If
End If
If bolSaveMsg Or bolSaveAtt Then
If strFolderPath = "" Then
strRootFolder = Environ("USERPROFILE") & "\My Documents\"
Else
strRootFolder = strFolderPath & IIf(Right(strFolderPath, 1) = "\", "", "\")
End If
End If
If bolSaveMsg Then
Select Case varMsgFormat
Case olTIF
strExtension = “.tif”
Case olTIFF
strExtension = “.tiff”
Case olHTML
strExtension = ".html"
Case olMSG
strExtension = ".msg"
Case olRTF
strExtension = ".rtf"
Case olDoc
strExtension = ".doc"
Case olTXT
strExtension = ".txt"
Case Else
strExtension = ".msg"
End Select
Item.SaveAs strRootFolder & RemoveIllegalCharacters(Item.Subject) & strExtension, varMsgFormat
End If
For intIndex = Item.Attachments.count To 1 Step -1
Set olkAttachment = Item.Attachments.Item(intIndex)
'Print the attachments if requested'
If bolPrintAtt Then
If olkAttachment.Type <> olEmbeddeditem Then
For Each strFileType In arrFileTypes
If (strFileType = "*") Or (LCase(objFSO.GetExtensionName(olkAttachment.FileName)) = LCase(strFileType)) Then
olkAttachment.SaveAsFile strTempFolder & olkAttachment.FileName
ShellExecute 0&, "print", strTempFolder & olkAttachment.FileName, 0&, 0&, 0&
End If
Next
End If
End If
'Save the attachments if requested'
If bolSaveAtt Then
strFileName = olkAttachment.FileName
intCount = 0
Do While True
strMyPath = strRootFolder & strFileName
If objFSO.FileExists(strMyPath) Then
intCount = intCount + 1
strFileName = "Copy (" & intCount & ") of " & olkAttachment.FileName
Else
Exit Do
End If
Loop
olkAttachment.SaveAsFile strMyPath
If bolInsertLink Then
If Item.BodyFormat = olFormatHTML Then
strLinkText = strLinkText & "<a href=""file://" & strMyPath & """>" & olkAttachment.FileName & "</a><br>"
Else
strLinkText = strLinkText & strMyPath & vbCrLf
End If
olkAttachment.Delete
End If
End If
Next
If bolPrintMsg Then
Item.PrintOut
End If
If bolPrintMsg Or bolPrintAtt Then
If strOriginalPrinter <> "" Then
SetDefaultPrinter strOriginalPrinter
End If
End If
If bolInsertLink Then
If Item.BodyFormat = olFormatHTML Then
Item.HTMLBody = Item.HTMLBody & "<br><br>Removed Attachments<br><br>" & strLinkText
Else
Item.Body = Item.Body & vbCrLf & vbCrLf & "Removed Attachments" & vbCrLf & vbCrLf & strLinkText
End If
Item.Save
End If
Set objFSO = Nothing
Set olkAttachment = Nothing
End Sub
Function GetDefaultPrinter() As String
Dim strPrinter As String, _
intReturn As Integer
strPrinter = Space(255)
intReturn = GetProfileString("Windows", ByVal "device", "", strPrinter, Len(strPrinter))
If intReturn Then
strPrinter = UCase(Left(strPrinter, InStr(strPrinter, ",") - 1))
End If
GetDefaultPrinter = strPrinter
End Function
Function RemoveIllegalCharacters(strValue As String) As String
' Purpose: Remove characters that cannot be in a filename from a string.'
' Written: 4/24/2009'
' Author: BlueDevilFan'
' Outlook: All versions'
RemoveIllegalCharacters = strValue
RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "<", "")
RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, ">", "")
RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, ":", "")
RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, Chr(34), "'")
RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "/", "")
RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "\", "")
RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "|", "")
RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "?", "")
RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "*", "")
End Function
Sub SetDefaultPrinter(strPrinterName As String)
Dim objNet As Object
Set objNet = CreateObject("Wscript.Network")
objNet.SetDefaultPrinter strPrinterName
Set objNet = Nothing
End Sub
‘Change MySubroutineName to a unique name on the next line’
Sub MySubroutineName(Item As Outlook.MailItem)
MessageAndAttachmentProcessor Item, P1, P2, P3, P4, P5, P6, P7, P8, P9
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks very much, I'll see if I can get it going and let you know.
Appreciate your time
Peter
Appreciate your time
Peter
ASKER
Hi
I have another question
I would like to monitor 3 email accounts like "LEOs-Order, LEOs-General, leosffhk@leos.com".
and send the printed mails only into the one folder("Root").Folders("Pr inted Mails").
' *** Change path below as required
Dim newMailFolder As Outlook.Folder
Set newMailFolder = Session.Folders("Root").Fo lders("Pri nted Mails")
Thanks for your help again
Peter
I have another question
I would like to monitor 3 email accounts like "LEOs-Order, LEOs-General, leosffhk@leos.com".
and send the printed mails only into the one folder("Root").Folders("Pr
' *** Change path below as required
Dim newMailFolder As Outlook.Folder
Set newMailFolder = Session.Folders("Root").Fo
Thanks for your help again
Peter
This is out of the scope of this question.
You are triggering that somehow already for one account, I assume by having a ItemAdd event handler for a specific MailtItem folder. Just do exactly the same for the other folders then.
You are triggering that somehow already for one account, I assume by having a ItemAdd event handler for a specific MailtItem folder. Just do exactly the same for the other folders then.
ASKER
Thanks again for your help and insight
I am getting an compile error
User-defined type not defined, could you please help me out on this one. The code is below, last line
Thanks
Sub MessageAndAttachmentProces sor(Item As Outlook.MailItem, _
Optional bolPrintMsg As Boolean, _
Optional bolSaveMsg As Boolean, _
Optional bolPrintAtt As Boolean, _
Optional bolSaveAtt As Boolean, _
Optional bolInsertLink As Boolean, _
Optional strAttFileTypes As String, _
Optional strFolderPath As String, _
Optional varMsgFormat As OlSaveAsType, _
Optional strPrinter As String)
Dim olkAttachment As Outlook.Attachment, _
objFSO As FileSystemObject, _
I am getting an compile error
User-defined type not defined, could you please help me out on this one. The code is below, last line
Thanks
Sub MessageAndAttachmentProces
Optional bolPrintMsg As Boolean, _
Optional bolSaveMsg As Boolean, _
Optional bolPrintAtt As Boolean, _
Optional bolSaveAtt As Boolean, _
Optional bolInsertLink As Boolean, _
Optional strAttFileTypes As String, _
Optional strFolderPath As String, _
Optional varMsgFormat As OlSaveAsType, _
Optional strPrinter As String)
Dim olkAttachment As Outlook.Attachment, _
objFSO As FileSystemObject, _
And that compile error does come up only with the changed code, not the original one?
ASKER
Hi
Sorry for the delay. I am trying to reconstruct what I had before to give you a correct answer. Please allow me some more time as I am trying to manage a business the same time.
Regards
Peter
Sorry for the delay. I am trying to reconstruct what I had before to give you a correct answer. Please allow me some more time as I am trying to manage a business the same time.
Regards
Peter
ASKER
Hi
When I looked in to the originaly into setting up outlook the code it was not active and I did not try to run it and I was unable to reproduce the old setup - just wouldn't work for me. Below is the whole code and when I run it it brings up the error as mentioned before.
Thanks
Peter
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
Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Sub MessageAndAttachmentProces sor(Item As Outlook.MailItem, _
Optional bolPrintMsg As Boolean, _
Optional bolSaveMsg As Boolean, _
Optional bolPrintAtt As Boolean, _
Optional bolSaveAtt As Boolean, _
Optional bolInsertLink As Boolean, _
Optional strAttFileTypes As String, _
Optional strFolderPath As String, _
Optional varMsgFormat As OlSaveAsType, _
Optional strPrinter As String)
Dim olkAttachment As Outlook.Attachment, _
objFSO As FileSystemObject, _
strMyPath As String, _
strExtension As String, _
strFileName As String, _
strOriginalPrinter As String, _
strLinkText As String, _
strRootFolder As String, _
strTempFolder As String, _
varFileType As Variant, _
intCount As Integer, _
intIndex As Integer, _
arrFileTypes As Variant
' *** Change path below as required
Dim LeosOrder As Outlook.Folder
Set LeosOrder = Session.Folders("LEOsOrder ").Folders ("Printed Mails")
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
strTempFolder = Environ("TEMP") & "\"
If strAttFileTypes = "" Then
arrFileTypes = Array("*")
Else
arrFileTypes = Split(strAttFileTypes, ",")
End If
If bolPrintMsg Or bolPrintAtt Then
If strPrinter <> "" Then
strOriginalPrinter = GetDefaultPrinter()
SetDefaultPrinter strPrinter
End If
End If
If bolSaveMsg Or bolSaveAtt Then
If strFolderPath = "" Then
strRootFolder = Environ("USERPROFILE") & "\My Documents\"
Else
strRootFolder = strFolderPath & IIf(Right(strFolderPath, 1) = "\", "", "\")
End If
End If
If bolSaveMsg Then
Select Case varMsgFormat
Case olPDF
strExtension = ".pdf"
Case olTIF
strExtension = ".tif"
Case olTIFF
strExtension = ".tiff"
Case olHTML
strExtension = ".html"
Case olMSG
strExtension = ".msg"
Case olRTF
strExtension = ".rtf"
Case olDoc
strExtension = ".doc"
Case olTXT
strExtension = ".txt"
Case Else
strExtension = ".msg"
End Select
Item.SaveAs strRootFolder & RemoveIllegalCharacters(It em.Subject ) & strExtension, varMsgFormat
End If
For intIndex = Item.Attachments.Count To 1 Step -1
Set olkAttachment = Item.Attachments.Item(intI ndex)
'Print the attachments if requested'
If bolPrintAtt Then
If olkAttachment.Type <> olEmbeddeditem Then
For Each strFileType In arrFileTypes
If (strFileType = "*") Or (LCase(objFSO.GetExtension Name(olkAt tachment.F ileName)) = LCase(strFileType)) Then
olkAttachment.SaveAsFile strTempFolder & olkAttachment.FileName
ShellExecute 0&, "print", strTempFolder & olkAttachment.FileName, 0&, 0&, 0&
End If
Next
End If
End If
'Save the attachments if requested'
If bolSaveAtt Then
strFileName = olkAttachment.FileName
intCount = 0
Do While True
strMyPath = strRootFolder & strFileName
If objFSO.FileExists(strMyPat h) Then
intCount = intCount + 1
strFileName = "Copy (" & intCount & ") of " & olkAttachment.FileName
Else
Exit Do
End If
Loop
olkAttachment.SaveAsFile strMyPath
If bolInsertLink Then
If Item.BodyFormat = olFormatHTML Then
strLinkText = strLinkText & "<a href=""file://" & strMyPath & """>" & olkAttachment.FileName & "</a><br>"
Else
strLinkText = strLinkText & strMyPath & vbCrLf
End If
olkAttachment.Delete
End If
End If
Next
If bolPrintMsg Then
Item.PrintOut
End If
If bolPrintMsg Or bolPrintAtt Then
If strOriginalPrinter <> "" Then
SetDefaultPrinter strOriginalPrinter
End If
End If
If bolInsertLink Then
If Item.BodyFormat = olFormatHTML Then
Item.HTMLBody = Item.HTMLBody & "<br><br>Removed Attachments<br><br>" & strLinkText
Else
Item.Body = Item.Body & vbCrLf & vbCrLf & "Removed Attachments" & vbCrLf & vbCrLf & strLinkText
End If
Item.Save
End If
' *** Hardcoded: - move mail to other folder, and trigger receipt if requested in the mail
Item.Move LeosOrder
Item.UnRead = False
Set objFSO = Nothing
Set olkAttachment = Nothing
End Sub
‘Change MySubroutineName to a unique name on the next line'
Sub LeosOrder(Item As Outlook.MailItem)
MessageAndAttachmentProces sor Item, True
End Sub
When I looked in to the originaly into setting up outlook the code it was not active and I did not try to run it and I was unable to reproduce the old setup - just wouldn't work for me. Below is the whole code and when I run it it brings up the error as mentioned before.
Thanks
Peter
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
Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Sub MessageAndAttachmentProces
Optional bolPrintMsg As Boolean, _
Optional bolSaveMsg As Boolean, _
Optional bolPrintAtt As Boolean, _
Optional bolSaveAtt As Boolean, _
Optional bolInsertLink As Boolean, _
Optional strAttFileTypes As String, _
Optional strFolderPath As String, _
Optional varMsgFormat As OlSaveAsType, _
Optional strPrinter As String)
Dim olkAttachment As Outlook.Attachment, _
objFSO As FileSystemObject, _
strMyPath As String, _
strExtension As String, _
strFileName As String, _
strOriginalPrinter As String, _
strLinkText As String, _
strRootFolder As String, _
strTempFolder As String, _
varFileType As Variant, _
intCount As Integer, _
intIndex As Integer, _
arrFileTypes As Variant
' *** Change path below as required
Dim LeosOrder As Outlook.Folder
Set LeosOrder = Session.Folders("LEOsOrder
Set objFSO = CreateObject("Scripting.Fi
strTempFolder = Environ("TEMP") & "\"
If strAttFileTypes = "" Then
arrFileTypes = Array("*")
Else
arrFileTypes = Split(strAttFileTypes, ",")
End If
If bolPrintMsg Or bolPrintAtt Then
If strPrinter <> "" Then
strOriginalPrinter = GetDefaultPrinter()
SetDefaultPrinter strPrinter
End If
End If
If bolSaveMsg Or bolSaveAtt Then
If strFolderPath = "" Then
strRootFolder = Environ("USERPROFILE") & "\My Documents\"
Else
strRootFolder = strFolderPath & IIf(Right(strFolderPath, 1) = "\", "", "\")
End If
End If
If bolSaveMsg Then
Select Case varMsgFormat
Case olPDF
strExtension = ".pdf"
Case olTIF
strExtension = ".tif"
Case olTIFF
strExtension = ".tiff"
Case olHTML
strExtension = ".html"
Case olMSG
strExtension = ".msg"
Case olRTF
strExtension = ".rtf"
Case olDoc
strExtension = ".doc"
Case olTXT
strExtension = ".txt"
Case Else
strExtension = ".msg"
End Select
Item.SaveAs strRootFolder & RemoveIllegalCharacters(It
End If
For intIndex = Item.Attachments.Count To 1 Step -1
Set olkAttachment = Item.Attachments.Item(intI
'Print the attachments if requested'
If bolPrintAtt Then
If olkAttachment.Type <> olEmbeddeditem Then
For Each strFileType In arrFileTypes
If (strFileType = "*") Or (LCase(objFSO.GetExtension
olkAttachment.SaveAsFile strTempFolder & olkAttachment.FileName
ShellExecute 0&, "print", strTempFolder & olkAttachment.FileName, 0&, 0&, 0&
End If
Next
End If
End If
'Save the attachments if requested'
If bolSaveAtt Then
strFileName = olkAttachment.FileName
intCount = 0
Do While True
strMyPath = strRootFolder & strFileName
If objFSO.FileExists(strMyPat
intCount = intCount + 1
strFileName = "Copy (" & intCount & ") of " & olkAttachment.FileName
Else
Exit Do
End If
Loop
olkAttachment.SaveAsFile strMyPath
If bolInsertLink Then
If Item.BodyFormat = olFormatHTML Then
strLinkText = strLinkText & "<a href=""file://" & strMyPath & """>" & olkAttachment.FileName & "</a><br>"
Else
strLinkText = strLinkText & strMyPath & vbCrLf
End If
olkAttachment.Delete
End If
End If
Next
If bolPrintMsg Then
Item.PrintOut
End If
If bolPrintMsg Or bolPrintAtt Then
If strOriginalPrinter <> "" Then
SetDefaultPrinter strOriginalPrinter
End If
End If
If bolInsertLink Then
If Item.BodyFormat = olFormatHTML Then
Item.HTMLBody = Item.HTMLBody & "<br><br>Removed Attachments<br><br>" & strLinkText
Else
Item.Body = Item.Body & vbCrLf & vbCrLf & "Removed Attachments" & vbCrLf & vbCrLf & strLinkText
End If
Item.Save
End If
' *** Hardcoded: - move mail to other folder, and trigger receipt if requested in the mail
Item.Move LeosOrder
Item.UnRead = False
Set objFSO = Nothing
Set olkAttachment = Nothing
End Sub
‘Change MySubroutineName to a unique name on the next line'
Sub LeosOrder(Item As Outlook.MailItem)
MessageAndAttachmentProces
End Sub
Sorry, I just can't understand you. Did you run the original code successfully, or did you not? It is a big difference to get code running from start versus getting small changes applied to already working code.
ASKER
Sorry, no I didn't get it going, just would not work for me.
As I told you already, the code needs to be triggered, which again requires to write some more code. Do you have that part (something about ItemAdd)?
ASKER
Hi
I thought the code below and creating a rule to call was the way to go. No I do not have another code.
Thank you
‘Change MySubroutineName to a unique name on the next line'
Sub LeosOrder(Item As Outlook.MailItem)
MessageAndAttachmentProces sor Item, True
End Sub
I thought the code below and creating a rule to call was the way to go. No I do not have another code.
Thank you
‘Change MySubroutineName to a unique name on the next line'
Sub LeosOrder(Item As Outlook.MailItem)
MessageAndAttachmentProces
End Sub
Ok, you use a rule and a function calling the generic function. That is fine, and another way to trigger execution.
So, the code is executed, but does nothing, or do you get an error, or what is the outcome?
So, the code is executed, but does nothing, or do you get an error, or what is the outcome?
ASKER
I got the error I posted at the top
"at the top" means in-midst of all the posts, namely http:#a40791169 , a compile error regarding FileSystemObject ?
ASKER
FileSystemObject is correct,
Dim olkAttachment As Outlook.Attachment, _
objFSO As FileSystemObject, _
Dim olkAttachment As Outlook.Attachment, _
objFSO As FileSystemObject, _
As it seems, this is the only place early binding is used, and it isn't required, so we just remove that. Replace with
Dim olkAttachment As Outlook.Attachment, _
objFSO, _
Dim olkAttachment As Outlook.Attachment, _
objFSO, _