Outlook VBA to Save Multiple Attachments as Different Names - vba

This is what I have so far:
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "P:\ME\TEST\"
Dim dateFormat
dateFormat = Format(Now, "yyyy.mm.dd")
For Each objAtt In itm.Attachments
If InStr(objAtt.FileName, "ASDFA ADSF.pdf", vbTextCompare) > 0 Then
objAtt.SaveAsFile saveFolder & dateFormat & " ASDF ASDF.pdf"
ElseIf InStr(objAtt.FileName, "GASD.pdf", vbTextCompare) > 0 Then
objAtt.SaveAsFile saveFolder & dateFormat & " ASDF ADSF ADD.pdf"
ElseIf InStr(objAtt.FileName, "ASDF AD.pdf", vbTextCompare) > 0 Then
objAtt.SaveAsFile saveFolder & dateFormat & " ASDF ASDF.pdf"
ElseIf InStr(objAtt.FileName, "ASDF AS.pdf", vbTextCompare) > 0 Then
objAtt.SaveAsFile saveFolder & dateFormat & " asd asdf.pdf"
Else
objAtt.SaveAsFile saveFolder & "Caught"
End If
Set objAtt = Nothing
Next
End Sub
I used random letters just for privacy. I'm trying to get outlook to autosave email attachments as specific names with date in front using rules and VBA. What am I doing wrong here?

My guess is you don't have permission to write to P:\ME\TEST\ or you aren't getting inside your If statements to save.
You want to set your If lines to be a > 0:
If InStr(objAtt.DisplayName, "BBB AAA.pdf") > 0 Then
If any character is in the first position other than that name, then it will not go inside the If. Of course, that may also be the behavior you want.
You can test if it is not hitting inside the If statments by adding:
Else
objAtt.SaveAsFile saveFolder & "not caught.pdf"
End If
If you don't get a save after adding the Else code, try saving to your local documents folder. Then, if you still don't get any saves, the MailItem doesn't have any attachments.
EDIT:
Should look like this:

Related

Outlook Rules VBA Run a Script - Multiple Rules

How can I have multiple 'scripts' available to run multiple rules? The only code that appears available to run with Rules/run a script..is the code pasted in ThisOutlookSession. The modules below do not appear.
I have Outlook 2010 on windows 11.
I'm accounts payable for my company.
All my invoices (many!) come in my email.
What I'm trying to do: I have many vendors, I want to create rules that will automatically save the attachments from specific vendors to the correct folder. I want to be able to set a rule to save attachments that come from joeshmo's email to the joeshmo folder in My Documents.
What I CAN do: I have a couple working codes:
I have a code where i can select emails, run the macro and all attachments are saved to the same folder.
I have a code that i can use in a rule.
I did have to re-enable scripts for my version of outlook.
So I go to Rules, I set conditions, choose "run a script" and in the drop down, only 1 code is available to run, and that is whatever is pasted into "ThisOutlookSession". When I open visual basic, there is a list of Modules but none of them appear in Run a script. Nor can I move them up there. I can only paste one code. Below is an example of the code.
I wanted to create a Rule and accompanying script per Vendor to run automatically. So all the incoming emails with invoices are automatically saved to their folder.
Am i asking too much? i can save all attachments to the same folder and then sort.
Or I can create rules to assign categories to each vendors email and then sort alphabetically...meaning, I can select all the "A" vendors, run the macro, Select the "B" vendors, etc.
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat
dateFormat = Format(Now, "yyyy-mm-dd H-mm")
saveFolder = "C:\Users\jenny\Documents\Attachments\outlook testing\test number one"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
Any ideas? thanks
"Run a script" expects the incoming item as input.
Code must start with something like Sub somename(itm As Object).
You could create code for each vendor.
Public Sub saveAttachtoDisk_Sender1(itm As Object)
' Rule conditions: from Sender1 with attachment
Dim objAtt As Attachment
Dim saveFolder As String
Dim dateFormat As String
dateFormat = Format(Now, "yyyy-mm-dd H-mm")
saveFolder = "C:\Users\jenny\Documents\Attachments\outlook testing\test number one"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
Next
End Sub
Public Sub saveAttachtoDisk_Sender2(itm As Object)
' Rule conditions: from Sender2 with attachment
Dim objAtt As Attachment
Dim saveFolder As String
Dim dateFormat As String
dateFormat = Format(Now, "yyyy-mm-dd H-mm")
saveFolder = "C:\Users\jenny\Documents\Attachments\outlook testing\test number two"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
Next
End Sub
A single rule may be preferable:
Public Sub saveAttachtoDisk_VariousSenders(itm As Object)
' Rule condition: with attachment
Dim objAtt As Attachment
Dim saveFolder As String
Dim dateFormat As String
dateFormat = Format(Now, "yyyy-mm-dd H-mm")
Debug.Print itm.senderEmailAddress
Select Case itm.senderEmailAddress
Case "sender1#someplace.com"
saveFolder = "C:\Users\jenny\Documents\Attachments\outlook testing\test number one"
Case "sender2#someplace.com"
saveFolder = "C:\Users\jenny\Documents\Attachments\outlook testing\test number two"
Case Else
Debug.Print itm.senderEmailAddress & " not listed."
End Select
Debug.Print saveFolder
If saveFolder <> "" Then
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
Next
End If
End Sub

How to run code, that normally runs in a rule, on a selected item?

I have code to save all attachments in a specific folder.
When I run it on a selected email, I get an error due to Outlook.MailItem being empty.
Does this need a modification to work with selected items instead of an email fetched by a rule?
Public Sub saveAttachtoDisk2(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Set Totalmsg = itm.ReceivedTime
saveFolder = "C:\path\to\file"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & Totalmsg & "." & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
Try this:
For Each objAtt In itm.Attachments
If Not objAtt Is Nothing Then
objAtt.SaveAsFile saveFolder & "\" & Totalmsg & "." & objAtt.DisplayName
End If
Next
Note: There's no need to Set objAtt = Nothing for objAtt will be assigned a new value in the next loop and/or released at all when exiting sub.
You can initialize the itm variable by setting it to Application.ActiveExplorer.Selection.Item(1) (error checking omitted)

Auto Download Attachments and Rename using Sender's Name

I have the following code that auto save the attachments from any incoming mail and renames the attachment with the date and time so as not to overwrite.
Public Sub AutoSaveAttachment(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat
saveFolder = "C:\temp"
dateFormat = Format(Now, "mm.dd.yyyy (Hh.Nn)")
For Each objAtt In itm.Attachments
If InStr(objAtt.DisplayName, ".pdf") Then
objAtt.SaveAsFile saveFolder & "\" & "Attch " & dateFormat & "." & "pdf"
End If
Set objAtt = Nothing
Next
End Sub
I want to add the sender's name before the date/time. I tried this code below which doesn't work. Run-time error.
Sub AttachmentDownloader(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim senderName As String
Dim dateFormat
dateFormat = Format(Now, "mmddyyyy_(Hh.Nn)")
senderName = objAtt.senderName & "_"
saveFolder = "C:\temp"
For Each objAtt In itm.Attachments
If InStr(objAtt.DisplayName, ".pdf") _
Or InStr(objAtt.DisplayName, ".xlsx") _
Or InStr(objAtt.DisplayName, ".xlsm") _
Or InStr(objAtt.DisplayName, ".doc") _
Or InStr(objAtt.DisplayName, ".docx") Then
objAtt.SaveAsFile saveFolder & "\" & senderName & dateFormat & objAtt.DisplayName
End If
Set objAtt = Nothing
Next
End Sub
Any help would be greatly appreciated.
Runtime error "424: Object Required" because you haven't assigned to the objAtt variable yet. You've got an instantiated, but unassigned object variable.
Which is presumably an Attachment object, but that will raise a "Method or data member not found" error, because the attachment object doesn't have a senderName property, that belongs to the MailItem class.
So, try:
senderName = itm.senderName & "_"

Splitting up a field in OUTLOOK

Basically the script below works along with a rule in OUTLOOK. When I receive the email it saves the PDF attachment. The problem I am having is I want to change the file name (objAtt.SaveAsFile). The file name that it comes in as is something like “userid.jobname.JOB22979……..”. I would like to save the file using the second node (jobname in this case), followed by the date and time. I believe I can get the file name from object objAtt.DisplayName, but I don’t know how to pick up just the second node in the file name.
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat
dateFormat = Format(Now, "yyyy-mm-dd H-mm-ss")
saveFolder = "c:\users\xxxxxx\USER\documents\email\"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & dateFormat & "print.pdf"
Set objAtt = Nothing
Next
End Sub
Split to pull out the 2nd token;
DisplayName = "userid.jobname.JOB22979.blah"
job = Split(DisplayName & ".", ".")(1)
If Len(job) > 0 Then
job = job & "_" & Format$(Now, "yyyy-mm-dd H-mm-ss") & "_print.pdf"
Else
'// no match, use original
job = DisplayName
End If
objAtt.SaveAsFile saveFolder & job

Extract msg attachments from outlook email

I have the following vba code that saves attachments in an email.
This works fine for .docx, .jpg etc but I need to use it to extract multiple .msg attachments which doesn't work.
The code is
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "C:\Test\"
For Each objAtt In itm.Attachments
stFileName = saveFolder & "\" & objAtt.DisplayName
i = 0
JumpHere:
If Dir(stFileName) = "" Then
objAtt.SaveAsFile stFileName
Else
i = i + 1
stFileName = saveFolder & "\" & i & " - " & objAtt.DisplayName
GoTo JumpHere
End If
Set objAtt = Nothing
Next
End Sub
The error concerns the line - If Dir(stFileName) = "" Then
Following our chat, here is the final code :
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim i As Integer
saveFolder = "C:\Test\"
For Each objAtt In itm.Attachments
stFileName = saveFolder & objAtt.FileName
i = 0
'Loop to find the first available filename
Do While Dir(stFileName) <> ""
i = i + 1
stFileName = saveFolder & i & " - " & objAtt.FileName
Loop
objAtt.SaveAsFile stFileName
Next
End Sub
Regards,
Max