awel20 wrote:Code: Select all
Outlook := ComObjActive("Outlook.Application")
email := Outlook.CreateItemFromTemplate(A_Desktop "\Untitled.oft")
email.To := "someone@email.com"
email.Subject := "COM email test with formatting"
; .Display(true) - True to make the window modal. The default value is False.
; The modal window seems to break pasting.
email.Display
; If Word is not the editor. msoTrue = -1, olEditorWord = 4
if !(Outlook.ActiveInspector.IsWordMail = -1 && Outlook.ActiveInspector.EditorType = 4)
{
MsgBox, 48, Error, Editor is not Word.
return
}
WordDoc := Outlook.ActiveInspector.WordEditor
WordApp := WordDoc.Application
; Option1 - Select the end of the document a paste there
; Move selection to the end
;WordApp.Selection.EndOf(6) ; wdStory = 6
; Add a new line
;WordApp.Selection.TypeParagraph
;OR
; Option2 - The template is saved with a bookmark named "MyBookmark"
; The bookmark is used to locate the selection. You could also directly assign text to the bookmark instead of pasting:
; https://autohotkey.com/boards/viewtopic.php?p=84712#p84712
WordDoc.Bookmarks("MyBookmark").Select
; Paste
WordApp.Selection.Paste
Outlook := "", email := "", WordDoc := "", WordApp := ""
Good information for more advanced pasting in Outlook.
Outlook emails are composed in a mini-version of Word with almost all of the same COM abilities as Word.
If you just want to paste in an open email at the current position then you can do this:
Code: Select all
^F12::
olApp := ComObjActive("Outlook.Application")
wdApp := olApp.ActiveInspector.WordEditor.Application
wdApp.Selection.Paste
return
Beginning and End of email:
Code: Select all
^F10::
olApp := ComObjActive("Outlook.Application")
wdApp := olApp.ActiveInspector.WordEditor.Application
wdApp.Selection.HomeKey(6) ; Beginning (wdStory = 6)
wdApp.Selection.Paste
return
^F11::
olApp := ComObjActive("Outlook.Application")
wdApp := olApp.ActiveInspector.WordEditor.Application
wdApp.Selection.EndKey(6) ; End (wdStory = 6)
wdApp.Selection.Paste
return
As awel20 shows there are lots of other selection commands to paste right where you want. There are many more. Once you get that wdApp handle you can use most all the power that COM with Word brings. I have a script that I use everyday that creates and inserts a table of dynamically generated information into an email.
Here is an example of inserting a table at the end of the current email.
Code: Select all
^F9::
olApp := ComObjActive("Outlook.Application")
olMailItem := olApp.ActiveWindow.CurrentItem
olInspector := olApp.ActiveInspector.WordEditor.Application.ActiveDocument
olTable := olInspector.Tables.Add(olInspector.Range(olInspector.Range.End - 1), 6, 7, 1, 0) ; Range, NumRows, NumColumns, DefaultTableBehavior, AutoFitBehavior
olTable.Cell(1,1).Range.Text := "First Cell"
olTable.Cell(3,2).Range.Text := "Three Down, Two Over"
return
If you are pasting from Excel a table is really what is being created in an email.
Kind of off track from the original post but good information that might be useful to someone.
FG