0
votes

I am trying to return an ArrayList from a function in VBScript. The code goes through mails and extracts the Subject and Sender from them and stores them into An ArrayList. Then I Set the ArrayList to the name of the function to return it. But no returns are being output as arguments.

I execute the VBScript through CMD and only shows

C:\Users\XXXXXX\Documents>cscript DetailSaver.vbs Microsoft (R) Windows Script Host Version 5.812 Copyright (C) Microsoft Corporation. All rights reserved.


Function getDetails()

    Set outlook = createobject("outlook.application")
    Set session = outlook.getnamespace("mapi")
    session.logon

    Set inbox = session.getdefaultfolder(6).Folders("Clasification")
    Set newFolder = inbox.Folders("Devs")
    Set detailList = CreateObject("System.Collections.ArrayList")
    Dim emailSubject
    Dim emailSender
    Dim emailObject

    For Each m In newFolder.items
        If m.unread  Then 

            emailSubject = m.Subject
            emailSender = m.Sender.GetExchangeUser().PrimarySmtpAddress
            emailObject = emailSender & "|.|" & emailSubject
            detailList.Add emailObject

            m.Unread = False
        End If
    Next

    session.logoff

    Set outlook = Nothing
    Set CaseTitle = Nothing
    Set session = Nothing
    'Set detailList = Nothing

    Set getDetails = detailList

End Function

Call getDetails()

WScript.Quit



1
When run, this calls the getDetails() function, then exits. It doesn't do anything with the returned list. What are you expecting? Also, note that using System.Collections.ArrayList will only work on systems with .Net 2 or 3.5 on them. It will fail on systems with just .Net 4.x (like Win10).Why not use a type that VBScript understands natively, like Dictionary? - Rno
Why use System.Collections.ArrayList at all and not VBScript's native Array? - Hel O'Ween
@ArnovanBoven I am trying to output the ArrayList to use it as input argument by other software. The other software captures the output argument of the vbscript execution. I can print in function as wscript.echo join(detailList.ToArray(), vbCrLf) but the Windows Script Host Window appears when executed. I was trying to return value without using echo. & Thanks! For sure will try Dictionary - silver_lynx
@HelO'Ween Not sure why I went with System.Collections.ArrayList will try with VBScript native Arrays :) - silver_lynx
@silver_lynx I am not sure you can return a value from VBScript other than an Integer: WScript.Quit someExitCode, where someExitCode is an Integer. It's been ages since I used it so I may be wrong. PowerShell can do it. Or you could perhaps write the desired output to some file. - Rno

1 Answers

0
votes

at the end of your script the command Call is eating up the output and sending it nowhere. Instead of Call, use a new variable name.

arrData = getDetails()

But then did you want to output it's values?

For Each strArrayEntry In arrData
    'This will loop each item in the array
    Wscript.Echo strArrayEntry
Next