[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Newbie VB script question

Posted on 2005-04-18
7
Medium Priority
?
631 Views
Last Modified: 2007-12-19
OK - embarrassing question.

I'm used to writing VB(A) in an Access/SQL enviornment. *stop sniggering at the back*

However, I've got an urgent need for a 'stand alone' script that will open up Outlook and create an email from a command line or something like that. (.vbs file?)

Is this possible ? The clients are running XP so I think they natively understand vb ...

Basically, I want something like the below to be 'runnable' or call-able from a command line - as a stand alone 'program' (if parameters cannot be passed, that is no problem)

---------------


Sub SendMailOutlook(aTo, Subject, TextBody, aFrom)
 
  'Create an Outlook object
  Dim Outlook As New Outlook.Application
  Set Outlook = CreateObject("Outlook.Application")
 
  'Create e new message
  Dim Message As Outlook.MailItem
  Set Message = Outlook.CreateItem(olMailItem)
  With Message
   
    .Display
   
    .Subject = Subject
    .Body = TextBody
   
    'Set destination email address

    .Recipients.Add (aTo)
   
    'Set sender address If specified.

    Const olOriginator = 0
    If Len(aFrom) > 0 Then .Recipients.Add(aFrom).Type = olOriginator
   
    'Send the message

    .Send

  End With

End Sub
0
Comment
Question by:SpencerSteel
  • 4
  • 2
7 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 1200 total points
ID: 13804368
Try this:

  Dim aTo, Subject, TextBody, aFrom

  aTo      = WScript.Arguments.Item(0)
  Subject  = WScript.Arguments.Item(1)
  TextBody = WScript.Arguments.Item(2)
  aFrom    = WScript.Arguments.Item(3)


  '// Create an Outlook object
  Dim Outlook
  Set Outlook = CreateObject("Outlook.Application")
 
  '// Create e new message
  Dim Message
  Set Message = Outlook.CreateItem(olMailItem)

  With Message
   
    .Display
    .Subject = Subject
    .Body = TextBody
   
    '// Set destination email address
    .Recipients.Add (aTo)
   
    '// Set sender address If specified.
    Const olOriginator = 0
    If Len(aFrom) > 0 Then .Recipients.Add(aFrom).Type = olOriginator
   
    '// Send the message
    .Send

  End With

If you're using Outlook 2003 then you will probably get the added headache of Outlook warning users that an external app is trying to use it to send mail. The code above should get you started tho.

Hope this helps.
0
 

Author Comment

by:SpencerSteel
ID: 13804452
Thanks for the reply.

I've copied and pasted that into a txt file and saved it as a .vbs file. When I run it I get an error on line 3 - subscript out of range.

Any clues ?

S.S.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 13804460
Thats because its expecting 4 command line arguments matching the four arguments in your original snippet.
Either pass parameters on the command-line, or comment the top five lines of code if you don't want to use them.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:SpencerSteel
ID: 13804632
say it very slowly ... *i am an idiot*

Actually, that works (kinda) but I'm in a rush here and technically I'm not that fantastic, so I wondered if you had time to correct this for me.

1) The 'from' is claiming not to be supported
2) How to attach a file to the email from a file path (aPath)

If you could correct this, I would be a very happy man.

Many thanks

S.S.
--------------------

Dim aTo, Subject, TextBody, aFrom, aPath

  aTo      = WScript.Arguments.Item(0)
  Subject  = WScript.Arguments.Item(1)
  TextBody = WScript.Arguments.Item(2)
  aFrom    = WScript.Arguments.Item(3)
  aPath         = WScript.Arguments.Item(4)


  '// Create an Outlook object
  Dim Outlook
  Set Outlook = CreateObject("Outlook.Application")
 
  '// Create e new message
  Dim Message
  Set Message = Outlook.CreateItem(olMailItem)

  With Message
   
    .Display
    .Subject = Subject
    .Body = TextBody
   
    '// Set destination email address
    .Recipients.Add (aTo)



    '// Add attachement if path provided
    .AttachFile = attachment

    '// Set sender address If specified.
    If Len(aFrom) > 0 Then .From = aFrom




    '// Send the message
    .Send

  End With

0
 
LVL 29

Assisted Solution

by:nffvrxqgrcfqvvc
nffvrxqgrcfqvvc earned 300 total points
ID: 13805037
' Go to this link has sample script.

http://techrepublic.com.com/5100-6346_11-5579195.html#

0
 

Author Comment

by:SpencerSteel
ID: 13805500
Thanks for that headsup - but my mail server won't let this script (below) send to anyone apart from internal users - as it's not allowing relaying.

Good grief, I thought this would be quite easy

........................

Option Explicit
Dim objEmail
Dim mbAnswer
Dim attFName

'-------------------------------------------------------------------------------
' Main script
'-------------------------------------------------------------------------------

' Create the message object using CDO
Set objEmail = CreateObject("CDO.Message")

' Assign message properties (To, From, Subject, etc.)
objEmail.From = "me@myCompany.com"

objEmail.To = "someone@123.com"
objEmail.Subject = "Testing Subject"
objEmail.Textbody = "1,2,3,4,5 ... here is the file"

objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "MyMailServer"

' Add attachments
            
objEmail.AddAttachment "c:\test.txt"


' Assign additional message properties, update the object, and send the message
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update

objEmail.Send
0
 

Author Comment

by:SpencerSteel
ID: 13806019
OK - I went back to the 1st method - and I got the attachments to work using

Attachments.Add (aFile)

So, it was just the 'FROM' I was lacking - but that's not critical, so I'll dish out the points and thank you all very much for getting me out of a hole.

S.S.

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question