Reputation: 9
I have a rule that whenever I receive an email with a specific word in a subject line, it will trigger this script.
The script will forward the email to the specific user in the subject line and use the word editor to delete the first line of the email body and change it to "Hi,".
When I use .Display
method then manually click send
it is working fine, but whenever I use .Send
method, the outlook won't update/receive the email in the mailbox and won't forward the edited email. I need to close/relaunch outlook for the script to trigger again.
What is wrong in the script?
Sub EmailForward(item As Outlook.MailItem)
Dim body As String
Dim MI As MailItem
Dim oMail As Outlook.MailItem
Dim olInsp As Outlook.Inspector
Dim wdDoc As Object
Dim oRng As Object
Dim objSel As Word.Selection
Dim olApp As Outlook.Application
Set MI = item
Set olApp = Outlook.Application
item.Subject = Replace(item.Subject, ", 4 - Low, Open", "")
item.Subject = Replace(item.Subject, ", 4 - Low, New", "")
item.Save
Eadd = Right(MI.Subject, Len(MI.Subject) - InStr(MI.Subject, "|"))
Set oMail = MI.Forward
oMail.Subject = MI.Subject
oMail.To = Eadd
oMail.HTMLBody = item.HTMLBody
Set olInsp = oMail.GetInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range(0, 0)
Set objSel = wdDoc.Windows(1).Selection
'oRng.Text = "The accompanying message text"
objSel.MoveDown wdLine, 1, wdExtend
objSel.Delete wdCharacter, 1
objSel.TypeText Text:="Hi,"
objSel.TypeParagraph
oMail.SendUsingAccount = olApp.Session.Accounts.item(1)
oMail.Display
oMail.Save
oMail.Send
End Sub
Upvotes: 0
Views: 700
Reputation: 49395
That's a known issue in Outlook. You have to call the Display method first to get the inspector visible.
Use the HTMLBody or Body property to modify the message body on the fly.
Upvotes: 2