Hey guys, I'm new to the forum, my name is Jassie.
So I've been given some very redundant reporting tasks, and I know I can automate them so I don't have to spend hours every month doing them. unfortunately, I just started learning VBA about 2 weeks a go. I can definitely help people with some SQL Server stuff!
Basically, I want to flag specific emails based on the subject, email address. Then save it, open it up in Excel and call a macro to do some formatting so it can be uploaded into a database. After all that is done, save it and email it back to the sender... But I can't seem to get outlook to do anything...
Any help would really be appreciated, here's what I've got so far, hopefully it's just a silly mistake.
Option Explicit
Private WithEvents olInboxItems As Items
Private Sub Application_Startup()
Dim objNS As Outlook.NameSpace
Dim X As Integer
Set objNS = GetNamespace("MAPI")
Set olInboxItems = Session.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
On Error Resume Next
Dim olMailItem As MailItem
Dim olAttName As String
Dim objNS As Outlook.NameSpace
Dim olDestFldr As Outlook.MAPIFolder
Dim ExcelApp As Object
Dim ExcelWK As Object
Dim Att As String
Set objNS = GetNamespace("MAPI")
If TypeOf Item Is MailItem Then
Set olMailItem = Item
If olMailItem.Attachments.Count = 1 _
And olMailItem.SenderEmailAddress = "SENDER'[email protected]" _
And InStr(olMailItem.Subject, "this is a test of automation") > 0 Then
Const AttPath As String = "C:\Users\jvaughn\Desktop\Test"
Set olDestFldr = objNS.Folders("Inbox - *my personal email*").Folders("Inbox").Folders("Test")
Set ExcelApp = CreateObject("Excel.Application")
Set olAttName = olMailItem.Item(1).FileName
olMailItem.Attachments.Item(1).SaveAsFile AttPath + olAttName + Date
On Error Resume Next
ExcelApp.Workbooks.Open (AttPath + olAttName + Date)
ExcelApp.Run (PERSONAL.XLSB!TestingMacro1)
ExcelApp.Workbooks.Close
Kill AttPath & Att
ExcelApp.Quit
olMailItem.UnRead = False
olMailItem.Move olDestFldr
End If
End If
End Sub
Bookmarks