I am trying to download the attachment from a subject specified e-mail.
If Msg.Subject = "CALENDAR-EVENT" Then
'Download the attachment
Dim itmAttach As Outlook.MailItem
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "C:\events\"
Dim dateFormat As String
dateFormat = Format(itmAttach.ReceivedTime, "yyyy-mm-dd Hmm ")
For Each objAtt In itmAttach.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
Next
End if
I get the error:
91 object variable or with block variable not set
Maybe there is some error with the line:
Dim itmAttach As Outlook.MailItem
It also would be nice to get the downloaded file's name.
I did not use the outlook APIs before, neither did I touch VBA for years, but by the looks of it you meant this:
If Msg.Subject = "CALENDAR-EVENT" Then
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "C:\events"
Dim dateFormat As String
dateFormat = Format(Msg.ReceivedTime, "yyyy-mm-dd Hmm ")
For Each objAtt In Msg.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
Next
End if
Error 91 seems to be VBA's NullReferenceException.
Related
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)
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 & "_"
I am getting the following error on the script below, I'm trying to set up a script that will run when activated by an outlook rule (i.e. Apply this rule after message arrives; from person#email.com; run a script) and save any attachments to a particular folder.
runtime error '91' Object variable With block variable not set
the error is against 'For Each objAtt In itm.Attachments'
Public Sub script()
Dim saveFolder As String
Dim objAtt As Outlook.attachment
Dim itm As Outlook.MailItem
Dim dateFormat
dateFormat = Format(SentOn, "yymmdd ")
saveFolder = "C:\temp"
For Each objAtt In itm.Attachments
If objAtt.Size > 5200 Then
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
Set objAtt = Nothing
End If
Next
End Sub
You didn't initialize the itm object. Try to define it as a parameter to a method in the following way:
Public Sub script(itm as Outlook.MailItem)
save only images to a folder i.e .jpg .jpeg .gif .png
Include the received date
rename all saved image filetypes to ".jpg"
I have most of it down. It is saving files like this: test.jpeg.jpg and test.jpg.jpg
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat As String
Dim strFileExtension As String
saveFolder = "C:\emails\"
dateFormat = Format(itm.ReceivedTime, "yyyy-mm-dd Hmm ")
strFileExtension = ".jpg"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName & strFileExtension
Set objAtt = Nothing
Next
End Sub
Something like the following would work:
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat As String
Dim strFileExtension As String
Dim strSaveFileName as string
saveFolder = "C:\emails\"
dateFormat = Format(itm.ReceivedTime, "yyyy-mm-dd Hmm ")
strFileExtension = ".jpg"
For Each objAtt In itm.Attachments
if lcase(right(objAtt.FileName, 4)) = "jpeg" or lcase(right(obtAtt.FileName, 3) = "jpg") then
strSaveFileName = mid(objAtt.FileName, instr(1, objAtt.FileName, ".", length(objAtt.FileName) - instr(1, obtAtt.FileName)) & strFileExtension
objAtt.SaveAsFile saveFolder & "\" & dateFormat & strSaveFileName
Set objAtt = Nothing
End if
Next
End Sub
This has an added if statement to test for the file extension being JPG or JPEG. If it is, then it uses some string functions to grab the bits of the filename before the extension and uses that in the final saveasfile.
I am trying to use a script and rule in Outlook to automatically file an attachment. It works but instead of using the attachment's date time, it uses the current datetime.
Here is my script:
Public Sub saveFedhealthstatement(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat
dateFormat = Format(Now, "yyyy-mm-dd H-mm")
Dim subject
subject = itm.subject
saveFolder = "f:\filing\fedhealth"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & subject & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub