Avatar of David Bigelow
David Bigelow
Flag for United States of America asked on

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

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
OutlookVBA

Avatar of undefined
Last Comment
David Bigelow

8/22/2022 - Mon
Norie

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 Bigelow

ASKER
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
ASKER CERTIFIED SOLUTION
Norie

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
David Bigelow

ASKER
Hi Norie,
The change you made addressed the compile error completely. Thank you for your help!
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck