Trying to loop through strings

Arg81

Well-known member
Joined
Mar 11, 2005
Messages
949
Location
Midlands, UK
Programming Experience
1-3
I won't go into too much detail because I don't think many people are using Outlook Redemption (from a previous post I put up ages ago with no replies ;) )

I have 4 comboboxes that drop down to list names.

I want to be able to store them 4 names as some kind of array, and then loop through them using a For Each....Next statement.

I'm stuck at storing the combobox text to the array (or whatever I should be using for this) , as I have never done anything like this before!

Any advice appriciated!!
 
OK I've made a start, but I need to work out how to keep each value seperate...

Using "pseudocode", here's what I'm doing

VB.NET:
Dim strArray As New ArrayList
        strArray.Add("Captain Caveman")
        strArray.Add("Captain Hero")
        strArray.Add("Captain Scarlet")

Dim objArray as Object

For Each objArray in strArray

Try
    send email to name listed in array
Catch
   message show to user "email not sent"
End Try

Next

The email goes to the first name in the array, but then fails from there. I assume it's because it doesn't realise that the next name on the list is a new entity as such... ?
 
I've now got that one bit further!!

I can now display each "item", but for some reason the email isn't being sent to each one in the For Each loop...

I don't really want to post the whole code as most of it is pretty useless to this question, but here's what is in the sub:

VB.NET:
Dim oApplication As New Outlook.Application
        Dim SafeItem, oItem
        SafeItem = CreateObject("Redemption.SafeMailItem")
        oItem = oApplication.CreateItem(0)
        SafeItem.Item = oItem

        Dim objsession As Redemption.RDOSession
        objsession = CreateObject("Redemption.RDOSession")
        objsession.Logon()

        Dim strArray As New ArrayList
        strArray.Add("Captain Caveman")
        strArray.Add("Captain Hero")
        strArray.Add("Captain Scarlet")

        Dim i As Integer

        For i = 0 To strArray.Count - 1
            Dim strEmailMe As String = (CStr(strArray.Item(i)))
            Console.WriteLine(strEmailMe)
            Try
                Dim strAddress = objsession.AddressBook.GAL.ResolveName(strEmailMe)
                'continue here with sending the email to those names that were resolved OK
                Dim strEmail As String = "hello"

                With SafeItem
                    .Subject = "This is me trying to be a clever programmer!!!"
                    .To = strEmailMe
                    .BodyFormat = OlBodyFormat.olFormatHTML
                    .HTMLBody = strEmail
                    .Send()
                End With
            Catch ex As System.Exception
                MessageBox.Show("An email was not sent to: " & strEmailMe)
            End Try

        Next

Again, the first array item gets the email, but then it fails so the error is Caught. Not sure why it won't loop through and send the email to that user (the user does exist - if I move them around so that Captain Hero is the first in the array, he is emailed but Caveman isnt ;) )
 
I used to do something like this in Outlook VBA, but I'll be damned if I can find it :mad:

My only suggestion is that you have to create a new SafeItem for every e-mail you send. So clear it after you send the first e-mail, and then initialise it again.
 
spot on!

I moved all the initial declarations inside the For Each loop and now it works a treat!

VB.NET:
Dim i As Integer
        For i = 0 To strArray.Count - 1
            Dim strEmailMe As String = (CStr(strArray.Item(i)))
            Dim oApplication As New Outlook.Application
            Dim SafeItem, oItem
            SafeItem = CreateObject("Redemption.SafeMailItem")
            oItem = oApplication.CreateItem(0)
            SafeItem.Item = oItem
            Dim objsession As Redemption.RDOSession
            objsession = CreateObject("Redemption.RDOSession")
            objsession.Logon()
            'Console.WriteLine(strEmailMe)
            Try
                Dim strAddress = objsession.AddressBook.GAL.ResolveName(strEmailMe)
                'continue here with sending the email to those names that were resolved OK
                Dim strEmail As String = "hello"
                With SafeItem
                    .Subject = "This is me trying to be a clever programmer!!!"
                    .To = strEmailMe
                    .BodyFormat = OlBodyFormat.olFormatHTML
                    .HTMLBody = strEmail
                    .Send()
                End With
            Catch ex As System.Exception
                MessageBox.Show(ex.Message)
                MessageBox.Show("An email was not sent to: " & strEmailMe)
            End Try
        Next

You are a legend! Headache over, I finally have some email validation!! :D
 
You should be able to take these parts out of the loop and only run them once :-

VB.NET:
            Dim oApplication As New Outlook.Application
            Dim objsession As Redemption.RDOSession
            objsession = CreateObject("Redemption.RDOSession")
            objsession.Logon()

It may make it run a bit faster ;)
 
Back
Top