none
Script Help - .Send method not working when using Word Editor RRS feed

  • General discussion

  • Hi Guys,

    Seeking your assistance on this outlook script.

    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 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? Seeking your insights here.

    Thank you very much! Much appreciated.


    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
    

    Friday, May 8, 2015 12:52 PM

All replies

  • Can't be sure this will work but try inserting DoEvents as follows.

    DoEvents         'Insert before following line

    oMail.SendUsingAccount = olApp.Session.Accounts.item(1)


    Regards, OssieMac

    Friday, May 8, 2015 11:43 PM
  • There are a couple of anomalies in your code, but when fixed (below) it works here.

    There are a couple of issues that need clarification. You are extracting the e-mail address from subject. I can't test that, so will assume it is correct. If the address is not valid, the message will not send. Put a temporary message box after the line Eadd = .... to see what exactly you have there.

    You are also using the message body as the forwarded body. I assume that this is what you wanted to do as it doesn't produce a true forwarded message, but a new message which emulates the original. Remove the line oMail.HTMLBody = item.HTMLBody to see the difference.

    As you have declared a range (oRng) which you have not used, you can use that instead of the selection object, as shown to make the change to the first line.

    I think the main problem is that you have inserted oMail.Save. This is not required. Just send the message

    .Display is however required or the edit changes will not occur.

    It is good practice to use Option Explicit at the head of the module, to force you to declare variables. You can set the editor options to insert that for you.

    You cannot use Dim objSel As Word.Selection unless you set a reference to Word. Use instead Dim objSel As Object when using such a construction. This will allow Late Binding to Word and doesn't nbeed the reference.

    Option Explicit Sub EmailForward(item As Outlook.MailItem) Dim oMail As Outlook.MailItem Dim olInsp As Outlook.Inspector Dim wdDoc As Object Dim oRng As Object Dim strSubject As String Dim eAdd As String strSubject = item.Subject eAdd = Right(strSubject, Len(strSubject) - InStr(strSubject, "|")) '?
    'MsgBox eAdd

    strSubject = Replace(strSubject, ", 4 - Low, Open", "") strSubject = Replace(strSubject, ", 4 - Low, New", "") Set oMail = item.Forward

    oMail.Subject = strSubject oMail.To = eAdd oMail.SendUsingAccount = Session.Accounts.item(1) oMail.HTMLBody = item.HTMLBody '? Set olInsp = oMail.GetInspector Set wdDoc = olInsp.WordEditor Set oRng = wdDoc.Paragraphs(1).Range oRng.End = oRng.End - 1 oMail.Display oRng.Text = "Hi," oMail.sEnd Set oMail = Nothing Set olInsp = Nothing Set wdDoc = Nothing Set oRng = Nothing End Sub



    Graham Mayor - Word MVP
    www.gmayor.com


    Sunday, May 10, 2015 5:26 AM
  • Yes, the email address extraction from the subject is working.

    Actually, I'm enabled MS Word Library in Reference to use the word editor.

    Tried using the code you posted, it is working but I'm still having the same problem.

    My main issue is, whenever I run my script or your script, it is properly working but after that my mailboxes won't update at all. It show it's updating and up to date but whenever I checked my mail in OWA, there's a new email there and it is not showing in the mailbox in Outlook.

    Btw, I'm calling this macro whenever I receive a new mail, I set up a rule that will call this script whenever I receive mail. I think that's where the issue is coming from.

    Monday, May 11, 2015 12:19 PM