i assembled a lot of bits and pieces from the web
here is a working test program
just open "new email" then run the program (or single-step)
the second half of the program is just to show how to manipulate text in the email body
it is only VBA, but it may give you a starting point
i will have a look at the python interface next
regards
js:)
' NOTE: make sure that you have a reference to "microsoft word object library"
' go to "tools" ... "references" ... find microsoft word object library in dropdown and click on checkmark box
' this is just some test text
Const a1 = "1111111111 1234567890 1111111133 1111111144 1111111155"
Const a2 = "2222222211 2222222222 2222222233 2222222244 2222222255"
Const a3 = "3333333311 3333333322 3333333333 3333333344 3333333355"
Const a4 = "3333333366 3333333377 3333333388 3333333399 3333333300"
Const a5 = "4444444411 4444444422 4444444433 4444444444 4444444455"
Sub test1234()
Dim wordDoc As Document
Set wordDoc = Application.ActiveInspector.WordEditor
wordDoc.Range.Delete ' clean page
wordDoc.Paragraphs.Space1 ' single space paragraph
wordDoc.Paragraphs.SpaceBefore = 0 ' no space before paragraph
wordDoc.Paragraphs.SpaceAfter = 0 ' no space after paragraph
wordDoc.Range.InsertAfter (a1)
wordDoc.Range.InsertParagraphAfter ' paragraph mark
wordDoc.Range.InsertAfter (a2)
wordDoc.Range.InsertParagraphAfter
wordDoc.Range.InsertAfter (a3)
wordDoc.Range.InsertAfter Chr(11) ' linebreak
wordDoc.Range.InsertAfter (a4)
wordDoc.Range.InsertParagraphAfter
wordDoc.Range.InsertAfter (a5)
wordDoc.Range.InsertParagraphAfter
Stop
Dim objRange As Range
Dim rng As Range
Dim objEq As oMath
Dim formula As String
' check this webpage re. problems with oMath
' https://stackoverflow.com/questions/20068212/excel-word-equations-using-omath-buildup-method#20072757
'formula = "Celsius = (5/9)(Fahrenheit - 32)"
formula = "Celsius = " & ChrW(&H221A) & "(x+y) + sin(5/9 × (Fahrenheit – 23 (" & ChrW(&H3B4) & ")^2))"
wordDoc.Range.InsertParagraphAfter
wordDoc.Range.InsertAfter (formula)
wordDoc.Range.InsertParagraphAfter
Stop
Offset = Len(formula)
Set rng = wordDoc.Range(wordDoc.Sentences(5).Characters(1).Start, wordDoc.Sentences(5).Characters(Offset).End)
rng.Select ' this line is not necessary, but it shows size and position of the range when single-stepping through the program
Set objRange = wordDoc.OMaths.Add(rng)
' objRange.OMaths(1).BuildUp
wordDoc.OMaths(1).BuildUp
Application.ActiveInspector.currentItem.Display ' not sure what this does, exactly ... may not be necessary
Application.ActiveInspector.currentItem.Save ' save email draft
' Application.ActiveInspector.currentItem.Close olSave
Stop
' the equation should be in the email body at this point
' ------------------------------------------------------
' ------ following is just code that "plays around" with the text
' ------ it may be of interest to someone
response = InputBox("Type some text")
With wordDoc.Paragraphs(wordDoc.Paragraphs.Count).Range
.InsertAfter "1." & vbTab & response
.InsertParagraphAfter
End With
Stop
wordDoc.Undo
wordDoc.Undo
Stop
wordDoc.Range.Delete
wordDoc.Undo
wordDoc.Paragraphs(1).Range.Delete unit:=wdWord, Count:=3 ' delete 1st three words
wordDoc.Undo
wordDoc.Sentences(1).Words(2).Delete unit:=wdWord, Count:=2 ' delete words 2 and 3
wordDoc.Undo
wordDoc.Sentences(1).Words(2).Characters(4).Delete unit:=wdCharacter, Count:=2 ' delete characters in second word
wordDoc.Undo
' Debug.Print wordDoc.Sentences(1).Characters.Count
' Debug.Print wordDoc.Words.Count ' EOL counts as a word
' For i = 1 To wordDoc.Words.Count
' Debug.Print wordDoc.Words(i)
' Next
Stop
' Dim word As Range
' Debug.Print wordDoc.Sentences(2).Words.Count ' EOL counts as a word
' For Each word In wordDoc.Sentences(2).Words
' Debug.Print word
' Next
Stop
' these two are the same
' Application.ActiveInspector.currentItem.GetInspector.WordEditor.Characters(2).InsertBefore "___xxxxxx___"
' Application.ActiveInspector.WordEditor.Characters(2).InsertBefore "___xxxxxx___"
wordDoc.Characters(1).Select ' 1st character from top of page
wordDoc.Characters(10).Select ' 10th character from top of page
wordDoc.Range(0, 5).Select ' 1nd to 5th character
wordDoc.Range(1, 5).Select ' 2nd to 5th character
Debug.Print wordDoc.Characters(1) ' 1
Debug.Print wordDoc.Characters(1).Start ' 0
Debug.Print wordDoc.Characters(1).End ' 1
Debug.Print wordDoc.Characters(1) ' 1
' wordDoc.Sentences(3).Select ' all of 3rd sentence (paragraph)
' wordDoc.Sentences(4).Characters(5).Select ' 5th character of 4th sentence
' Debug.Print wordDoc.Sentences(4).Characters(3) ' !!!! position of 2nd character on PAGE !!!!
' Debug.Print wordDoc.Sentences(2).Characters(3).Start ' position of 3rd character in 2nd sentence
' Debug.Print wordDoc.Sentences(2).Characters(3).End ' position AFTER 3rd character in 2nd sentence
' wordDoc.Range(wordDoc.Sentences(2).Characters(3).Start, wordDoc.Sentences(3).Characters(5).End).Select
' wordDoc.Range(wordDoc.Sentences(2).Characters(3).End, wordDoc.Sentences(3).Characters(5).End).Select
' wordDoc.Sentences(2).InsertBefore "__ before sentence #2 __"
' wordDoc.Sentences(3).InsertAfter "__ after sentence #3 __"
End Sub