PDA

View Full Version : [SOLVED:] Compile error: argument not optional - Item_Add, Application_Reminder, UserForm



Sharper1989
02-04-2021, 04:29 PM
Hi All,

I was hoping for some help, I've got myself confused...

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 create a new email from template, make original email item an attachment, assign "to" field based on original email & do some changes to email body.


PROBLEM
The user form doesn't load on Application_Reminder & when compiling I get "Compile error: argument not optional" on the UserForm code (which does nothing but attempt to call the macro in module 1).

I'm having trouble working out which module each macro should go into. I had it working fine previously with a msgbox & a VBYes/VBNo button, as I could just build an if yes/ if no directly into the THISOUTLOOKSESSION routine & it remembered all the references to the original email item etc. But I needed 3 buttons, not 2 & I liked being able to use a more design friendly user form.

I get the feeling the macro in module 1 can't find reference to the original email item? Do I need the code in module 1 or can I run it directly from the userform?


THIS OUTLOOK SESSION
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
End Sub

Public Sub CommandButton2_Click()
ButtonClick_No
End Sub

Public Sub CommandButton3_Click()
ButtonClick_Dismiss
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.

Paul_Hossler
02-04-2021, 06:07 PM
Just taking a very quick glance, I'd say the problem is that when you call ButtonClick_Yes, you don't pass the Item



USERFORM1

Public Sub CommandButton1_Click()
ButtonClick_Yes MISSING AN ITEM <<<<<<<<<<<<<<<<<<
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

BTW, I added CODE tags to my reply. You can use the [#] icon to insert them and paste your macro between

Sharper1989
02-05-2021, 01:05 AM
Sorry, not quite understanding what you mean..?

Did you mean the full name of the module1 routine wasn't being referenced by the UserForm Command Button 1?

I tried;





Public Sub CommandButton1_Click()
ButtonClick_Yes(ByVal Item As Object)
End Sub




But I get a red syntax error. Is there another way I should pass the item?

Many thanks.

Sharper1989
02-05-2021, 01:25 AM
To be specific, the red syntax error says "Compile Error: Expected: List Separator or)"

gmayor
02-05-2021, 06:23 AM
You need to tell the ButtonClick macro what 'Item' refers to. In this case it appears to be the original Item, so you need to alter that code so that the process can be called with reference to that item e.g.


Option Explicit
Public Sub Application_Reminder(ByVal Item As Object)
Dim objFollowUpMail As Outlook.MailItem
With Item
If .Categories = "Dunning" Then
.Display
With UserForm1
.Show
Select Case .Tag
Case 1: ButtonClick_Yes Item
Case 2: ButtonClick_No Item
Case 3: ButtonClick_Dismiss Item
End Select
End With
End If
End With
Unload UserForm1
End Sub
The userform Code also needs to be changed e.g.

Option Explicit

Public Sub CommandButton1_Click()
Tag = 1
Hide
End Sub

Public Sub CommandButton2_Click()
Tag = 2
Hide
End Sub

Public Sub CommandButton3_Click()
Tag = 3
Hide
End Sub

Paul_Hossler
02-05-2021, 07:56 AM
The usage and the definition are inconsistent

The definition of the sub requires an object to be passed, but when you call the sub, you do not give it the required parameter

Paul_Hossler
02-05-2021, 02:24 PM
Sorry, not quite understanding what you mean..?


Public Sub CommandButton1_Click()
ButtonClick_Yes(ByVal Item As Object)
End Sub


But I get a red syntax error. Is there another way I should pass the item?

Many thanks.




Public Sub CommandButton1_Click()
Call ButtonClick_Yes (Item)
End Sub


1. When you use ( )'s in a call to a sub you need to use the 'Call' keyword

2. The call to a sub doesn't use ByVal or Type the variable ( ... As Object); they are used in the definition of the sub


Public Sub ButtonClick_Yes(ByVal Item As Object)

Sharper1989
02-11-2021, 03:52 PM
Thanks both!

The Tag approach from gmayor worked & I implemented quickly.

Thanks for the information Paul_Hossler, helped me understand better for next time.