Command Button_Click action on Item/Reminder

Status
Not open for further replies.

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
  • 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.
 
Item & Object objects need to public variables so they can pass from form to module code.

on the userform code - can you post a screenshot of what the form looks like? Do you actually need a userform or just a message box with yes, no, cancel buttons?

I'm thinking you could do

If Item.Categories = "Dunning" Then
Item.Display

strMsg = "Your question goes here"
intRes = MsgBox(strMsg, vbYesNo + vbExclamation, "whatever goes here")
If intRes = vbYes Then
ButtonClick_Yes Item
End If


' assuming you want to do something beside cancel if no
If intRes = vbNo Then
ButtonClick_No Item
End If


End If

Actually, it might be easier to call a macro that brings up the msgbox. But... depending on what the userform does and looks like you may not need to use it.
 
This is basically what I would do - if you need to do something if no - then use a second if -

If intRes = vbNo Then
' do whatever
end if


There might be mistakes in the code I added - have not had a chance to test it yet.

Code:
Public Sub Application_Reminder(ByVal Item As Object)
Dim objFollowUpMail As Outlook.MailItem
If Item.Categories = "Dunning" Then

Item.Display

SendReminder Item
End If
End Sub


Public Sub SendReminder(ByVal Item As Object)
Dim objFollowUpMail As Outlook.MailItem

strMsg = "Your question goes here"
intRes = MsgBox(strMsg, vbYesNo + vbExclamation, "whatever goes here")

If intRes = vbYes Then

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 If

End Sub
 
Thanks for coming back to me! :)

I did actually manage to get it to work using a User Form (after help from another VBA forum), which I was keen on, as I definitely wanted more than just a yes/no value (there's also a defer reminder option). Plus the UserForm is company branded etc.

FIX

Essentially in the UserForm code, I set up 4 subs which are loaded on the relevant button click, which all set a "tag" to a different numeric value .

i.e.

Public Sub CommandButton1_Click()
Tag = 1
Hide
End Sub

Public Sub CommandButton2_Click()
Tag = 2
Hide
End Sub

After setting that "tag" - the UserForm gets hidden & the focus of the macro automatically returns to the Application_Reminder sub I was running (that had paused in the background because of the UserForm). The "tag" value I set in the UserForm is remembered & used in that sub to tell it what route to follow.

i.e. if button 1 has been pressed, this is the "yes" button, so load the follow on macro ButtonClick_Yes, if button 2 has been pressed, this is the "no" button, so load the follow on macro ButtonClick_No etc. etc.


SUB APPLICATION_REMINDER
Public Sub Application_Reminder(ByVal Item As Object)
Dim strPrompt As String
Dim nResponse As Integer
Dim objFollowUpMail As Outlook.MailItem

If Item.Categories = "Awaiting Dunning" Then
Item.Display
With DunningReminderPopUp
.Show
Select Case .Tag
Case 1: ButtonClick_Yes Item
Case 2: ButtonClick_No Item
Case 3: ButtonClick_Dismiss Item
Case 4: ButtonClick_DismissTmr Item
End Select
End With
Unload DunningReminderPopUp
End If
Item.Close olSave
End Sub
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
bdsermons Outlook 365 command button in outlook form Outlook VBA and Custom Forms 5
M VbScript for Command Button on Contacts Custom Form Using Outlook 1
J Command Button to stamp a date and time in a textbox in Outlook 2016 Outlook VBA and Custom Forms 3
D Command Button code will not execute. Any suggestions Please. Outlook VBA and Custom Forms 2
S Problem running Command button code Outlook VBA and Custom Forms 2
S Code behind form command button Using Outlook 2
M Open new outlook form from within existing outlook form using command button Using Outlook 4
A Command Button Controls Outlook VBA and Custom Forms 6
J Command Button to insert Email Signature Outlook VBA and Custom Forms 2
Z Command button no longer firing after sending custom form over ema Outlook VBA and Custom Forms 1
U Customized Command Button Outlook VBA and Custom Forms 3
M Move command Outlook VBA and Custom Forms 11
J OUTLOOK 2016 FILE STORAGE WHERE COMMAND Using Outlook 12
D Custom Form Accept and Reject Command buttons Outlook VBA and Custom Forms 2
Dr. Demento Outlook version of Excel command? Using Outlook 5
L Outlook Data Files command not working in the quick access bar Using Outlook 1
R Missing Backup command in File menu after installing pfback.exe Using Outlook 2
S Exchange Management Shell closes on command failure Exchange Server Administration 5
M "New Appointment from Contact" command missing from Outlook 2003 Using Outlook 1
J Executing Ribbon Command from Code Outlook 2010 Outlook VBA and Custom Forms 3
S How to call a procedure from a custom command bar in Outlook 2007 Outlook VBA and Custom Forms 1
L Outlook 2003 - Set Virables via command line Outlook VBA and Custom Forms 1
T Command bar IDs for Outlook 2007 Using Outlook 2
T Is there a VBA command to Redirect your Emails? Outlook VBA and Custom Forms 1
A How to access command bars in outlook 2007 Outlook VBA and Custom Forms 3
5 How to get Outlook's command line parameters? Outlook VBA and Custom Forms 1
G Add Map It button to Custom Contacts Form in Outlook Outlook VBA and Custom Forms 1
J Outlook 2021 ScanPST errors (yet again ... sorry): repair button missing Outlook 2021 Using Outlook 5
S Outlook 2002- "Send" button has disappeared. Help please. Using Outlook 1
TomHuckstep Remove Send/Receive All Folders (IMAP/POP) button from Outlook 365 Ribbon Using Outlook 2
Witzker How to get the button Karte ( map) in custom contact form Outlook VBA and Custom Forms 2
J Want to create a button on the nav bar (module add-in) to run code Outlook VBA and Custom Forms 2
B How to create a button that sorts and selects the most recent message with ONE click Using Outlook 2
jehan2256 "History" button in Business Contact Manager Using Outlook 1
L "Insert Pictures" Button-Wrong Folder Using Outlook 5
O Replace hard returns with soft returns on selected text and button to QAT Using Outlook 5
J "Contact" button in Journal entry Using Outlook 1
O Outlook 2010 Add delete button to the side of the message list Using Outlook 1
I Button PDF in Outlook Contact custom form Outlook VBA and Custom Forms 1
D Outlook 2013 Macros only run in VB editor, not in drop down or button Outlook VBA and Custom Forms 14
N contact list seen in Contact folder but knot in Address book or when 'TO' button is clicked in new email Using Outlook 0
Witzker HowTo start a macro with an Button in OL contact form Outlook VBA and Custom Forms 12
R Follow up button doesn't working neither the reminders in BCM BCM (Business Contact Manager) 0
Eike Move mails via macro triggered by the click of a button? Outlook VBA and Custom Forms 0
K Disabling import/export button to restrict PST creation Using Outlook 3
N Which Button Was Clicked on the Ribbonbar Menu Outlook VBA and Custom Forms 2
E Can't accept or decline task (no button appears) Using Outlook 2
Q Prompt button to auto turn on Out of Office Outlook VBA and Custom Forms 3
C iCloud Setting missing Outlook tab and Outlook missing the iCloud refresh button Using Outlook 4
Witzker Outlook 2010 Insert Date & Time at the button of an OL contactform in red Using Outlook 2

Similar threads

Back
Top