Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending email and capturing the sent item

Status
Not open for further replies.

NickNNNNN

Programmer
Sep 8, 2014
1
GB
I want to send an email and saved the 'sent' version to c:\ drive

I can kind of achieved something like what I want by using 3 vb scripts (1 calling script and 2 scripts)(see below) BUT I needed to add a wait which works fine unless the email contains an attachment, it's the sleep that causes the problem here as if sending hasn't finished it saves the last sent item
I tried adding saveas to the Application_ItemSend it did save but only the draft version
I tried ItemAdd but nothing happened

I want to be able to do everything in one script without the sleep, it needs to happen after the .send and get the sent version rather than the draft version
I'm happy to explore c# or vb.net if it's a better way to do this ?

Here tr the scripts

Script 1 = this calls script 2 (send email) then waits a couple of seconds and calls script 3 save sent mail

Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")

objShell.Run "C:\script2.vbs"
WScript.Sleep 2000

objShell.Run "C:\script3.vbs"

Set objShell = Nothing


Script 2 - this sends email
Set MyApp = CreateObject("Outlook.Application")
Set MyItem = MyApp.CreateItem(0)

With MyItem
.To = "me@myemail.com"
.Subject = "xsubjectline"
.ReadReceiptRequested = False
.HTMLBody = "Test Body text"

'.save

End With
'MyItem.Attachments.Add("c:\attachment.xls")
'msgbox(MyItem.Sent)
MyItem.Send

' I try to do something with MyItem here but what ever I do it returns NULL and errors, I guess MyItem no longer exists after the .send

Script 3
Dim objOutlook
Dim objDoc
Dim myNameSpace
Dim MyLastSentMessage

On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application")
If objOutlook Is Nothing Then
Set objOutlook = CreateObject("Outlook.Application")
End If
On Error GoTo 0
Set myNameSpace = objOutlook.GetNameSpace("MAPI")
Set MyLastSentMessage = myNameSpace.GetDefaultFolder(5).items(1)

'MyLastSentMessage.Display
MyLastSentMessage.SaveAs("c:\lastsentitem.msg")

Set objDoc = Nothing
Set objOutlook = Nothing
Set myNameSpace = Nothing
Set MyLastSentMessage = Nothing

any suggestions would be very appreciated

Thannk in advance

 
You can combine these into one script. Save the item to MSG file just before sending it like this.

Code:
Set MyApp = CreateObject("Outlook.Application")
Set MyItem = MyApp.CreateItem(0)

With MyItem
.To = "me@company.com"
.Subject = "Test Subject"
.ReadReceiptRequested = False
.HTMLBody = "Test Body text"
[COLOR=#CC0000].SaveAs("c:\temp\mylastsentitem.msg")[/color]
End With
MyItem.Send

I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top