Solved VB code to open outlook and send compose email

December 3, 2012 at 19:28:08
Specs: Windows 7, 3.401 GHz / 6126 MB

Hello all,

I have scoured the internet and haven't found a code I understand for this. What I want is real simple.. a VB code to open outlook (no address or subject necessary), attach a file and that's it. This code can be preferably executed from a button/link.

Jonas "Jackal" Lear
http://www.sbccrew.com
Serving the South Bay Since 1999


See More: VB code to open outlook and send compose email

Report •


#1
December 3, 2012 at 20:34:00
✔ Best Answer

Googled and found this:

Dim objOutl
Set objOutl = CreateObject("Outlook.Application")
Set objMailItem = objOutl.CreateItem(olMailItem)
objMailItem.Display
strEmailAddr  = "me.me@you.com"
objMailItem.Recipients.Add strEmailAddr
objMailItem.Body = "body here"
objMailItem.Attachments.Add "file.xml"
Set objMailItem = nothing
Set objOutl = nothing

Replace file.xml with your file and strEmailAddr variable to your recipient address.

I didn't test it as I do not have Outlook on this box.

Tony


Report •

#2
December 3, 2012 at 23:20:53

Well, here's a classic case of "overkill", and I think Tony has already nailed this one quite
nicely.
Anyhows, in this case, (blat) you only need "to:" and "att:", as in:
blat to:recipient@sudd.net att:\att1,att2


w=wscript.arguments.count
const forreading=1, forwriting=2, forappend=8
dim z(100)
'dim atts(100)
dim atts
set control=createobject("scripting.dictionary")
set fso=createobject("scripting.filesystemobject")
set config=fso.opentextfile("config",forreading)
if w=0 then
a="?"
else
a=wscript.arguments(0)
end if
if instr(a,"?")>0 then
hlp=fso.opentextfile("blat.hlp",forreading).readall
wscript.stdout.write hlp
wscript.quit
end if
'====== defaults
control("timeout")=10
control("text")=""
control("port")=25
control("ssl")=False
'======= file keys: server from port ssl user pass timeout
do until config.atendofstream
i=i+1 : cf=config.readline
call load( cf)
loop
'======== argument keys: from (override) subject text file to att
for i=0 to wscript.arguments.count-1
call load (wscript.arguments(i))
next
for i=0 to ubound(atts)
wscript.echo atts(i)
next

sub load (cf)
p=instr(cf,":")
if p=0 then p=instr(cf,"=")
if p=0 then
wscript.echo "invalid entry: " & cf
wscript.quit
end if
z(0)=left(cf,p-1)
z(1)=mid(cf,p+1)
z(0)=rtrim(ltrim((lcase(z(0)))))
if z(0)="att" then
atts=split(z(1),",")
else
'wscript.echo ":"&z(0)&":" & z(1)
if z(0)="text" or z(0)="file" then
if z(0)="file" then
z(1)=fso.opentextfile(z(1),forreading).readall
end if
control.item(z(0))=control.item(z(0)) & z(1)
else
control.item(z(0))=z(1)
end if
wscript.echo z(0) & " __ " & control(z(0))
end if
end sub

Set oMessage = CreateObject("CDO.Message")

'(5)
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'(9)
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = control("server")
'(10)
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = control("port")
oMessage.Configuration.Fields.Item(5) = 2
'(9) smtp server
oMessage.Configuration.Fields.Item(9) = control("server")
'(10) server smtp port (always 25)
oMessage.Configuration.Fields.Item(10) = control("port")
'
'timeout for fail
oMessage.Configuration.Fields.Item(8) = control("timeout")
oMessage.Configuration.Fields.Item(1) = 0

'(4) username@domain
oMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = control("user")

'(3) Your password on the SMTP server
oMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = control("pass")
oMessage.Configuration.Fields.Update
on error resume next
for i=0 to 100
wscript.echo cstr(i) & ": " & omessage.configuration.fields.item(i)
next
err.clear
on error goto 0

oMessage.Subject = control("subject")
oMessage.From = control("from")
oMessage.To = control("to")
oMessage.TextBody = control("text")
for i=0 to ubound(atts)
wscript.echo "+" & atts(i)
oMessage.AddAttachment atts(i)
next

h=control.keys
for i=0 to control.count-1
wscript.echo h(i) & ": " & control(h(i))
next

oMessage.Send
'======== end BLAT
'reference used:
'http://www.paulsadowski.com/wsh/cdo.htm


Report •

#3
December 4, 2012 at 21:43:04

Your button/link comment makes me think you're doing this on a web page. What you want to do will need to run on the client, and you'll be limited to either VBScript or JScript. Either language will require Internet Explorer with the scripting security largely disabled, as well as Outlook installed on their machine and configured. This might not be an issue if you're doing an in-house web application used internally, because IT can ensure the above criteria are met.

A better approach might be to use a mailto: link. While not officially supported by the mailto scheme, Outlook does allow a file name in the URI.

The only way you can be sure an email is sent with an attachment is to send it yourself. Have the user upload the file, and send the email with the attachment on the server.

Unicode cat face with wry smile: 😼

How To Ask Questions The Smart Way


Report •

Related Solutions


Ask Question