PDA

View Full Version : [SOLVED:] outllok vba to insert text to a email



ZZZZZZZZZZZ
04-10-2018, 11:22 AM
hi have the below code

two questions

#1 I would like to add my default outlook signature to this macro how can i accomplish that
#2 how can i have the cursor automatically be places in the body of the email at the start of this macro




Sub test()
Dim olInspector As Outlook.Inspector
Dim olDocument As Word.Document
Dim olSelection As Word.Selection
Set olInspector = Application.ActiveInspector()
Set olDocument = olInspector.WordEditor
Set olSelection = olDocument.Application.Selection
olSelection.InsertBefore "Hello World!"
olSelection.Collapse wdCollapseEnd
Set olSelection = Nothing
Set olDocument = Nothing
Set olSelection = Nothing
End Sub

gmayor
04-11-2018, 01:53 AM
If you have your signature associated with the account, it will be inserted automatically when you create a new e-mail, without the need for a macro, so what is the purpose of this exercise?

ZZZZZZZZZZZ
04-11-2018, 06:20 AM
If you have your signature associated with the account, it will be inserted automatically when you create a new e-mail, without the need for a macro, so what is the purpose of this exercise?

it doesn't show my signature when i'm opining outlook from another application that is why i'm requesting this modification to my code

ZZZZZZZZZZZ
04-12-2018, 05:47 AM
it doesn't show my signature when i'm opining outlook from another application that is why i'm requesting this modification to my code

ANY UPDATES TO MY QUESTION ?

gmayor
04-12-2018, 06:59 AM
No need to SHOUT!

You need to start Outlook properly if you want to edit the message body effectively. You can call the following macro from your application. Note the comment at the top of the macro for the code needed to start Outlook correctly - the reasons are linked from that page also. Substitute the data from your other application as appropriate


Sub CreateMessage()
'Graham Mayor - http://www.gmayor.com - Last updated - 14 Jul 2017
'Requires the code from http://www.rondebruin.nl/win/s1/outlook/openclose.htm
'to either retrieve an open instance of Outlook or open Outlook if it is closed.
Dim bStarted As Boolean
Dim olApp As Object
Dim oItem As Object
Dim objDoc As Object
Dim oRng As Object
Set olApp = OutlookApp()
Set oItem = olApp.CreateItem(0)
With oItem
.BodyFormat = 2
.Display 'Do not delete this line
Set objDoc = .GetInspector.WordEditor
Set oRng = objDoc.Range
oRng.Collapse 1
oRng.Text = "Hello World"
.to = "someone@somewhere.com"
.Subject = "This is the subject"
'.Send ' Restore after testing
End With
lbl_Exit:
Set oItem = Nothing
Set olApp = Nothing
Set objDoc = Nothing
Set oRng = Nothing
Exit Sub
End Sub

ZZZZZZZZZZZ
04-12-2018, 08:23 AM
this code opens up a new msg

my scenario is that i already have an open MSG i just want to insert some text and my signature to the open msg

gmayor
04-12-2018, 08:59 PM
You said that you were opening Outlook from another applications and your message didn't display the default signature. If you open Outlook this way, from that other application, it will have the default signature associated with the mail account and this is the way to add text to such a message. If you already have a message open in Outlook, why are you opening from another application. It doesn't make sense. The aim is to create a message with the signature and add text to it, not screw up a message with text then attempt to add back the signature.

If you have the message open in Outlook and you want to access that message from another application and add text while preserving the signature, then the following will do that (using the same method to access Outlook as in the previous reply) however it is not as robust as the other code


Sub EditOpenMessage()
Dim olApp As Object
Dim objDoc As Object
Dim oRng As Object

Set olApp = OutlookApp()

On Error GoTo ErrHandler
If TypeName(olApp.ActiveWindow) = "Inspector" Then
If olApp.ActiveInspector.IsWordMail And olApp.ActiveInspector.EditorType = 4 Then
Set objDoc = olApp.ActiveInspector.WordEditor
Set oRng = objDoc.Range
oRng.Collapse 1
oRng.Text = "This is the text to add at the start of the open message"
End If
End If
lbl_Exit:
Set objDoc = Nothing
Set oRng = Nothing
Set olApp = Nothing
Exit Sub
ErrHandler:
Beep
Resume lbl_Exit
End Sub