Link to home
Start Free TrialLog in
Avatar of SpencerSteel
SpencerSteel

asked on

Newbie VB script question

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
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SpencerSteel
SpencerSteel

ASKER

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.
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.
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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.