VB Script to send Outlook 2003 mail to recepient with attachment

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to find the code that will allow me to automatically send and
email a defined receiptent's name with an attachment.

I have been attempting to use the Command-Line options of outlook /c /m
(e-mail address removed) /a c:\temp\xyz.dat.

The switches are not compatiable and result in error.

As it is extremely difficult to find a definitive answer from any source, I
was hoping that any of you kind folk may be able to suggest a work-around, VB
script, etc that can be run on Windows XP.

Thanks
 
Set objOL = CreateObject("Outlook.Application")
Set objMail = objOL.CreateItem(0)
With objMail
.Subject = "something"
.To = "name of recipient"
.Attachments.Add "c:\temp\xyz.dat"
.Display
End With
 
Sue,

Thank you - this worked perfectly, however, I am executing the VB script as
a macro from the command-line such that:

c:> c:\progra~1\micros~1\Office11\outlook /autorun SendMail

SendMail macro is defined as:

Sub SendMail()
Set objOL = CreateObject("Outlook.Application")
Set objMail = objOL.CreateItem(0)
With objMail
.Subject = "Test Message via VB"
.To = "name of recipient"
.Attachments.Add "c:\temp\xyz.dat"
.Send
End With
End Sub

The above does not execute automatically, of which I have deduced to the
SendMail macro. When I run the macro separately, a "program is trying to send
mail using Item.Send" dialog box is displayed of which I must manually press
yes for the mail to be send. I would like the VB script (or command line
swicth) to assume I have pressed Yes and send the email.

Thanks is anticipation of your response.

Steve
 
Outlook version?

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Sue,

Outlook 2003.

Steve

Sue Mosher said:
Outlook version?

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Since you're coding this as a VBA not macro, not VBScript, you can use the
intrinsic Application object that Outlook VBA supports instead of creating a
new Outlook.Application object.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Sue,

Excuse by ignorance but I have little programming understanding. I really
would appreciate if you could spell out exactly what I would need to do.

Thanks, Steve :)
 
Replace

Set objOL = CreateObject("Outlook.Application")
Set objMail = objOL.CreateItem(0)

with

Set objMail = Application.CreateItem(0)
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Sue,

Thnak you - the Set objMail = Application.CreateItem(0) automaically sent
the email when the SendMail macro was run from within Outlook 2003.

I still cannot get this macro to be execute from the DOS command line, such
that:

c:\program files\microsoft office\Office11\outlook /autorun SendMail

The "/autorun macroname" switch does not get recognised, although it is
supposed to open Outlook and immediately run the macro specified in
macroname. All that happens is outlook is started, nothing is executed.
Possiblly the /autorun switch does not actually function???

Thanks, Steve
 
Sorry, I was focusing on the issue with the security prompt. I've never seen
the /autorun switch work successfully with an Outlook macro.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Hi Steve,

this works. Maybe you have to correct the path to OL, and the one for
the attachment, of course. Paths need to be in quotas.

"C:\Programme\Microsoft Office\Office\Outlook.exe" /c ipm.note /m
(e-mail address removed) /a "c:\test.txt"
 
Michael,

Thanks - tried and did not work. error is the "The command line argument is
not valid". Appears the combination of /c & /m with /a does not work.

I am running Outlook 2003 on Windows XP Pro.

Steve
 
Michael,

The full string is as follows, which I run from c:\program files\microsoft
office\office10:

"c:\program files\microsoft office\office10\outlook" /c ipm.note /m
(e-mail address removed) /a "c:\temp\SendMail.xyz"

I am running Outlook 2002 on MS Windows XP Home at home, and also testing on
a Windows XP Pro with Outlook 2003.

Steve
 
Hi Steve,

indeed it doesn´t work. You can use /c together with /m *or* with /a,
but not with both of them.
 
How do you get around the security warning
"A program is trying to automatically send e-mail on you behalf"

with outlook 2000

Here is what I have
---------------------------------------------

Sub SendMail()
Set objMail = Application.CreateItem(0)

With objMail
.Subject = "Test Message via VB"
.To = "sendor"
.Body = "This is the body"
'.Attachments.Add "e:\wayne.doc"
.Send
End With
End Sub
 
Back
Top