automatically save outlook attachment - error - vba

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)

Related

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)

Getting "Run-time error '-2147024864 (80070020) which saving outlook attachment on arrival

I run below script to save Outlook attachment on arrival of email. But I frequently get Run-time error '-2147024864 (80070020), which will always stop the inflow of Outlook email. Please advise how to get rid.
Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "C:\Email\"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
try this code please
it prints the name of each attachment before it saves the attachment
press ctrl-G to see "immediate window" where the printout is sent
check the last file name printed when the program crashes
it may provide some insight
Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "C:\Email\"
For Each objAtt In itm.Attachments
debug.print objAtt.DisplayName ' print attachment name to "immediate window"
objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
Next
Set objAtt = Nothing
End Sub

Run a application after saving mail attachment

Need to run an application after saving attachment with below script.
how do i call it After end sub?
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "C:\New folder\tmp\"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
To open Application in VBA
Sub OpenApp()
Dim x As Variant
Dim Path As String
'// Application Path
Path = "C:\Program Files\blabla.exe"
x = Shell(Path, vbNormalFocus)
End Sub

change script from selected to incoming mail

Basic question - I have a script that saves attachments from selected emails in outlook, I want it to save the attachments automatically when they come in instead (I'll create a rule in outlook to run the script when an email comes in), any help would be appreciated!
Public Sub script()
Dim saveFolder As String
Dim objAtt As Outlook.attachment
Dim itm As Outlook.MailItem
Dim dateFormat
dateFormat = Format(Now, "yymmdd ")
saveFolder = "C:\temp"
For Each itm In ActiveExplorer.Selection
For Each objAtt In itm.Attachments
If objAtt.Size > 5200 Then
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
Set objAtt = Nothing
End If
Next objAtt
Next itm
End Sub
You need to pass an item as a parameter. So, the code should look like the following one:
Public Sub script(itm as Outlook.MailItem)
Dim saveFolder As String
Dim objAtt As Outlook.attachment
Dim dateFormat
dateFormat = Format(Now, "yymmdd ")
saveFolder = "D:\temp"
For Each objAtt In itm.Attachments
If objAtt.Size > 5200 Then
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
Set objAtt = Nothing
End If
Next objAtt
End Sub
And don't save attachments on the C: drive, it requires admin privileges on the latest Windows OS. Choose another drive/folder.
I am not sure that you can use a rule for this. I think you will need to hook up an Outlook event. To do this you would use code like the following;
Private WithEvents olItems As Outlook.Items
Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
' this is for your local Inbox - if you have more inboxes you need to set it for each one
Set olItems = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub
'You can add this because you used "WithEvents" to declare olItems
Private Sub olItems_ItemAdd(ByVal item As Object)
Dim olMailItem As Outlook.MailItem
'this event will fire for all items so you need to make sure you have a mail item.
If TypeName(item) = "MailItem" Then
Set olMailItem = item
Dim saveFolder As String
Dim objAtt As Outlook.attachment
Dim dateFormat
dateFormat = Format(Now, "yymmdd ")
saveFolder = "D:\temp"
For Each objAtt In olMailItem.Attachments
If objAtt.Size > 5200 Then
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
Set objAtt = Nothing
End If
Next objAtt
End If
End Sub

91 object variable or with block variable not set

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.