Adding vba to script list

Status
Not open for further replies.

Rocklyn Winchester

New Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server 2007
I have a script that transfers attachments from emails in a specific folder and transfers them to a network folder location. The macro run fine manually but I am trying to automate it. I have a rule that moves emails with certain subject lines to the mailbox folder and then I run the script manually. I tried to add the script to the rule but it does not appear in the script list. Below is the VBA code for the macro.

Sub Test()
'Arg 1 = Receipts
'Arg 2 = ""
'Arg 3 = "\\Server\share\rocklynw\Mobile_Receipts"

SaveEmailAttachmentsToFolder "Receipts", "", ""

End Sub

Sub SaveEmailAttachmentsToFolder(OutlookFolderInInbox As String, _
ExtString As String, DestFolder As String)
Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim SubFolder As MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim FileName As String
Dim MyDocPath As String
Dim I As Integer
Dim wsh As Object
Dim fs As Object

On Error GoTo ThisMacro_err

Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set SubFolder = Inbox.Folders(OutlookFolderInInbox)

I = 0
' Check subfolder for messages and exit of none found
If SubFolder.Items.Count = 0 Then
MsgBox "There are no messages in this folder : " & OutlookFolderInInbox, _
vbInformation, "Nothing Found"
Set SubFolder = Nothing
Set Inbox = Nothing
Set ns = Nothing
Exit Sub
End If

'Create DestFolder if DestFolder = ""
If DestFolder = "" Then
Set wsh = CreateObject("WScript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")
MyDocPath = wsh.SpecialFolders.Item("mydocuments")
DestFolder = MyDocPath & "\" & Format(Now, "dd-mmm-yyyy hh-mm-ss")
If Not fs.FolderExists(DestFolder) Then
fs.CreateFolder DestFolder
End If
End If

If Right(DestFolder, 1) <> "\" Then
DestFolder = DestFolder & "\"
End If

' Check each message for attachments and extensions
For Each Item In SubFolder.Items
For Each Atmt In Item.Attachments
If LCase(Right(Atmt.FileName, Len(ExtString))) = LCase(ExtString) Then
FileName = DestFolder & Item.SenderName & " " & Atmt.FileName
Atmt.SaveAsFile FileName
I = I + 1
End If
Next Atmt
Next Item

' Show this message when Finished
If I > 0 Then
MsgBox "You can find the files here : " _
& DestFolder, vbInformation, "Finished!"
Else
MsgBox "No attached files in your mail.", vbInformation, "Finished!"
End If

' Clear memory
ThisMacro_exit:
Set SubFolder = Nothing
Set Inbox = Nothing
Set ns = Nothing
Set fs = Nothing
Set wsh = Nothing
Exit Sub

' Error information
ThisMacro_err:
MsgBox "An unexpected error has occurred." _
& vbCrLf & "Please note and report the following information." _
& vbCrLf & "Macro Name: SaveEmailAttachmentsToFolder" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume ThisMacro_exit

End Sub
 
To create a run a script rule, you need to use a macro using the following naming convention:
sub macroname (item as outlook.mailitem). See http://www.slipstick.com/outlook/rules/outlooks-rules-and-alerts-run-a-script/ for more information.

If you use a run a script rule, you should put conditions in the rule and all actions in the script. You could keep the rule you have and use an itemadd macro - when a message is dropped in the folder, the macro runs.

Option Explicit
Private objNS As Outlook.NameSpace
Private WithEvents objNewMailItems As Outlook.Items

Private Sub Application_Startup()

Dim objMyInbox As Outlook.MAPIFolder

Set objNS = Application.GetNamespace("MAPI")

'change the folder so it looks at the one you are moving the messages to
Set objMyInbox = objNS.GetDefaultFolder(olFolderInbox)

Set objNewMailItems = objMyInbox.Items
Set objMyInbox = Nothing
End Sub

Private Sub objNewMailItems_ItemAdd(ByVal Item As Object)

Dim objEmail As Outlook.MailItem
'Ensure we are only working with e-mail items
If Item.Class<> OlItemType.olMailItem Then Exit Sub

'Arg 1 = Receipts
'Arg 2 = ""
'Arg 3 = "\\Server\share\rocklynw\Mobile_Receipts"
SaveEmailAttachmentsToFolder "Receipts", "", ""

Set objEmail = Nothing
End Sub


for a run a script, do something like this


Sub MoveMail(Item As Outlook.MailItem)
Dim strID As String
Dim objMail As Outlook.MailItem

strID = Item.EntryID
Set objMail = Application.Session.GetItemFromID(strID)

'use the correct path
objMail.Move Session.GetDefaultFolder(olFolderInbox)

'Arg 1 = Receipts
'Arg 2 = ""
'Arg 3 = "\\Server\share\rocklynw\Mobile_Receipts"
SaveEmailAttachmentsToFolder "Receipts", "", ""

Set objMail = Nothing
End Sub
 
Diane thanks for the reply. This is my first time scripting so I don't understand everything you are suggesting. I did try using the naming convention suggested in the post but no joy on the results. I figure I am probably not doing something correctly. I tried creating the itemadd macro but the script debugger doesnt like the "Private With Events objNewMailItems As Outlook Items" line. It highlights it in red. Please advise.


To create a run a script rule, you need to use a macro using the following naming convention:
sub macroname (item as outlook.mailitem). See http://www.slipstick.com/outlook/rules/outlooks-rules-and-alerts-run-a-script/ for more information.

If you use a run a script rule, you should put conditions in the rule and all actions in the script. You could keep the rule you have and use an itemadd macro - when a message is dropped in the folder, the macro runs.

Option Explicit
Private objNS As Outlook.NameSpace
Private WithEvents objNewMailItems As Outlook.Items

Private Sub Application_Startup()

Dim objMyInbox As Outlook.MAPIFolder

Set objNS = Application.GetNamespace("MAPI")

'change the folder so it looks at the one you are moving the messages to
Set objMyInbox = objNS.GetDefaultFolder(olFolderInbox)

Set objNewMailItems = objMyInbox.Items
Set objMyInbox = Nothing
End Sub

Private Sub objNewMailItems_ItemAdd(ByVal Item As Object)

Dim objEmail As Outlook.MailItem
'Ensure we are only working with e-mail items
If Item.Class<> OlItemType.olMailItem Then Exit Sub

'Arg 1 = Receipts
'Arg 2 = ""
'Arg 3 = "\\Server\share\rocklynw\Mobile_Receipts"
SaveEmailAttachmentsToFolder "Receipts", "", ""

Set objEmail = Nothing
End Sub


for a run a script, do something like this


Sub MoveMail(Item As Outlook.MailItem)
Dim strID As String
Dim objMail As Outlook.MailItem

strID = Item.EntryID
Set objMail = Application.Session.GetItemFromID(strID)

'use the correct path
objMail.Move Session.GetDefaultFolder(olFolderInbox)

'Arg 1 = Receipts
'Arg 2 = ""
'Arg 3 = "\\Server\share\rocklynw\Mobile_Receipts"
SaveEmailAttachmentsToFolder "Receipts", "", ""

Set objMail = Nothing
End Sub
 
The itemadd macro needs to be in ThisOutlookSession.

This is watching the inbox - to test, click in the application_startup macro and click run. Then send a test message.


Code:
Option Explicit
Private objNS As Outlook.NameSpace
Private WithEvents objNewMailItems As Outlook.Items

Private Sub Application_Startup()

Dim objMyInbox As Outlook.MAPIFolder

Set objNS = Application.GetNamespace("MAPI")

'change the folder so it looks at the one you are moving the messages to
Set objMyInbox = objNS.GetDefaultFolder(olFolderInbox)

Set objNewMailItems = objMyInbox.items
Set objMyInbox = Nothing
End Sub

Private Sub objNewMailItems_ItemAdd(ByVal Item As Object)

Dim objEmail As Outlook.MailItem
'Ensure we are only working with e-mail items
If Item.Class <> OlItemType.olMailItem Then Exit Sub

'Arg 1 = Receipts
'Arg 2 = ""
'Arg 3 = "\\Server\share\rocklynw\Mobile_Receipts"
SaveEmailAttachmentsToFolder "Receipts", "", ""

Set objEmail = Nothing
End Sub
 
Thanks for the reply Diane. I will work on this and then let you know next week.

The itemadd macro needs to be in ThisOutlookSession.

This is watching the inbox - to test, click in the application_startup macro and click run. Then send a test message.


Code:
Option Explicit
Private objNS As Outlook.NameSpace
Private WithEvents objNewMailItems As Outlook.Items

Private Sub Application_Startup()

Dim objMyInbox As Outlook.MAPIFolder

Set objNS = Application.GetNamespace("MAPI")

'change the folder so it looks at the one you are moving the messages to
Set objMyInbox = objNS.GetDefaultFolder(olFolderInbox)

Set objNewMailItems = objMyInbox.items
Set objMyInbox = Nothing
End Sub

Private Sub objNewMailItems_ItemAdd(ByVal Item As Object)

Dim objEmail As Outlook.MailItem
'Ensure we are only working with e-mail items
If Item.Class <> OlItemType.olMailItem Then Exit Sub

'Arg 1 = Receipts
'Arg 2 = ""
'Arg 3 = "\\Server\share\rocklynw\Mobile_Receipts"
SaveEmailAttachmentsToFolder "Receipts", "", ""

Set objEmail = Nothing
End Sub


Code:
Option Explicit
Private objNS As Outlook.NameSpace
Private WithEvents objNewMailItems As Outlook.Items

Private Sub Application_Startup()

Dim objMyInbox As Outlook.MAPIFolder

Set objNS = Application.GetNamespace("MAPI")

'change the folder so it looks at the one you are moving the messages to
Set objMyInbox = objNS.GetDefaultFolder(olFolderInbox)

Set objNewMailItems = objMyInbox.items
Set objMyInbox = Nothing
End Sub

Private Sub objNewMailItems_ItemAdd(ByVal Item As Object)

Dim objEmail As Outlook.MailItem
'Ensure we are only working with e-mail items
If Item.Class <> OlItemType.olMailItem Then Exit Sub

'Arg 1 = Receipts
'Arg 2 = ""
'Arg 3 = "\\Server\share\rocklynw\Mobile_Receipts"
SaveEmailAttachmentsToFolder "Receipts", "", ""

Set objEmail = Nothing
End Sub
[/QUOTE]
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
B Adding signature to bottom of VBA reply email Outlook VBA and Custom Forms 1
M Adding Subject to this Link-Saving VBA Outlook VBA and Custom Forms 5
G Adding a contact to Outlook with a custom form using Access VBA Outlook VBA and Custom Forms 1
G Adding an attachment to email I just created (VBA) Outlook VBA and Custom Forms 1
S Adding Custom Forms Outlook VBA and Custom Forms 4
G Adding a contact to a specific folder Using Outlook 0
S Adding a recipient's column to Sent folder in Outlook 2010 Outlook VBA and Custom Forms 1
R Adding Userform Dropdown List items from names of subfolders on network drive Outlook VBA and Custom Forms 10
G Stop Outlook 365 adding meetings to calendar Using Outlook 1
G Removing old emails when adding accounts Using Outlook 3
D Contact Group - Adding Bulk Addresses Using Outlook 2
C Outlook 2007 Removing then adding account restores junk email processing Using Outlook 0
J O365 - Adding Shared Google Calendar ICS link issue in O365 Using Outlook 0
S User Defined Fields adding new value (2) Using Outlook 0
M Changing the preferred order for "Put this entry in" list for adding new contacts to the Address Book Using Outlook 1
E Project Management - Adding Folders for Different Folder Types Using Outlook.com accounts in Outlook 0
D Adding Enterprise Exchange Email Account to Outlook Prevents Sending via Outlook.com Account Using Outlook.com accounts in Outlook 10
S Adding new Exchange (2016) rule very slow down Microsoft Outlook Exchange Server Administration 0
Z Outlook Custom Form: Adding Dropdown(Project Code) at the end of subject Outlook VBA and Custom Forms 0
Z Adding dropdown list using custom form Outlook VBA and Custom Forms 7
M Adding Macro to populate "to" "subject" "body" not deleting email string below. Outlook VBA and Custom Forms 5
E Unable to open Outlook 2010 after adding new email account Using Outlook 4
O Adding a new account - "CompanyL (none)" line is added Using Outlook 5
broadbander Needing help with reply/reply all while keeping attachments and adding a new CC recipient. Outlook VBA and Custom Forms 5
M adding corresponding contact form data on a mass scale Using Outlook 5
A VB to "reply all" email items stored in a folder of outlook with adding a new message Outlook VBA and Custom Forms 0
K adding more rules to 'different domains check' macro Outlook VBA and Custom Forms 2
P MS OUTLOOK 2013 - Adding Sender on the CC line Using Outlook 5
R User Defined Fields adding new value Using Outlook 3
W Adding A Macro To Message Ribbon Outlook VBA and Custom Forms 2
I Collecting mail address from GAB and adding to Outlook Task Using Outlook 2
A Outlook 2016 - adding outlook.com account creates a new/strange address Using Outlook.com accounts in Outlook 18
F Adding textbox filter to listbox? Outlook VBA and Custom Forms 2
N Recurring invite sent w/distribution list adding/removing attendees Using Outlook 0
J Issues with adding iCloud to Outlook Using Outlook 1
C Macro to send email after changing from address and adding signature Outlook VBA and Custom Forms 1
J Adding Reply & Delete to main toolbar? Using Outlook 0
T Outlook 2007 adding categories Using Outlook 15
N Adding Appointment Item in Outlook to Shared Calendar Folder Outlook VBA and Custom Forms 7
Diane Poremsky Adding Emojis to Outlook's AutoCorrect Using Outlook 0
T Adding "Mark As Complete" btton to Task Remindet Pop-Up Using Outlook 3
O Saving Attachments to folder on disk and adding Initials to end of file name Outlook VBA and Custom Forms 9
Ascar_CT Adding contacts on Android phone and then syncing them to Outlook Using Outlook.com accounts in Outlook 4
A Adding a 2010 sharepoint contact list to outlook 2010 address book Using Outlook 1
M Adding fields to Task in Outlook Home and Business 2010 Outlook VBA and Custom Forms 7
S Using Send on Behalf is adding extra data in from line Using Outlook 1
Lucylou Outlook 2013 Adding Outlook.com breaks profile, "Outlook not working" messag Using Outlook.com accounts in Outlook 1
C Adding Categories when Composing Email Using Outlook 1
stephenjones Adding a business account to Outlook Using Outlook 1
Chris Grew Adding 2nd Email Address BCM (Business Contact Manager) 3

Similar threads

Back
Top