Sign in to follow this  
Followers 0
taurus905

How can I use a word document to populate the body of an email in Outlook?

11 posts in this topic

As the title states, I am trying to use COM to paste the contents of an existing word document in to the body of an email in Outlook.

I know this may be really simple, (I am hoping anyway) but the solution is aluding me at the moment.

Any help is appreciated.

taurus905


"Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs

Share this post


Link to post
Share on other sites



I haven't gone through it personally, but have you sifted through big_daddy's word automation library to see if anything there might help?

http://www.autoitscript.com/forum/index.php?showtopic=30461


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

I haven't gone through it personally, but have you sifted through big_daddy's word automation library to see if anything there might help?

http://www.autoitscript.com/forum/index.php?showtopic=30461

SmOke_N,

Thanks for the suggestion. I looked through big_daddy's thread and could not find anything I needed.

Another thing I would like to do is use COM to attach a document to an email created in Outlook.

I will keep seaching and hope that someone knows the simple commands I need.

Thanks again.

taurus905


"Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs

Share this post


Link to post
Share on other sites

I found the command to attach a file:

$oMail.Attachments.Add("C:\Memo.doc")

Now I just need something like this:

$oMail.Body("C:\Letter.doc")

But it doesn't work.

Any ideas?


"Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Are you trying to automate CDO or Outlook?

Dale

Edit: Oh... I just reread your topic title... Outlook... Ok

Edited by DaleHohm

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

Yikes... this appears to be much harder than you would expect it to be. Google RTFBody and you'll find a lot of discussion... essentually it requires that you need to create an RTF text stream instead of "including" a document.

Looks like HTMLBody format is much easier to deal with and should give you the same rich formatting... you can convert a word doc to html...

Dale


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

Sorry it took me so long, but this works for me.

#include <IE.au3>
#include <Word.au3>

Const $olMailItem = 0

Const $olByValue = 1
Const $olByReference = 4
Const $olEmbeddeditem = 5
Const $olOLE = 6

_WordErrorHandlerRegister ()

$sFilePath = @ScriptDir & "\test.doc"
$oWordApp = _WordCreate ($sFilePath, 1, 0)
$oDoc = _WordDocGetCollection ($oWordApp, 0)
$sDoc = $oDoc.Range.Text
_WordQuit ($oWordApp)

$oOutlook = ObjCreate("Outlook.Application")

$sSubject = "Email Subject"
$sTo = "someone@somewhere.com"
$sAttachment = @ScriptDir & "\test.txt"
_SendEmailItem($oOutlook, $sSubject, $sTo, $sDoc, $sAttachment)

Func _SendEmailItem($o_Object, $s_SubjectEmail, $s_ToEmail, $s_BodyEmail, $s_Attachment = "")
    
    Local $o_EMail = $o_Object.CreateItem ($olMailItem)

    With $o_EMail
        .To = $s_ToEmail
        .Subject = $s_SubjectEmail
        .Body = $s_BodyEmail
        If $s_Attachment <> "" Then
            .Attachments.Add ($s_Attachment, $olByValue, 0)
        EndIf
        .Send ()
    EndWith
EndFunc   ;==>_SendEmailItem

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Yikes... this appears to be much harder than you would expect it to be. Google RTFBody and you'll find a lot of discussion... essentually it requires that you need to create an RTF text stream instead of "including" a document.

Looks like HTMLBody format is much easier to deal with and should give you the same rich formatting... you can convert a word doc to html...

Dale

It appears taht the "Redemption" library may give you more to work with. I had only been aware of its use related to the Outlook Security patch issues, but it appears to allow more: "Directly access RTF body of any Outlook item". You'll find it (free) here: http://www.dimastr.com/redemption/home.htm

Dale

Edit: small typo

Edited by DaleHohm

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

And more info here: How to create formatted messages in Microsoft Outlook - http://www.outlookcode.com/d/formatmsg.htm

Dale


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

So I'm doing this research and didn't notice that Bob slipped a working example into the thread... please backup and see post 7 above :whistle:

Dale


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

DaleHolm and big_daddy,

Thank you both for your input. Your ideas helped me to complete my script.

I ended up doing the following to paste the contents of a word document:

$oWord = ObjCreate("Word.Application")
$oWord.Documents.Open($Body_Path)
    With $oWord.Activewindow.Selection
        .WholeStory
        .Copy
    EndWith
$oWord.Quit; Close the Word object.
$oWord = ""; Nullify the object string just to clean things up.

$oMail.Display
Send("^v")

(Note that I did not need the rich text formatting.)

And I used this to add the attachment:

$oMail.Attachments.Add($Attach_Path)

Thanks again for taking enough interest in my question to spend your time to research and respond to this post.

taurus905


"Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0