Consulting

Results 1 to 7 of 7

Thread: outllok vba to insert text to a email

  1. #1

    outllok vba to insert text to a email

    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

  2. #2
    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?
    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
    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

  4. #4
    Quote Originally Posted by ZZZZZZZZZZZ View Post
    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 ?

  5. #5
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    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

  7. #7
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

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