Solved

Object to array

Posted on 2016-10-12
7
45 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
No Data for DropDown List 2 32
ASP.NET (VB) return a record 2 52
VS 2015 is giving [No relevant source lines] 2 40
reading an excel file vb.net 2 20
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

726 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