Consulting

Results 1 to 8 of 8

Thread: Compile error: argument not optional - Item_Add, Application_Reminder, UserForm

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Compile error: argument not optional - Item_Add, Application_Reminder, UserForm

    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.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    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.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    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


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Quote Originally Posted by Sharper1989 View Post
    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)
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    To be specific, the red syntax error says "Compile Error: Expected: List Separator or)"

  7. #7
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  8. #8
    Thanks both!

    The Tag approach from gmayor worked & I implemented quickly.

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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •