PDA

View Full Version : [SOLVED:] Start Outlook mail from word, body txt variable + fixed text



wdg1
08-04-2020, 01:17 AM
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.

gmayor
08-04-2020, 05:51 AM
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

wdg1
08-05-2020, 06:37 AM
Fine! This rocks (again)! Thanks a lot. (Ed)Ward