Search code examples
arraysvbscriptmsgbox

Display text file using MsgBox


I have multiple files with names:

c123we_1014_A1_45333
c123we_1014_A2_45333
c123we_1014_A3_45333

What I need is to get only the third parameter and display it using a message box. What I do is I get the third parameter and write it in text file.

For Each filelog In FileList
    LogFile = Split(filelog, "~")(1)

    Set otf = fso.OpenTextFile("C:\Data\" & LogFile, 1)

    sFile = Split(LogFile, "_")
    CurStep = sFile(3)

    FileNameStep = LotId & "_" & "Step"
    ScriptPath = Mid(ScriptFolder, 1, Len(ScriptFolder) - 8)

    If Not fso.FileExists(ScriptFolder & "\" & FileNameStep & ".txt") Then
        Set ctf = fso.CreateTextFile(ScriptFolder & "\" & FileNameStep & ".txt", True)
        ctf.Close
    End If

    Set otf = fso.OpenTextFile(ScriptFolder & "\" & FileNameStep & ".txt", 8)
    otf.Writeline "Current - " & CurStep
    otf.Close
Next

My text file output will be as below:

Current - A1
Current - A2
Current - A3

I am stuck at how to display the content of text file using message box.

I have also tried using array instead write it to txt file which more simpler that using txt file. My code as below:

For Each filelog In FileList
    LogFile = Split(filelog, "~")(1)

    Set otf = fso.OpenTextFile("C:\Data\" & LogFile, 1)

    l = 0
    MsgBox FileList.Count
    Do While l < FileList.Count 
        sFile = Split(LogFile, "_")
        CurStep = sFile(4)

        array.Add CurStep 

        l = l + 1
    Loop
Next

MsgBox Join(array, vbNewLine)

But got error. The error is at MsgBox Join() line:

Error: Invalid procedure call or argument


Solution

  • After you have written the data to your text file you can close it and then follow these steps:

    A. Open the file in Read mode again and set an object reference to it:

    Set objFile = fso.OpenTextFile(ScriptFolder & "\" & FileNameStep & ".txt",1)
    

    B. Read the contents of the file using the readall() method and store it in a variable:

    tempData = objFile.readAll()
    

    C. Close the file and Display the contents using 'Msgbox'

    objFile.Close
    MsgBox tempData
    

    If you want to display the data in text file, line-by-line you can use the readline() method and modify step B to:

    While not fso.atEndOfStream
        tempData = fso.readline()
        Msgbox tempData
    Wend
    

    Edit 2: For the second part of your question:

    You should not use the word "array" as a variable name as it is a keyword in vbscript. Also, you do not add elements in an array using .Add as we are talking about arrays here not arraylists.

    You can replace your code with the following:

    Dim intCtr: intCtr=-1
    Dim tempArr()
    For Each filelog in FileList    
    LogFile = Split(filelog, "~")(1)
    
    Set otf = fso.OpenTextFile("C:\Data\" & LogFile, 1)
    
         l = 0
        msgbox FileList.Count
        do while l < FileList.Count 
            intCtr=intCtr+1
            sFile = Split(LogFile, "_")
            CurStep = sFile(4)
            Redim preserve tempArr(intCtr)
            tempArr(intCtr)=CurStep 
            l = l + 1
        Loop
    next
    
    MsgBox Join(tempArr, vbNewLine)