Solved

Object to array

Posted on 2016-10-12
7
42 Views
Last Modified: 2016-11-07
Is it possible to turn an object to an array in VB.net?

I have a object from outlook which is a mailitem, and I want to turn all the variables into an array to store in a file.

I know Ill loose the functionality of the object (such as move, copy etc) however i only want to store all the information in the object (in this case mailID, recipents etc).

Usually I could just count the elements and then copy the values one at a time, however the mailitem is a nested array inside a nested array inside a nested array so could be quite confusing.

Any ideas?
0
Comment
Question by:tonelm54
  • 4
  • 3
7 Comments
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 41839660
I have a object from outlook which is a mailitem, and I want to turn all the variables into an array to store in a file.

yes, that's possible. you may want to consider to store it in a List(Of T) or look into the ToArray() method

List(Of T) Class
https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx
(look for the possible constructors and click on it to see the examples)

List<T>.ToArray Method ()
https://msdn.microsoft.com/en-us/library/x303t819(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
0
 

Author Comment

by:tonelm54
ID: 41841016
Couldnt get these to work :-(

So my code at the moment is:-
            Dim mail As Outlook.MailItem = item
            Dim output() As String = mail.toarray()

            Dim serializer As New JavaScriptSerializer()
            Dim arrayJson As String = serializer.Serialize(output)

            MsgBox(arrayJson)

Open in new window


Any ideas what Im doing wrong?
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 41841403
what info you wish to read from MailItem and put into JSON array ?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:tonelm54
ID: 41841448
Problem is, I dont know yet :-S

The data to store in the database (to search) keeps changing, so my plan was to store all the information in a JSON text file, then when a request comes along to add xxx to the search I can simply write some code to look at the JSON file and pull the requested field into the database and build the indexes for searching.

Initially they only wanted the `Date Received` and the `Subject`. Now its the `recipient` and `sender`, and already I've heard rumours they want the `conversationID` and Ive had requests to add `attachments` into the search. Hence store everything in a JSON text file and pull as needed, instead of trying to upload from the PST files again (which takes ages).
0
 

Author Comment

by:tonelm54
ID: 41841452
So looking at the element for mail (which is a mailtem from outlook) I get the following elements:-
Untitled.png
I did think I could loop through each element and store in an array via:-
Dim objElement As Object
For Each objElement in mail

Next

Open in new window


But I cant figure a way of reading all the elements inside mail.
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 41841464
perhaps it doesn't make sense if we are looping through all the elements in mailtem since it's quite complex to read through all its content, as back to the basic, we only store useful info into database, and hence we only extract useful info from the source.
0
 
LVL 51

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41841488
I come out with something like this, not too sure if this is useful for you.

Private Sub Test()
        Dim tempApp As Outlook.Application
        Dim tempInbox As Outlook.MAPIFolder
        Dim InboxItems As Outlook.Items

        tempApp = CreateObject("Outlook.Application")
        tempInbox = tempApp.GetNamespace("Mapi").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        InboxItems = tempInbox.Items

        Dim MailList As New List(Of MyMailItem)

        'Dim newMail As Outlook.MailItem
        For Each newMail In InboxItems
            If TypeOf newMail Is Outlook.MailItem Then
                'You may want to put some conditions here?
                If newMail.Subject.IndexOf("whatever") > 0 Then
                    Dim itm As New MyMailItem
                    itm.Subject = newMail.Subject
                    itm.ReceivedTime = newMail.ReceivedTime
                    itm.SenderName = newMail.SenderName
                    itm.SenderEmailAddress = newMail.SenderEmailAddress

                    For Each x As Outlook.Recipient In newMail.Recipients
                        Dim y As New MyRecipient()
                        y.Name = x.Name
                        y.EmailAddress = x.Address
                        itm.Recipient.Add(y)
                    Next
                    MailList.Add(itm)
                End If
            End If
        Next
        Dim serializer As New JavaScriptSerializer()
        Dim arrayJson As String = serializer.Serialize(MailList)
        MsgBox(arrayJson)
    End Sub

Open in new window

then you would need to have some classes like these:
Class MyRecipient
    Public Name As String
    Public EmailAddress As String
End Class

Class MyMailItem
    Public Subject As String
    Public ReceivedTime As String
    Public SenderName As String
    Public SenderEmailAddress As String
    Public Recipient As New List(Of MyRecipient)
End Class

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question