Consulting

Results 1 to 3 of 3

Thread: Start Outlook mail from word, body txt variable + fixed text

  1. #1
    VBAX Regular
    Joined
    Oct 2018
    Location
    Antwerp
    Posts
    41
    Location

    Start Outlook mail from word, body txt variable + fixed text

    When starting Outlook-mail from WORD,
    I use a var to add the subject line.
    Now, I want tio use the same var in the body.
    How Do I use my var 2 times?
    (..)
    Dim inputData As String
    inputData = InputBox("Your number", "Input number")

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    On Error Resume Next
    ' Change the mail address and subject in the macro before you run it.
    With OutMail
    .To = "yourname@yourname.be"
    .CC = ""
    .BCC = ""
    .Subject = inputData
    .Body = "" & vbCrLf & _
    "kind regards" & vbCrLf & _
    "My company name" & vbCrLf & _
    "My company adress" & vbCrLf & _
    "My company city"
    ----------
    In the body I want to use again the var.
    If I use
    .Body = inputData & vbCrLf & _
    "kind regards" & vbCrLf & _
    "My company name" & vbCrLf & _
    "My company adress" & vbCrLf & _
    "My company city"

    the macro does not work.
    How to do this?
    Thanks.
    (Ed)ward.

  2. #2
    You should use the Outlook editor e.g. as follows, however I would strongly urge you to use the code from http://www.rondebruin.nl/win/s1/outlook/openclose.htm to start Outlook properly, whether it is running or not.

    Sub Macro1()
    
    Dim inputData As String
    Dim OutApp As Object
    Dim outMail As Object
    Dim olInsp As Object
    Dim wdDoc As Document
    Dim oRng As Range
    
        inputData = InputBox("Your number", "Input number")
    
        Set OutApp = CreateObject("Outlook.Application") 'Set OutApp = OutlookApp()
        Set outMail = OutApp.CreateItem(0)
        On Error Resume Next
        ' Change the mail address and subject in the macro before you run it.
        With outMail
            .To = "yourname@yourname.be"
            .CC = ""
            .bCC = ""
            .Subject = inputData
            .Display
            Set olInsp = .GetInspector
            Set wdDoc = olInsp.WordEditor
            Set oRng = wdDoc.Range
            oRng.Collapse 1
            oRng.Text = inputData & vbCrLf & _
                        "kind regards" & vbCrLf & _
                        "My company name" & vbCrLf & _
                        "My company adress" & vbCrLf & _
                        "My company city"
        End With
        Set OutApp = Nothing
        Set outMail = Nothing
        Set olInsp = Nothing
        Set wdDoc = Nothing
        Set oRng = Nothing
    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

  3. #3
    VBAX Regular
    Joined
    Oct 2018
    Location
    Antwerp
    Posts
    41
    Location
    Fine! This rocks (again)! Thanks a lot. (Ed)Ward

Posting Permissions

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