Adding fields on Outlook

celobateira

Member
Joined
Feb 19, 2007
Messages
18
Programming Experience
Beginner
Hi,

I'm using VB .Net 2005 and I want to add a new field on the Outlook view

like the fields 'From', 'Subject', 'Received' and 'Size'...

I have a clue that I may have to modify the XML View but I don't know how to do it

Can someone help me with this?

Thanx
 
I've seen that thread to... I found some code that I had to rewrite to VB2005 and it goes like this:

------------------------------------------------------------------
Private Sub AddColumnToView(ByVal strName As String, ByVal strHead As String, ByVal strProp As String)

Dim objView As Outlook.View
Dim olApp As Outlook.Application = CreateObject("Outlook.Application")
Dim objViews As Outlook.Views
Dim objXMLDoc As New XmlDocument
Dim objNode As XmlNode
Dim objRoot As XmlNode

On Error Resume Next

objViews = olApp.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).Views
objView = objViews.Add(strName, Outlook.OlViewType.olTableView, Outlook.OlViewSaveOption.olViewSaveOptionAllFoldersOfType)
objXMLDoc.LoadXml(objView.XML)
objRoot = objXMLDoc.CreateElement(
"column")

With objRoot
.AppendChild(objXMLDoc.CreateElement(
"Heading")).Value = strHead
.AppendChild(objXMLDoc.CreateElement(
"prop")).Value = strProp
.AppendChild(objXMLDoc.CreateElement(
"sort")).Value = "Copied"
.AppendChild(objXMLDoc.CreateElement("width")).Value = "50"
.AppendChild(objXMLDoc.CreateElement("type")).Value = "boolean"
End With
objNode = objXMLDoc.DocumentElement.InsertBefore(objRoot, objXMLDoc.DocumentElement.ChildNodes(3))
objView.XML = objXMLDoc.OuterXml
objView.Save()
objView.Apply()
End Sub
-------------------------------------------------------------

unfortunaly it doesn't insert anything for now... maybe because the source code had 'objView.XML = objXMLDoc.xml' instead of
'objView.XML = objXMLDoc.OuterXml'...

but when I write 'objXMLDoc.' it doesn't appear the attribute 'XML'

can you see what I'm doing wrong? Thnx
 
The code looks ok, except you insertbefore childnode3 of documentelement. If you compare with the last example here you see they insert it in relation to the "column" nodes of documentelement. Not sure how significant that is.

I tried the code but I only get intermixing "Value does not fall within the expected range." and RPC_E_SERVERFAULT errors with the Views.Add method. Without getting passed that it's hard to tell.

You say "doesn't insert anything" and this could mean you have problems with the xml insert or problem with the Views.Add also. You should try to run the code without the "ignorance is bliss" option (On Error Resume Next), maybe you also experience automation errors without knowing.
 
Does the code 'objView.XML = objXMLDoc.OuterXml' is the correct code to use?

I'm saying this because all the examples I read the code is
'objView.XML = objXMLDoc.XML, but if I use this code it gives me an error saying that the XML is not a member of System.Xml.XmlDocument

i've excluded the 'on error resume next' option but it didn't give me any error

PS: thnx for help me with this;)
 
I finnaly get the code to work..

I've made some changes like:

objRoot = objXMLDoc.DocumentElement
With objRoot.InsertBefore(objXMLDoc.CreateElement("column"), objRoot.SelectNodes("column").Item(5))

and:

.AppendChild(objXMLDoc.CreateElement("type")).InnerText = "boolean"

Now, since the type of the node is Boolean I'de like to insert some default chekboxes to false in all the mailitems.

Any clue how I do this?

thanx
 
How did you solve the "Value does not fall within the expected range"? I have installed and referenced the Primary Interop Assemblies (PIA) which seemed to solve the RPC_E_SERVERFAULT error, but I still get the "value" error with Views.Add method.
 
You have to remove the view because if you don't, when you're going to insert a View with the same name it gives that error.

I've solved this problem with objViews.Remove("Name_of_the_View") at OnBeginShutDown Method
 
Good on you, I'm back to the RPC_E_SERVERFAULT error again (both for Views.Add and Remove methods).
 
No, regular winapp project with Office automation through the PIAs.

I got one step further when I ran into another error with other automation code, namely fixing problem with English version Office on PC with other regional setting. Another step problem was solved by using the MAPI Namespace.Logon. Other automation codes are working Excel, Word and Outlook, for example I can automate adding a new contact to Outlook without error. I just don't get Views.Add/Remove. The View is actually added/removed from Outlook but the automation server still crashes on that very line of code.

Back to what you asked, you wanted to "add a new field on the Outlook view", so this must mean you want to edit current mailbox view, not add another view? First can be done for example by getting the current view and modify its Xml same as other code (this approach works here, though I have no idea yet how to correctly use it:)).
VB.NET:
Dim oNS As Outlook.NameSpace = olApp.GetNamespace("MAPI")
oNS.Logon(Reflection.Missing.Value, Reflection.Missing.Value, True, True)
Dim inbox As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
Dim view As Outlook.View = inbox.CurrentView 
'... the rest
oNS.LogOff()
 
I've already managed to insert the custom field... What I'm trying now is to

insert some Yes or No property in that field to all the mailitems.

I'm using this code:

Private Sub addCheckboxDefault()
'Get inbox folder so that we can apply the checkbox userproperty in the new inbox view.
Dim olApp As Outlook.Application = CreateObject("Outlook.Application")
Dim inbox As Outlook.MAPIFolder = olApp.ActiveExplorer(). _
Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
Dim emailItem As Outlook.MailItem
Dim objItems As Object
objItems = inbox.Items
'Loop through the emailitems and set userproperty to default value True.
For Each emailItem In objItems
With emailItem
emailItem.UserProperties.Add(
"Copied", Outlook.OlUserPropertyType.olYesNo, True)
emailItem.UserProperties.Item(
"Copied").Value = vbYes
emailItem.Save()
End With
Next
End Sub
---------------------------------------------------------------
I can insert the value correctly but I can't see nothing in the "Copied" field
I was expected to see some 'Yes' text in the field... like the 'Size' field that has some text about the mail size...
 
Your column name/heading is "Copied" and is of type "boolean"? (just to be sure) UserProperties.Item can't accessed by name string, not for my version at least, please take note that I am using Outlook XP, that is the '2002' version with object library version 10.

'vbYes' constant is not the same as the Outlook automation boolean YesNo property type. It is only incidental due to conversion rules that it will produce a True value. 'vbNo' contant will also translate to boolean True value. Use True/False instead.

When you added the user-property to the mail item you could have caught the return and used it to set the value:
VB.NET:
Dim up As Outlook.UserProperty 
up = mi.UserProperties.Add("udf", Outlook.OlUserPropertyType.olYesNo, False)
up.Value = True
 
' or
 
mi.UserProperties.Add("udf", Outlook.OlUserPropertyType.olYesNo, False).Value = True
Else you have to use the Find method or iterate fields, example:
VB.NET:
Dim up As Outlook.UserProperty
For Each mi As Outlook.MailItem In inbox.Items
    up = mi.UserProperties.Find("udf")
    If up IsNot Nothing Then
        up.Value = True
    Else
      mi.UserProperties.Add("udf", Outlook.OlUserPropertyType.olYesNo, False).Value = True
    End If
    mi.Save()
Next
In case you want to compare, here is how my 'udf' column look in the Xml code in the View:
HTML:
 <column>
  <heading>udf</heading>
  <prop>http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/udf</prop>
  <type>boolean</type>
  <width>47</width>
  <style>padding-left:3px;;text-align:left</style>
 </column>
 
Thanx JohnH that worked perfectly... yes my Heading is "Copied" and is Boolean, now I can see the checkboxes

I'll continue to work on this project so possibly I'll have more questions to ask about outlook add-ins ;).

Meanwhile any doubt you may have about this just ask... I'm not a VB

wizard but I'll help in what I can.

thank you
 
Back
Top