Sharper1989
Member
- Outlook version
- Outlook 2016 32 bit
- Email Account
- Exchange Server
I was hoping for some help, I've got myself confused by cobbling together code found online.
BASIC CONCEPT
PROBLEM
I'm having trouble working out how to ensure the sub-routine macro has all the relevant references to the original Reminder/ Original mail item.
I don't actually know if I need to use Module1 for the separate sub-routine or if I could run it all through UserForm1 code, but either way the original reminder/email item doesn't seem to be known to either..
Previous advice on VBAExpress was to ensure I listed the items when calling the sub routine from UserForm1, but adding in the full reference Sub ButtonClick_Yes(ByVal Item As Object) gave me a syntax error of: "Compile Error: Expected: List Separator or)" .
So I moved to using ButtonClick_Yes , Item, Object when calling as per below but now I get an error of "Argument Not Optional".
THIS OUTLOOK SESSION
Public WithEvents objInboxItems As Outlook.Items
Public WithEvents OlItems As Outlook.Items
Public Sub Application_Startup()
Set objInboxItems = Application.Session.GetDefaultFolder(olFolderInbox).Items
Set OlItems = Session.GetDefaultFolder(olFolderInbox).Items
End Sub
Public Sub OlItems_ItemAdd(ByVal Item As Object)
Dim obApp As Application
Set obApp = Outlook.Application
If Item.Class = olMail And (LCase(Item.Subject) Like "*invoic*" Or LCase(Item.Body) Like "*invoic*") And Item.Attachments.Count > 0 Then
With Item
.Categories = "Dunning"
.ReminderSet = True
.ReminderTime = Now + 7
.Save
End With
End If
Set obApp = Nothing
End Sub
Public Sub Application_Reminder(ByVal Item As Object)
Dim objFollowUpMail As Outlook.MailItem
If Item.Categories = "Dunning" Then
Item.Display
UserForm1.Show
End If
End Sub
USERFORM1
Public Sub CommandButton1_Click()
ButtonClick_Yes , Item, Object
End Sub
Public Sub CommandButton2_Click()
ButtonClick_No, Item, Object
End Sub
Public Sub CommandButton3_Click()
ButtonClick_Dismiss, Item, Object
End Sub
MODULE1
Public Sub ButtonClick_Yes(ByVal Item As Object)
Dim objFollowUpMail As Outlook.MailItem
Set objFollowUpMail = Application.CreateItemFromTemplate("Example.oft")
With objFollowUpMail
.To = Item.Recipients.Item(1).Address
.Subject = "Follow Up: " & Chr(34) & Item.Subject & Chr(34)
.Attachments.Add Item
.HTMLBody = "Example"
.Display
End With
End Sub
Any help, would be really appreciated! Many thanks.
BASIC CONCEPT
- Email comes into inbox, macro runs off _ItemAdd, mail gets categorized based on some keywords & reminder set for 7 days later (this part working fine!).
- Then when relevant category reminder fires, original email item from inbox displays AND userform appears over the top with 3 command buttons.
- (For instance) 1 of these command buttons should run sub-routine ButtonClick_Yes in Module 1 - which creates new email from template, makes original email item an attachment, assign "to" field based on original email & do some changes to email body.
PROBLEM
I'm having trouble working out how to ensure the sub-routine macro has all the relevant references to the original Reminder/ Original mail item.
I don't actually know if I need to use Module1 for the separate sub-routine or if I could run it all through UserForm1 code, but either way the original reminder/email item doesn't seem to be known to either..
Previous advice on VBAExpress was to ensure I listed the items when calling the sub routine from UserForm1, but adding in the full reference Sub ButtonClick_Yes(ByVal Item As Object) gave me a syntax error of: "Compile Error: Expected: List Separator or)" .
So I moved to using ButtonClick_Yes , Item, Object when calling as per below but now I get an error of "Argument Not Optional".
THIS OUTLOOK SESSION
Public WithEvents objInboxItems As Outlook.Items
Public WithEvents OlItems As Outlook.Items
Public Sub Application_Startup()
Set objInboxItems = Application.Session.GetDefaultFolder(olFolderInbox).Items
Set OlItems = Session.GetDefaultFolder(olFolderInbox).Items
End Sub
Public Sub OlItems_ItemAdd(ByVal Item As Object)
Dim obApp As Application
Set obApp = Outlook.Application
If Item.Class = olMail And (LCase(Item.Subject) Like "*invoic*" Or LCase(Item.Body) Like "*invoic*") And Item.Attachments.Count > 0 Then
With Item
.Categories = "Dunning"
.ReminderSet = True
.ReminderTime = Now + 7
.Save
End With
End If
Set obApp = Nothing
End Sub
Public Sub Application_Reminder(ByVal Item As Object)
Dim objFollowUpMail As Outlook.MailItem
If Item.Categories = "Dunning" Then
Item.Display
UserForm1.Show
End If
End Sub
USERFORM1
Public Sub CommandButton1_Click()
ButtonClick_Yes , Item, Object
End Sub
Public Sub CommandButton2_Click()
ButtonClick_No, Item, Object
End Sub
Public Sub CommandButton3_Click()
ButtonClick_Dismiss, Item, Object
End Sub
MODULE1
Public Sub ButtonClick_Yes(ByVal Item As Object)
Dim objFollowUpMail As Outlook.MailItem
Set objFollowUpMail = Application.CreateItemFromTemplate("Example.oft")
With objFollowUpMail
.To = Item.Recipients.Item(1).Address
.Subject = "Follow Up: " & Chr(34) & Item.Subject & Chr(34)
.Attachments.Add Item
.HTMLBody = "Example"
.Display
End With
End Sub
Any help, would be really appreciated! Many thanks.