PDA

View Full Version : [SLEEPER:] Outlook VBA .Display does not display; .Send works fine.



Rincewindwiz
04-01-2020, 12:40 PM
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

gmayor
04-01-2020, 09:47 PM
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?

Rincewindwiz
04-01-2020, 10:53 PM
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?

romelsms1
11-17-2021, 12:54 AM
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?

georgiboy
11-18-2021, 05:28 AM
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