We help IT Professionals succeed at work.

How to use more than one "Application_ItemSend" in VBA script for Outlook.

David Bigelow
on
I want both of these scripts to run before I send an email; however, a duplicate command is causing an error message. I really don't know VBA, just enough to copy/paste.

Grammar Check with Spelling
'Occurs before sending email
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim objMail As Outlook.MailItem
    Dim objMailDocument As Word.Document
    Dim objWordApp As Word.Application
    Dim strMsg As String
    Dim nAnswer As Integer
 
    If TypeOf Item Is MailItem Then
       Set objMail = Item
       Set objMailDocument = objMail.GetInspector.WordEditor
       Set objWordApp = objMailDocument.Application
 
       'Ask you if to spell check
       strMsg = "Do you want to spell check the email right now?"
       nAnswer = MsgBox(strMsg, vbQuestion + vbYesNo, "Spell Check")
 
       If nAnswer = vbYes Then
          'Start spell check
          If objWordApp.Options.CheckGrammarWithSpelling = True Then
             objMailDocument.CheckGrammar
          Else
             objMailDocument.CheckSpelling
          End If
       End If
    End If
End Sub

And then this Category script.

'Categorize Sent Items
'Place in ThisOutlookSession
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    If TypeOf Item Is Outlook.MailItem And Len(Item.Categories) = 0 Then
        Set Item = Application.ActiveInspector.CurrentItem
        Item.ShowCategoriesDialog
   
    End If
   
     
End Sub
Comment
Watch Question

NorieAnalyst Assistant

Commented:
David

Combine both sets of code in the same event.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim objMail As Outlook.MailItem
    Dim objMailDocument As Word.Document
    Dim objWordApp As Word.Application
    Dim strMsg As String
    Dim nAnswer As Integer
 
    If TypeOf Item Is MailItem Then
       Set objMail = Item
       Set objMailDocument = objMail.GetInspector.WordEditor
       Set objWordApp = objMailDocument.Application
 
       'Ask you if to spell check
       strMsg = "Do you want to spell check the email right now?"
       nAnswer = MsgBox(strMsg, vbQuestion + vbYesNo, "Spell Check")
 
       If nAnswer = vbYes Then
          'Start spell check
          If objWordApp.Options.CheckGrammarWithSpelling = True Then
             objMailDocument.CheckGrammar
          Else
             objMailDocument.CheckSpelling
          End If
       End If
    End If

    If TypeOf Item Is Outlook.MailItem And Len(Item.Categories) = 0 Then
        Set Item = Application.ActiveInspector.CurrentItem
        Item.ShowCategoriesDialog
   
    End If

End Sub

Open in new window

David BigelowStaff Operations Specialist

Author

Commented:
Now I'm getting a Compile Error: User-defined type not defined.

Yellow highlight is on: Private Sub Application_ItemSend (ByVal Item As Object, Cancel As Boolean)

And the following code is selected (as in when you might get ready to copy something).
Dim objMailDocument As Word.Document
Analyst Assistant
Commented:
David

I think you would have got that error anyway without combining the 2 sets of code.

Anyway, try this.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim objMail As Outlook.MailItem
    Dim objWordApp As Object
    Dim objMailDocument As Object    
    Dim strMsg As String
    Dim nAnswer As Long
 
    If TypeOf Item Is MailItem Then
       Set objMail = Item
       Set objMailDocument = objMail.GetInspector.WordEditor
       Set objWordApp = objMailDocument.Application
 
       'Ask you if to spell check
       strMsg = "Do you want to spell check the email right now?"
       nAnswer = MsgBox(strMsg, vbQuestion + vbYesNo, "Spell Check")
 
       If nAnswer = vbYes Then
          'Start spell check
          If objWordApp.Options.CheckGrammarWithSpelling = True Then
             objMailDocument.CheckGrammar
          Else
             objMailDocument.CheckSpelling
          End If
       End If
    End If

    If TypeOf Item Is Outlook.MailItem And Len(Item.Categories) = 0 Then
        Set Item = Application.ActiveInspector.CurrentItem
        Item.ShowCategoriesDialog
   
    End If

End Sub

Open in new window

David BigelowStaff Operations Specialist

Author

Commented:
Hi Norie,
The change you made addressed the compile error completely. Thank you for your help!