Issue reading .eml file in vbScript : Though no compilation error, there is no output

1

I am writing the following vbscript program to read an .eml file and extract the email ID of the sender. Though it runs successfully without any error, it does not show the msgbox with the email ID. Please guide me how to resolve this issue.

This Code works fine with VBA.

Sub reademl()

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso, MyFile, FileName, emlid, strLine

Set fso = CreateObject("Scripting.FileSystemObject")

' Open the file for output.
FileName = "C:\Documents\VBScript\test-sample-message.eml"

Set MyFile = fso.OpenTextFile(FileName, ForReading, True, TristateTrue)


Do While MyFile.AtEndOfStream <> True
    strLine = MyFile.ReadLine
    If(InStr(strLine,"To:")<>0) Then
        emlid=Mid(strLine,InStr(strLine,":")+2,Len(strLine)-InStr(strLine,":")-1)
        Exit Do
     End If

Loop
MsgBox(emlid)

MyFile.Close

End Sub

user2363110

Posted 2017-02-28T16:56:45.407

Reputation: 11

Answers

1

It is not entirely clear if this is a snippet of a larger piece of work, but you do not actually call reademl() in your example, so the code, which is in a Sub, is unreachable.

Presumably you do call the sub when using VBA.

Try adding reademl after End Sub

Yorik

Posted 2017-02-28T16:56:45.407

Reputation: 2 956

Alternatively, move the content of the sub to "main()" and delete the Sub (There is no requirement for main() in vbscript, AFAIK, but hopefully the point of the comment is clear) – Yorik – 2017-02-28T18:58:17.533