c# Outlook addin forcibly move the cursor to the end of email body

Viewed 243

I am adding a table to the email body programmatically. I would like to move the cursor below the table after insertion. I've tried SendKeys.Send("{PGDN 10}") and have had some success but I'm not sold on the idea that this is the best way to approach it.

Are there any other ways to forcibly move the cursor down to the end of the email body?

1 Answers

The office model allows you to fetch the WordEditor object if you have access to the MailItem object. Once you have the WordEditor object, you can perform a Range and select to put the cursor in a specific position in the document. There might be an easier way, but this is a quick example.

var editor = oMailItem.GetInspector.WordEditor;
editor.Range(1,1).Select();

Edit

If you'd like to set the cursor to the last position in the body before the signature, you can do something like the sample below.

var originalBody = oMailItem.HTMLBody;
oMailItem.HTMLBody = newBodyHTMLStringCompiled;
var wordDocument = oMailItem.GetInspector.WordEditor;
var endingPosition = wordDocument.Content.End;

// This will append the original signature
oMailItem.HTMLBody += originalBody;

// Set the range for the cursor
wordDocument.Range(endingPosition, endingPosition).Select();
Related