Consulting

Results 1 to 5 of 5

Thread: Outlook VBA .Display does not display; .Send works fine.

  1. #1

    Outlook VBA .Display does not display; .Send works fine.

    I an wrting a script to execute under a mail rule.
    The script generates a message based on a template, appends some information from the incoming email, displays the email to the user and gives them the option to send it

    The problem is that I cant get .display to display the message.
    • If I comment out the line with .display (as below) the msgbox pops up and, if the users clicks Yes (unwise as he has not seen the message!) the message appears in the outbox exactly as intended.
    • If I uncomment the .display line I get the msgbox "Before .display" and the script aborts (no other msgbox messages and no error message) and goes on to process the next mail item.


    Sub SendNew(Item As Outlook.MailItem)
    
    Dim objMsg As MailItem
    MsgBox "In SendNew"
    Set objMsg = Application.CreateItemFromTemplate("C:\Users\Rincewind\AppData\Roaming\Microsoft\Templates\Test.oft")
    NL = "<br/>"
    With objMsg
        .HTMLBody = .HTMLBody & NL & "- - - - - - - - - - - - - - - - - - - - " & NL & "." _
                    & NL & "." & "From:  " & Item.SenderEmailAddress & NL & "." & NL & "." _
                    & NL & Item.Subject & NL & "." & NL & "." & Item.HTMLBody
        .Subject = "FW: " & Item.Subject
        .Recipients.Add "me AT test.com"        'Send to selected persong
        MsgBox "Before .display"
    '   .Display
        MsgBox "After .display"
    End With
    answer = MsgBox("sendNewNunn. Send Message?", vbQuestion + vbYesNo + vbDefaultButton2)
    If answer = vbYes Then objMsg.Send
    Set objMsg = Nothing
    End Sub
    What am I doing wrong?
    And why do I not get an error message from VBA when it aborts the script?
    TFAI

  2. #2
    Basically you cannot do it as you intended. What does the template contain, that cannot be used in a conventional forwarding of the original?
    See the difference with the following.

    Sub SendNew(Item As Outlook.MailItem)
    Dim objMsg As MailItem
    Dim olInsp As Outlook.Inspector
    Dim wdDoc As Object
    Dim oRng As Object
    Dim Answer As Long
    
        MsgBox "In SendNew"
        'Set objMsg = Application.CreateItemFromTemplate(Environ("APPDATA") & "\Microsoft\Templates\Test.oft")
        Set objMsg = Item.Forward
        With objMsg
            Set olInsp = .GetInspector
            Set wdDoc = olInsp.WordEditor
            Set oRng = wdDoc.Range
            oRng.collapse 1
            oRng.Text = "- - - - - - - - - - - - - - - - - - - - " & vbCr & "." _
                        & vbCr & "." & "From:  " & Item.SenderEmailAddress & vbCr & "." & vbCr & "." _
                        & vbCr & Item.Subject & vbCr & "." & vbCr & "."
            .Subject = "FW: " & Item.Subject
            .Recipients.Add "me AT test.com"        'Send to selected person
            MsgBox "Before .display"
            .Display
            MsgBox "After .display"
        End With
        Answer = MsgBox("sendNewNunn. Send Message?", vbQuestion + vbYesNo + vbDefaultButton2)
        If Answer = vbYes Then objMsg.Send
        Set objMsg = Nothing
        Set olInsp = Nothing
        Set wdDoc = Nothing
        Set oRng = Nothing
    End Sub
    If this doesn't work for you, can you post your template?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Quote Originally Posted by gmayor View Post
    Basically you cannot do it as you intended. What does the template contain, that cannot be used in a conventional forwarding of the original?

    If this doesn't work for you, can you post your template?
    But it does work - at least as far as the .Send goes. I don't understand why .Send would work and .Display would not work? Any suggestions?

    I can post the template if necessary but how? All it contains is a picture and some text which effectively form a request.

    It would also work for me if I could add this template to the start of a normal forward - but there is no actual need to forward the entire message especially any attachments

    UPDATE
    So I copied your routine in exactly as posted above and .Display still does not work. So I get "In SendNew" and "Before Dispay" but I do not get the display nor do I get 'After Display'
    At least the problem has been reproduced. Maybe its a setting/failure/corruption on my version of Outlook?
    Last edited by Rincewindwiz; 04-01-2020 at 11:07 PM. Reason: update nw information

  4. #4

    Angry

    Quote Originally Posted by Rincewindwiz View Post
    But it does work - at least as far as the .Send goes. I don't understand why .Send would work and .Display would not work? Any suggestions?

    I can post the template if necessary but how? All it contains is a picture and some text which effectively form a request.

    It would also work for me if I could add this template to the start of a normal forward - but there is no actual need to forward the entire message especially any attachments

    UPDATE
    So I copied your routine in exactly as posted above and .Display still does not work. So I get "In SendNew" and "Before Dispay" but I do not get the display nor do I get 'After Display'
    At least the problem has been reproduced. Maybe its a setting/failure/corruption on my version of Outlook?

    any update on this topic? I have the same issue: .display doesn't open new mail (outlook is open) but .sent is working. I tried to copy only the .display sequence in another new fresh VBA code and there is working...any ideas why?

  5. #5
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,158
    Location
    Hi there,

    Have you tried moving the .Display part up the chain, for example displaying the mail before adding the details as below:

    Sub SendNew(Item As Outlook.MailItem)
    
        Dim objMsg As MailItem
        Set objMsg = Application.CreateItemFromTemplate("C:\Users\McRandom\Desktop\test.oft")
        NL = "<br/>"
        With objMsg
            .Display
            .HTMLBody = .HTMLBody & NL & "- - - - - - - - - - - - - - - - - - - - " & NL & "." _
                        & NL & "." & "From:  " & Item.SenderEmailAddress & NL & "." & NL & "." _
                        & NL & Item.Subject & NL & "." & NL & "." & Item.HTMLBody
            .Subject = "FW: " & Item.Subject
            .Recipients.Add "me AT test.com"        'Send to selected persong
        End With
        Set objMsg = Nothing
    End Sub
    Or adding a .Save line in before the .Display as below:

    Sub SendNew(Item As Outlook.MailItem)
    
        Dim objMsg As MailItem
        Set objMsg = Application.CreateItemFromTemplate("C:\Users\McRandom\Desktop\test.oft")
        NL = "<br/>"
        With objMsg
            .Save
            .Display
            .HTMLBody = .HTMLBody & NL & "- - - - - - - - - - - - - - - - - - - - " & NL & "." _
                        & NL & "." & "From:  " & Item.SenderEmailAddress & NL & "." & NL & "." _
                        & NL & Item.Subject & NL & "." & NL & "." & Item.HTMLBody
            .Subject = "FW: " & Item.Subject
            .Recipients.Add "me AT test.com"        'Send to selected persong
        End With
        Set objMsg = Nothing
    End Sub
    Hope this helps
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2401, Build 17231.20084

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •