How to get my macro as rule in Outlook?


Joined
Mar 14, 2012
Messages
1
Reaction score
0
Hello,

Below my macro which already works in Outlook, but I want to select the macro below as a script in Outlook for a rule. I know I must include "As MailItem" in my Public Sub, but I don't know how to implement it in the macro below. Can someone help me find a solution?

Public Sub GetAttachments()
On Error GoTo GetAttachments_err
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 i As Integer
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set Subfolder = Inbox.Folders("LuukMarel")
If Subfolder.Items.Count = 0 Then
MsgBox "There are no messages in the LuukMarel folder.", vbInformation, _
"Nothing Found"
Exit Sub
End If
If Subfolder.Items.Count > 0 Then
For Each Item In Subfolder.Items
For Each Atmt In Item.Attachments
If Right(Atmt.FileName, 3) = "pdf" Then
FileName = "C:\ok" & _
Format(Item.CreationTime, "yyyymmdd_hhnnss_") & Atmt.FileName
Atmt.SaveAsFile FileName
i = i + 1
End If
Next Atmt
Next Item
End If
If i > 0 Then
MsgBox "I found " & i & " attached files." _
& vbCrLf & "I have saved them into the C:\ok folder." _
& vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
Else
MsgBox "I didn't find any attached files in your mail.", vbInformation, _
"Finished!"
End If
GetAttachments_exit:
Set Atmt = Nothing
Set Item = Nothing
Set ns = Nothing
Exit Sub
GetAttachments_err:
MsgBox "An unexpected error has occurred." _
& vbCrLf & "Please note and report the following information." _
& vbCrLf & "Macro Name: GetAttachments" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume GetAttachments_exit
Exit Sub
End Sub
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top