I am trying to automate the execution of an email with an attachment and am at a point of no more ideas. My .vbs code is as follows with the .cfg file contents following. Can someone please help me correct this so I can move on.
Thanks, Mark
***** SendMessageOL.vbs ****
*********** SendMessage.cfg **************
TO_RECIPIENT=Mark
CC_RECIPIENT=
TEXT=Line 1 of some message. Omit if you want to.
TEXT=Line 2 of some message. Omit if you want to.
TEXT=Line N of some message. Omit if you want to.
SUBJECT=VB Send Email Test
ATTACHMENT=C:\OffisApps\SendMessage\stocklst.txt
Thanks, Mark
***** SendMessageOL.vbs ****
Code:
Option Explicit
Const I_REQ_PARMS = 5
Const S_CONFIG_FILE = "c:\OffisApps\SendMessage\SendMessage.cfg"
Const S_LOG_FILE = "c:\OffisApps\SendMessage\SendMessage.log"
Dim fsoLog
Dim oLogFile
Dim tsLogFile
Dim sLine
Dim sSubject
Dim sText
Dim sPassword
Dim sToRecipient
Dim sCcRecipient
Dim sAttachment
Dim iAttachIdx
Private Sub OpenLogFile()
Dim sErrMsg
Set fsoLog = CreateObject("Scripting.FileSystemObject")
Set tsLogFile = fsoLog.CreateTextFile(S_LOG_FILE)
tsLogFile.WriteLine "SendMessageOl.vbs started at " & Now & vbcrlf
End Sub
Private Function ReadConfigFile()
Dim fsoConfig
Dim oConfigFile
Dim tsConfigFile
Dim sLine
Dim iEqPos
Dim bConfigErr
Dim iParmCount
Dim sErrMsg
Set fsoConfig = CreateObject("Scripting.FileSystemObject")
Set oConfigFile = fsoConfig.GetFile(S_CONFIG_FILE)
Set tsConfigFile = oConfigFile.OpenAsTextStream
bConfigErr = False
iParmCount = 0
Do While Not tsConfigFile.AtEndOfStream
sLine = tsConfigFile.ReadLine
iEqPos = InStr(1, sLine, "=")
If InStr(1, sLine, "TO_RECIPIENT=") > 0 Then
sToRecipient = Trim(Mid(sLine, iEqPos + 1))
iParmCount = iParmCount + 1
ElseIf InStr(1, sLine, "CC_RECIPIENT=") > 0 Then
sCcRecipient = Trim(Mid(sLine, iEqPos + 1))
iParmCount = iParmCount + 1
ElseIf InStr(1, sLine, "TEXT=") > 0 Then
sText = sText & Trim(Mid(sLine, iEqPos + 1)) & vbcrlf
iParmCount = iParmCount + 1
ElseIf InStr(1, sLine, "SUBJECT=") > 0 Then
sSubject = Trim(Mid(sLine, iEqPos + 1))
iParmCount = iParmCount + 1
ElseIf InStr(1, sLine, "ATTACHMENT=") > 0 Then
sAttachment = Trim(Mid(sLine, iEqPos + 1))
iParmCount = iParmCount + 1
Else
sErrMsg = "An invalid parameter, " & _
Mid(sLine, 1, iEqPos - 1) & _
" was found in the config file, " & _
S_CONFIG_FILE & "."
tsLogFile.WriteLine (sErrMsg)
bConfigErr = True
Exit Do
End If
Loop
tsConfigFile.Close
Set tsConfigFile = Nothing
Set oConfigFile = Nothing
Set fsoConfig = Nothing
If iParmCount < I_REQ_PARMS Or bConfigErr Then
ReadConfigFile = False
Else
ReadConfigFile = True
End If
End Function
Sub SendMessage(DisplayMsg)
Dim fsoAttachment
Dim objOutlook
Dim objOutlookMsg
Dim objOutlookRecip
Dim objOutlookAttach
Dim CallDescription
' Create a FileSystemObject
Set fsoAttachment = CreateObject("Scripting.FileSystemObject")
Set objOutlook = CreateObject("Outlook.Application")
' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(0) ''' olMailItem = 0
With objOutlookMsg
' Add the To recipient(s) to the message.
.to = sToRecipient
' Add the CC recipient(s) to the message.
If Len( sCcRecipient ) > 0 Then .cc = sCcRecipient
''If Not .Recipients.ResolveAll Then tsLogFile.WriteLine "No recipients!!!"
' Set the Subject, Body, and Importance of the message.
.Subject = sSubject
.Body = sText
'.Body = .Body & vbCrLf & "more text" ' repeat this to append more text to the message.
'' Add attachments to the message. The fully qualified name of the file goes here.
If fsoAttachment.FileExists( sAttachment ) Then
Set objOutlookAttach = .Attachments.Add( sAttachment )
Else
tsLogFile.WriteLine "the file does not exist." & vbcrlf
End If
' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
if not objOutlookRecip.Resolve then
tsLogFile.WriteLine "recipient " & objOutlookRecip.Name & " was NOT resolved." & vbcrlf
else
tsLogFile.WriteLine "recipient " & objOutlookRecip.Name & " was resolved."
end if
Next
' Should we display the message before sending? NO!!!
If DisplayMsg Then
.Display
Else
.Send
End If
End With
Set objOutlookRecip = Nothing
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
Set fsoAttachment = Nothing
tsLogFile.WriteLine "SendMessage.vbs completed at " & Now & vbcrlf
End Sub
Private Sub CloseLogFile()
tsLogFile.Close
Set tsLogFile = Nothing
Set oLogFile = Nothing
Set fsoLog = Nothing
End Sub
OpenLogFile
ReadConfigFile
SendMessage False
CloseLogFile
*********** SendMessage.cfg **************
TO_RECIPIENT=Mark
CC_RECIPIENT=
TEXT=Line 1 of some message. Omit if you want to.
TEXT=Line 2 of some message. Omit if you want to.
TEXT=Line N of some message. Omit if you want to.
SUBJECT=VB Send Email Test
ATTACHMENT=C:\OffisApps\SendMessage\stocklst.txt