David Bigelow
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.WordE ditor
Set objWordApp = objMailDocument.Applicatio n
'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.CheckGr ammarWithS pelling = True Then
objMailDocument.CheckGramm ar
Else
objMailDocument.CheckSpell ing
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.ActiveInspecto r.CurrentI tem
Item.ShowCategoriesDialog
End If
End Sub
Grammar Check with Spelling
'Occurs before sending email
Private Sub Application_ItemSend(ByVal
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.WordE
Set objWordApp = objMailDocument.Applicatio
'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.CheckGr
objMailDocument.CheckGramm
Else
objMailDocument.CheckSpell
End If
End If
End If
End Sub
And then this Category script.
'Categorize Sent Items
'Place in ThisOutlookSession
Private Sub Application_ItemSend(ByVal
If TypeOf Item Is Outlook.MailItem And Len(Item.Categories) = 0 Then
Set Item = Application.ActiveInspecto
Item.ShowCategoriesDialog
End If
End Sub
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Norie,
The change you made addressed the compile error completely. Thank you for your help!
The change you made addressed the compile error completely. Thank you for your help!
Combine both sets of code in the same event.
Open in new window