Email using Outlook from VBscript does not run in Task Scheduler

  • Thread starter Thread starter SquirrelMan
  • Start date Start date
S

SquirrelMan

Guest
Tech people,

I have been working on resolving an issue that involves generating an email from database information and sending through outlook. I have got code working perfectly, so long as I run myself. When I try to run as a Task, the email does not get sent. It does not error, but it stops processing at the command, email.send. The code is below. Is this a limitation of Windows? Or is it that I am not technically logged in as myself when the job is run, therefore it can't send? I am not getting any errors, so it is making it a bit difficult in getting to a solution.

Thanks a bunch!

David Langschied


'Define Constants
Const ForReading = 1
Const ForWriting = 2
Const olFolderInbox = 6

'Set variable values
strComputer = "."
MFolder = "C:\v-email\remit\"
AFolder = "C:\v-email\remit\archive\"

'Generate Outlook objects
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_Process Where Name = 'outlook.exe'")
Set oFSOInput = CreateObject("Scripting.FileSystemObject")

'If outlook is not running, open it
If colItems.Count = 0 Then
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
objNamespace.Logon "Default Outlook Profile",, False, True
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
objFolder.Display
End If

'Move through the Mail folder looking for .mail files, generate email, send, and move file to archive
For Each oInputFile In oFSOInput.GetFolder(MFolder).Files
oFileParts = split(oInputFile.Name,".")
oInputFileExt = oFileParts(1)
oInputFileName = oFileParts(0)
If LCase(oInputFileExt) = "mail" Then
'Process mail file
ProcessFiles oFSOInput, oInputFile
'Move mail file to archive
oFSOInput.MoveFile oInputFile,AFolder
End if
Next

'Clear File object
Set oFSOInput = Nothing

Sub ProcessFiles(FSOInput, InputFile)
'Set Input Object
Set InputFile2 = FSOInput.OpenTextFile(InputFile.path, ForReading)
'Loop through file line by line
Do Until InputFile2.AtEndOfStream
strLine = InputFile2.ReadLine
strLineNo = strLineNo + 1
'First time through, generate HTML for email body
If strLineNo = 1 then
oMailBody = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD W3 HTML//EN"">"
oMailBody = oMailBody & "<HTML>" & vbcrlf
oMailBody = oMailBody & "<HEAD><TITLE>No Invoices</TITLE></HEAD>"
oMailBody = oMailBody & "<BODY>" & vbcrlf
End If
'First four lines of file contain the from, to, cc, and subject
If strLineNo <= 4 then
If Left(strLine, 5) = "FROM:" then
oMailfrom = Mid(strLine,6,52)
End If
If Left(strLine, 3) = "CC:" then
oMailcc = Mid(strLine,4,52)
End If
If Left(strLine, 3) = "TO:" then
oMailto = Mid(strLine,4,52)
End If
If Left(strLine, 8) = "SUBJECT:" then
oMailsubj = Mid(strLine,9,80)
End If
Else
'After line four, all the rest is text for the email body
oMailbody = oMailbody & "<br>" & strLine & "</br>"
End If
Loop
'Close off teh HTML of the body
oMailBody = oMailBody & "</BODY></HTML>"
'Close all and clean up
InputFile2.close
set InputFile2 = Nothing
'With all info, now create email
SendMessage oMailfrom, oMailcc, oMailto, oMailsubj, oMailbody
end sub

Sub SendMessage(Mailfrom, Mailcc, Mailto, Mailsubj, Mailbody)
'Set email objects
Set oolApp = CreateObject("Outlook.Application")
Set email = oolApp.CreateItem(0)
'Set email components
email.Recipients.Add(Mailto)
email.Subject = Mailsubj
email.HTMLBody = MailBody
'Send email
email.Send
End Sub

Continue reading...
 
Back
Top