Creae a VB script to send Outlook Email with Macro Scheduler

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Creae a VB script to send Outlook Email with Macro Scheduler

Post by Bob Hansen » Tue Oct 07, 2003 12:24 am

(Edited note: This code example is superceded by a newer example that I provided in subsequent posting on 10/09/03)

On 9/24/03 there was a request for email using Notes. Since I use Outlook, I thought I would try that instead, here is my Sensible Solution:
:D
VBSTART
Sub SendOutlookMail

Set objOutlook = CreateObject("Outlook.Application")
Set objMailItem = objOutlook.CreateItem(0)
' // Option Values for Outlook Object CreateItem( ) are:
' // 0=MailItem, 1=AppointmentItem, 2=ContactItem, 3=TaskItem
' // 4=JournalItem, 5=NoteItem, 6=PostItem

objMailItem.To = "[email protected]" ' This is the primary recipient
objMailItem.Subject = "Test Macro Scheduler VB Outlook email" ' This is the Subject
objMailItem.Body = "This was sent using Macro Scheduler code." & Chr(13) & Chr(13) & "This is an example of a second line, double spaced." ' This is the Email message

' // Following items are optional settings that can be used =====================
objMailItem.Recipients.Add "[email protected]" ' Append a 2nd Email recipient
objMailItem.Recipients.Add "[email protected];[email protected]" ' Append additional Email recipient ;(s);
objMailItem.CC = "[email protected];[email protected]" ' Carbon Copy name ;(s);
objMailItem.BCC = "[email protected];[email protected]" ' Blind Carbon Copy Name ;(s);
objMailItem.FlagStatus = 2 '0=None (Default), 2=Marked (Red) NOTE:1=Complete (White) cannot be used on unsent email,
objMailItem.Importance = 2 '0=Low (Blue), 1=Normal (None)(Default) 2=High (Red)
' // objAttachments.Add "Source", [Type], [Position], ["Display Name to replace default file name"]
objMailItem.Attachments.Add "C:\config.sys",olByValue, 1, "Test attached config.sys"
' // End of optional items ============================

Set objMailer = objOutlook.GetNameSpace("MAPI")
objMailer.Logon "Actual Profile Name", "Actual Password" 'Logon to Mailer
objMailItem.Send ' send method
' // Use Save method vs. Send to put email in Drafts Folder vs. Outbox/Sending
' // objMailItem.Save ' save method
objMailer.Logoff ' Logoff from Mailer

End Sub
'=======================================
VBEND

VBRun>SendOutlookMail

Label>End
:arrow:
This routine will send the email immediately and leave a copy in the Sent Items or move the Email to the Outbox folder, depending on your settings.

:idea: I have also included a few extra pieces of information to plant the seeds for other Email fields, and other Outlook tools.

:idea: Using the Save method can help troubleshoot problems if Send method is not working. Can see immediate results in Outlook Drafts folder.

This should provide you with a working model showing syntax, punctuation, etc. :idea: Since it is hard coded, you will probably want to change the VB function to take in the variables from Macro Scheduler Input> commands. But that is a project for another day........ :lol:
Last edited by Bob Hansen on Thu Oct 09, 2003 4:26 am, edited 1 time in total.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

Dave1

Post by Dave1 » Thu Oct 09, 2003 12:19 am

Hello Bob,

Indeed, it was helpful. Most helpful in fact.
I use Notes, so I can't take advantage of everything you posted.
However, I was able to use this
& Chr(13) &
to add the second line.

What Support has done the other day to help with the code for SendNotes was overwhelming. Since then, I figured out how to send to multiple people, and then how to cc.

Now, you helped with the line feed.

I am still trying to look for the way to do in SendNotes:
1> send bcc
2> send attachment (your code don't work in Notes)
3> send hyperlink
4> Format text (in notes, we can use color as well as fonts).

I think that it would be too much to ask Support for help again, so I am still trying (but can't see no light in the tunnel).

Cheers

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Thu Oct 09, 2003 4:21 am

objMailItem.Body = "This was sent using Macro Scheduler code." & Chr(13) & Chr(13) & "This is an example of a second line, double spaced." ' This is the Email message
Replace the Body section above from the original example

With this code below.
objMailItem.HTMLBody = "This line is using Header HTML codes.This was sent using Macro Scheduler code.This is an example of a second line, double spaced.Bookmark this link: Sensible Solutions"

============================
That should help take care of some of your formatting and hyperlinks.
(This code example supercedes example that I provided in an earlier posting on 10/06/03)
Complete modified sample is included here:
VBSTART
Sub SendOutlookMail

Set objOutlook = CreateObject("Outlook.Application")
Set objMailItem = objOutlook.CreateItem(0)
' // Option Values for Outlook Object CreateItem( ) are:
' // 0=MailItem, 1=AppointmentItem, 2=ContactItem, 3=TaskItem
' // 4=JournalItem, 5=NoteItem, 6=PostItem

objMailItem.To = "[email protected]" ' This is the primary recipient
objMailItem.Subject = "Test Macro Scheduler VB Outlook email" ' This is the Subject

objMailItem.Body = "This was sent using Macro Scheduler code." & Chr(13) & Chr(13) & "This is an example of a second line, double spaced." ' This is the Email message

' // The Body above is for plain text body.
' // The HTMLBody method below is for formatted email body, showing links, headers, etc.. and can be substituted instead of Body method

objMailItem.HTMLBody = "This line is using Header HTML codes.This was sent using Macro Scheduler code.This is an example of a second line, double spaced.Bookmark this link: Sensible Solutions"

' // Following items are additional optional settings that can be used =====================
objMailItem.Recipients.Add "[email protected]" ' Append a 2nd Email recipient
objMailItem.Recipients.Add "[email protected];[email protected]" ' Append additional Email recipient ;(s);
objMailItem.CC = "[email protected];[email protected]" ' Carbon Copy name ;(s);
objMailItem.BCC = "[email protected];[email protected]" ' Blind Carbon Copy Name ;(s);
objMailItem.FlagStatus = 2 '0=None (Default), 2=Marked (Red) NOTE:1=Complete (White) cannot be used on unsent email,
objMailItem.Importance = 2 '0=Low (Blue), 1=Normal (None)(Default) 2=High (Red)
' // Syntax for: objAttachments.Add "Source", [Type], [Position], ["Display Name to replace default file name"]
objMailItem.Attachments.Add "C:\config.sys",olByValue, 1, "Test attached config.sys"
' // End of optional items ============================

Set objMailer = objOutlook.GetNameSpace("MAPI")
objMailer.Logon "Actual Profile Name", "Actual Password" 'Logon to Mailer
objMailItem.Send ' send method
' // Use Save method INSTEAD OF Send method to put email in Drafts Folder vs. Outbox/Sending
' // objMailItem.Save ' save method
objMailer.Logoff ' Logoff from Mailer

End Sub
'=======================================
VBEND

VBRun>SendOutlookMail
I'm not familiar with Notes, but this allows Outlook to send HTML message bodies. So you can use HTML code to change fonts, colors, insert links, images, etc. (Note the single quotes in the link tag vs. normal double quotes). I would think that Notes must have a similar capability.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Sat Oct 11, 2003 11:09 pm

I received an email after the previous posting. I thought the reply should be public, under a new topic, so I submitted it here:
http://www.mjtnet.com/usergroup/viewtop ... =2618#2618
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

MTARLETSKY
Newbie
Posts: 2
Joined: Mon Feb 23, 2004 5:50 pm

item.send warning in Outlook 2000

Post by MTARLETSKY » Fri Apr 16, 2004 9:20 pm

Item.Send
When you run a program that uses the Outlook object model
to call the Send method, you receive a warning message.
This warning message tells you that a program is trying to
send mail on your behalf and asks if you want to allow the
message to be sent. The warning message contains both a
Yes and a No button, however, the Yes button is not
available until five seconds have passed since the warning
message appeared. You can dismiss the warning message
immediately if you click No . When you click No , the Send
method returns an E_FAIL error in the C or C++ programming
languages.

Question: Is there a way to code the Yes in MS?

Thanks
Mark

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Fri Apr 16, 2004 10:14 pm

You must be getting that message from some type of firewall, but the answer to your question is YES. Follow up the email command with a WaitWindowOpen or similar command.

Example:
...
...
VBRun>SendOutlookMail

//Next section is to wait for Warning Window to open and accept inputs.
WaitWindowOpen>Warning*
SetFocus>Warning*
WaitReady>1

//Next Wait ia a delay for the YES button to appear
Wait>10
PushButton>Warning*,YES
...
...
...
...
//Continue rest of script.
Note that you may need to use &YES on the PushButton if the "Y" is underlined for a Hot-Key.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

Michael Baasch
Newbie
Posts: 4
Joined: Mon Jun 22, 2009 8:39 pm

VBRun>SendOutlookMail

Post by Michael Baasch » Mon Jun 22, 2009 8:44 pm

Hi

Can i use var in lines

let>mail="[email protected]"


objMailItem.To = %mail% ' This is the primary recipient

I have tray, but i get an error

Michael :)

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Mon Jun 22, 2009 10:02 pm

Using variables is not a problem, can be done anywhere.

What is "I have tray....."?

I am not sure what you are doing here, but is it possible that you have too many/too few quote symbols? What if you remove the quotes from the Let> command?
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

Michael Baasch
Newbie
Posts: 4
Joined: Mon Jun 22, 2009 8:39 pm

Using variables

Post by Michael Baasch » Tue Jun 23, 2009 9:11 am

Hi again

I have tray... i have execute it but it dount work.

i get error code vbscript comp error:1032
when i use let>recipters="[email protected]" and
objMailItem.To = %recipts% ' This is the primary recipient

and when i use:
let>[email protected] and
objMailItem.To = %recipts% ' This is the primary recipient
outlook errror 2147467259
can usee name

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Tue Jun 23, 2009 1:00 pm

I am seeing different variable spellings on some of the lines....unable to test with Outlook now.....try these lines:

Code: Select all

let>recipts="[email protected]"
...
VBSTART
...
objMailItem.To = %recipts%
or these:

Code: Select all

let>[email protected]
...
VBSTART
...
objMailItem.To = "%recipts%"
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

Michael Baasch
Newbie
Posts: 4
Joined: Mon Jun 22, 2009 8:39 pm

Hej Bib

Post by Michael Baasch » Tue Jun 23, 2009 1:50 pm

Sorry i hawe 20 fingers om 1 hand so i hawe type wrong.

if i use objMailItem.To = %xx%
or
objMailItem.To = "%xx%"
my code cant compile

only
objMailItem.To = "[email protected]" is OK

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Tue Jun 23, 2009 2:11 pm

let>recipters="[email protected]"

MacroScheduler doesn't need quotes around strings.

Let>myvar="abc" makes myvar equal to "abc" including the quotes

Let>myvar=abc makes myvar equal to abc

Lose the quotes and give it another try.

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Tue Jun 23, 2009 4:08 pm

For Me_again...

You are correct, Macro Scheduler does not need the quotes. But the VBS does need the quotes. So, the MS variable is intentionally including the quotes as part of the variable. You should see them in the WatchList.

But quotes are not always handled the way we expect when mixing code syntaxes. That is why my last example made the same suggestoin as you, omit the quotes from the MS variable and put them explicitly into the VBS line instead.
Last edited by Bob Hansen on Tue Jun 23, 2009 11:24 pm, edited 1 time in total.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Tue Jun 23, 2009 5:29 pm

You're right, I ddn't spend very long trying to figure out where that fragment of code was from and guessed wrong :oops:

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts