Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

VB script help

Hi Expert:

I have this script that suppose to scan in a txt file and email them to me. The problem is that when the txt file is empty, the script refuses to run.
Could someone help me modify this script so if the txt is empty, it sends out " No record avaiable" in the email body?

Thanks
Const ForReading = 1
Const TriStateUseDefault = -2
 
'Customize these variables
strEmailFrom = "computera@abc.com"
strEmailTo = "andy@abc.com"
strEmailSubject = "Remote access report for computera"
strEmailBody = "Remote access report for computera " & time & "  " & date
strSMTP = "m-a@abc.com"
strAttachment = "c:\remoteaccess.txt"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strAttachment, ForReading, False, TriStateUseDefault)
strEmailBody = strEmailBody & vbCrLf & vbCrLf & objFile.ReadAll 
objFile.Close
 
Set objEmail = CreateObject("CDO.Message")
 
objEmail.From = strEmailFrom
objEmail.To = strEmailTo
objEmail.Subject = strEmailSubject
objEmail.Textbody = strEmailBody
 
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
        strSMTP
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
 
objEmail.Send

Open in new window

0
changjia
Asked:
changjia
2 Solutions
 
omgangCommented:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strAttachment, ForReading, False, TriStateUseDefault)
On Error Resume Next
strFileContents = objFile.ReadAll
If strFileContents = "" Then strFileContents = "No Record Available"
strEmailBody = strEmailBody & vbCrLf & vbCrLf & strFileContents
objFile.Close

OM Gang
0
 
Krys_KCommented:
Hi there

try this. We check if the file is empty and then work accordingly.

Hope it makes sense

Regards
Krystian

Const ForReading = 1
Const TriStateUseDefault = -2
 
'Customize these variables
strEmailFrom = "computera@abc.com"
strEmailTo = "andy@abc.com"
strEmailSubject = "Remote access report for computera"
strEmailBody = "Remote access report for computera " & time & "  " & date
strSMTP = "m-a@abc.com"
strAttachment = "c:\remoteaccess.txt"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strAttachment, ForReading, False, TriStateUseDefault)
 
If Trim(objFile.ReadAll) <> "" Then
	strEmailBody = strEmailBody & vbCrLf & vbCrLf & objFile.ReadAll 
	objFile.Close
Else
	strEmailBody = strEmailBody & vbCrLf & vbCrLf & "File Was Empty"
End If 
 
Set objEmail = CreateObject("CDO.Message")
 
objEmail.From = strEmailFrom
objEmail.To = strEmailTo
objEmail.Subject = strEmailSubject
objEmail.Textbody = strEmailBody
 
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
        strSMTP
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
 
objEmail.Send

Open in new window

0
 
changjiaAuthor Commented:
Hi guys:

Both of your scripts work!

points shared.

Thanks for the help!!

0

Featured Post

Technology Partners: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now