Outlook attachment save & print
Hello everyone,
I need help on the following Micro, it works but I need to add a second email address that needs to be saved in same directory and print as they arrive. Thank you
Code:
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
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim Ns As Outlook.NameSpace
Dim Folder As Outlook.MAPIFolder
Set Ns = Application.GetNamespace("MAPI")
Set Folder = Ns.GetDefaultFolder(olFolderInbox)
Set Items = Folder.Items
End Sub
Private Sub Items_ItemAdd(ByVal Item As Object)
If TypeOf Item Is Outlook.MailItem Then
PrintAttachments Item
End If
End Sub
Private Sub PrintAttachments(oMail As Outlook.MailItem)
On Error Resume Next
Dim colAtts As Outlook.Attachments
Dim oAtt As Outlook.Attachment
Dim sFile As String
Dim sDirectory As String
Dim sFileType As String
' Save attachments to
sDirectory = "C:\Attachments\"
Set colAtts = oMail.Attachments
' email address attachment that needs to be saved
If colAtts.Count And oMail.SenderEmailAddress ="email address here" Then
For Each oAtt In colAtts
'The code looks at the last 4 characters,
'including the period and will work as long as you use 4 characters in each extension we want to check.
sFileType = LCase$(Right$(oAtt.FileName, 4))
Select Case sFileType
' Add additional file types below
Case "xlsx", "docx", ".pdf", ".doc", ".xls"
sFile = sDirectory & oAtt.FileName
oAtt.SaveAsFile sFile
'Print saved attachements
ShellExecute 0, "print", sFile, vbNullString, vbNullString, 0
End Select
Next
End If
End Sub
File extension with more than one dot
Hi,
i´v been using the vba code below with sucess. however an issue ocourred. if i receive a file with more than one dot ex: 1.23.pdf the vba code does note recocnize it as a pdf file, and doe not print it. iv tried add aditional file extension but no sucess. can you please help me?
of corse if i rename the file it works but its nott a solution for me
Thans in advance
Carlos Pires
Quote:
Originally Posted by
gmayor
OK, in that case replace the section with the following, which I think should do the job
Code:
If colAtts.Count Then
Select Case oMail.SenderEmailAddress
Case "email address here"
sDirectory = "C:\Path\Folder1\"
Case "another email address here"
sDirectory = "C:\Path\Folder2\"
Case Else: Exit Sub
End Select
For Each oAtt In colAtts
'The code looks at the last 4 characters,
'including the period and will work as long as you use 4 characters in each extension we want to check.
sFileType = LCase$(Right$(oAtt.Filename, 4))
Select Case sFileType
' Add additional file types below
Case "xlsx", "docx", ".pdf", ".doc", ".xls"
sFile = sDirectory & oAtt.Filename
oAtt.SaveAsFile sFile
'Print saved attachements
ShellExecute 0, "print", sFile, vbNullString, vbNullString, 0
End Select
Next
End If