Link to home
Start Free TrialLog in
Avatar of David Bigelow
David BigelowFlag 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
Avatar of Norie
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

Avatar of 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
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Norie,
The change you made addressed the compile error completely. Thank you for your help!