passing variable to vbscript for outlook

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
chaos7692
Newbie
Posts: 2
Joined: Wed Oct 02, 2013 6:03 am

passing variable to vbscript for outlook

Post by chaos7692 » Wed Oct 02, 2013 6:08 am

I know zip about vb but am attempting to automate an email using outlook. I have pulled several examples of code, and found 1 that works, except I cant seem to get it to accept a variable in the message body:

let>var1="sample variable"
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" ' This is the Subject
objMailItem.Body = ("%var1%") ' This is the Email message


Set objMailer = objOutlook.GetNameSpace("MAPI")
objMailer.Logon "Outlook", "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


please help, thank you.

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Post by hagchr » Wed Oct 02, 2013 7:26 am

I think you need to pass the variable in the sub call (see x in VB part and extra parameter in VBRun call).


VBSTART
Sub SendOutlookMail(x)

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" ' This is the Subject
objMailItem.Body = x ' This is the Email message


Set objMailer = objOutlook.GetNameSpace("MAPI")
objMailer.Logon "Outlook", "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

let>var1="sample variable"
VBRun>SendOutlookMail,%var1%

chaos7692
Newbie
Posts: 2
Joined: Wed Oct 02, 2013 6:03 am

Post by chaos7692 » Wed Oct 02, 2013 5:28 pm

thank you for the reply.

I have seen a couple of examples of this, where the variable is called in vbrun, and I've tried a couple variations to no effect. This one doesn't work either, I get errors on the line assigning the variable and the vbrun line.

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Oct 03, 2013 1:35 pm

Here's a simple example:

Code: Select all

VBSTART
  Sub MySub(msg)
    MsgBox msg
  End Sub
VBEND

Let>local_var=Hello World
VBRun>MySub,local_var
Change the last part of hagchr's example to:

Let>var1=sample variable
VBRun>SendOutlookMail,var1
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

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