provençal le breton
provençal le breton

Reputation: 1438

Get whole .docx text with office automation, including font and properties (as bold, italic...)

so I am trying to select text from a word document, that contains text in italic, bold, have specific font family.

So I found this code to select all the text :

//doc is the opened document with office automation, this part works
string text = doc.Content.Text;
int end = text.Length;
Microsoft.Office.Interop.Word.Range range = doc.Range(Start: 0, End: end);
range.Select();

Then i use it, to copy the text into a Outlook message :

Microsoft.Office.Interop.Outlook.Application outlook = new   Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)outlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
oMsg.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
oMsg.Body = range.Text ;

And then, when I watch the message html body, I only ot calibri font, and no bold or italic.

What i want to attempt is the same thing as we got when we do it manually :

open word document select all text then copy (ctrl + c) Create a mail in outlook Paste the text in the mail (ctrl + v).

When it is done this way, It is well reproduced (font family, bold etc are well there), but when it is done in the code, it only take the text.

See the differences in this pic (the one bellow is made programmatically, the one bottom, manually):

enter image description here

How can I fix this(if possible)?

Edit : after the answer from Eugene, I add this :

                Microsoft.Office.Interop.Outlook.Inspector insp = oMsg.GetInspector;
                Microsoft.Office.Interop.Word.Document wordEditorDoc = insp.WordEditor;

But then, I don't get how I put the doc text, into the word Editor Object I created. I can still made a wordEditorDoc = doc; but the result is not better than before.

Upvotes: 1

Views: 679

Answers (1)

Eugene Astafiev
Eugene Astafiev

Reputation: 49395

The Body property contains a raw text. A possible way is to use HTMLBody or Word editor. I think the most convinient way is to use the Word object model to copy the content between documents (the Word existing document and the message body). As you probably know Outlook uses Word as an email editor in Outlook. So, you can use the Word object model for formatting email messages in Outlook.

The WordEditor property of the Inspector class returns an instance of the Document class (from the Word object model) which represents the message body.

You can read more about possible ways in the Chapter 17: Working with Item Bodies article in MSDN.

Upvotes: 1

Related Questions