Want to access "New Mail" editor fields of outlook in vb.net

Hi,
        i'm working on outlook mail programming. My problem is i want to get the objects of email fields, e.g. in "To..." section where we type email address , i want to get the object of that text box so that i can easily access the text(or email address) in my code. Such like that in "cc...", "Subject", "Body" ,I want their objects too.
         Actually i'm making a table having 4 fields (to,cc,subject and body). Now i want that when i type somethin in above 4 fields in outlook new mail and then click send button (or my own built) the text will be saved in my database.
Please help me.........Tongue Tied


Answer this question

Want to access "New Mail" editor fields of outlook in vb.net

  • Moussa El-tayeb

    hi Obertanner
      thank you very much for your correspondence. now i have another couple of  problems. 
    1. I want to permanently delete mails from outlook programmitically.

    2. The other problem is i want to access the outlook "address book" through my   program such that it will return phone nos of selected contacts.

    3. i created the registry key using vb.net and give it name and data but the problem is data is store in string format and i want to store data in binary format so how it is possible. below is some line of codes

            Registry.CurrentUser.CreateSubKey(stkey).SetValue("uname", "hello")
            Registry.CurrentUser.CreateSubKey(stkey).SetValue("pass", "password")
    i want this hello and password to be store in binary format


    Please help me i will be very thankful to you



  • jatwood

    Hello sahab,

    here is another approach to handle commandBarButtons for Explorers.
    The technic is just the same like for the Inspectors.
    There are always 2 Classes where i call a wrapper - a Collection (Hashtable) wich holds the customized (Explorer / Inspector... or whatever) in memory until i close.
    When the item is closed an event is sent and it is removed from memory.
    The difference is, that every customized Item has a unique ID and i can differentiate from wich Item the event comes.
    I'm sure that when you have more explorers are open, on other sample are 2 items are opened.
    However, here is the expanded code to the VB solution above.
    Well - It's a quick hack, i'm a C# programmer :-)

    ------------------ ThisApplication.vb -------------------------------------

    public class ThisApplication

    Dim _ActiveItems As New ActiveInspectors

    Dim _ActiveExplorers As New ActiveExplorers

    WithEvents _Inspectors As Outlook.InspectorsClass

    WithEvents _Explorers As Outlook.ExplorersClass

    Private Sub ThisApplication_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

    _Inspectors = CType(Me.Inspectors, Outlook.InspectorsClass)

    _Explorers = CType(Me.Explorers, Outlook.ExplorersClass)

    For Each Explorer As Outlook.Explorer In _Explorers

    AddNewExplorer(Explorer)

    Next

    End Sub

    Private Sub ThisApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

    End Sub

    Private Sub _Inspectors_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles _Inspectors.NewInspector

    Dim item As Object

    item = Inspector.CurrentItem

    If item.MessageClass = "IPM.Note" Then

    Dim x4uItem As X4UMailItem

    x4uItem = New X4UMailItem

    x4uItem.Initialize(item)

    _ActiveItems.Add(item)

    End If

    End Sub

    Private Sub _Explorers_NewExplorer(ByVal Explorer As Microsoft.Office.Interop.Outlook.Explorer) Handles _Explorers.NewExplorer

    AddNewExplorer(Explorer)

    End Sub

    Private Sub AddNewExplorer(ByVal Explorer As Microsoft.Office.Interop.Outlook.Explorer)

    Dim x4uExplorer As X4UExplorer

    x4uExplorer = New X4UExplorer

    x4uExplorer.Initialize(Explorer)

    _ActiveExplorers.Add(x4uExplorer)

    End Sub

    End Class

    ------------------ ActiveExplorers.vb -------------------------------------

    Public Class ActiveExplorers

    Dim _ActiveExplorers As New Hashtable(25)

    Public Sub Add(ByVal Item As X4UExplorer)

    AddHandler Item.Close, AddressOf CloseEventHandler

    _ActiveExplorers.Add(Item.ID, Item)

    End Sub

    Sub CloseEventHandler(ByVal ID As String)

    _ActiveExplorers.Remove(ID)

    End Sub

    End Class

    ------------------ X4UExplorer.vb -------------------------------------

    Imports Outlook = Microsoft.Office.Interop.Outlook

    Imports Office = Microsoft.Office.Core

    Public Class X4UExplorer

    WithEvents _Explorer As Outlook.Explorer

    WithEvents _Button As Office.CommandBarButton

    Public Event Close(ByVal id As String)

    Public ReadOnly Property ID() As String

    Get

    ID = _ID

    End Get

    End Property

    Dim _ID As String

     

    Public Sub Initialize(ByVal explorer As Outlook.Explorer)

    _Explorer = explorer

    _ID = System.Guid.NewGuid.ToString

    CreateToolBar()

    End Sub

     

    Private Sub CreateToolBar()

    Dim commandBar As Office.CommandBar

    For Each bar As Office.CommandBar In _Explorer.CommandBars

    If bar.Name = "X4UBar" Then

    commandBar = bar

    For Each control As Office.CommandBarControl In commandBar.Controls

    control.Delete(False)

    Next

    Exit For

    End If

    Next

    If commandBar Is Nothing Then

    commandBar = _Explorer.CommandBars.Add("X4UBar", Office.MsoBarPosition.msoBarTop, False, True)

    End If

    Dim ctrl As Office.CommandBarControl

    ctrl = commandBar.Controls.Add(Office.MsoControlType.msoControlButton, 1, , , 1)

    _Button = CType(ctrl, Office.CommandBarButton)

    _Button.Caption = "X4U"

    _Button.Tag = _ID

    _Button.FaceId = 24

    _Button.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption

    _Button.Visible = True

    commandBar.Visible = True

    End Sub

     

    Private Sub _Button_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _Button.Click

    If _Button.Tag = _ID Then

    Dim newMailItem As Outlook.MailItem

    newMailItem = _Explorer.Application.CreateItem(Outlook.OlItemType.olMailItem)

    newMailItem.Display(False)

    End If

    End Sub

     

    Private Sub _Explorer_Close() Handles _Explorer.Close

    _Explorer.CommandBars("X4UBar").Delete()

    _Explorer = Nothing

    RaiseEvent Close(_ID)

    End Sub

    End Class

    ------------------ ActiveInspectors.vb -------------------------------------

    Public Class ActiveInspectors

    Dim _ActiveItems As New Hashtable(25)

    Public Sub Add(ByVal Item As X4UMailItem)

    AddHandler Item.Close, AddressOf CloseEventHandler

    _ActiveItems.Add(Item.ID, Item)

    End Sub

    Sub CloseEventHandler(ByVal ID As String)

    _ActiveItems.Remove(ID)

    End Sub

    End Class

    ------------------ X4UMailItem.vb -------------------------------------

    Imports Outlook = Microsoft.Office.Interop.Outlook

    Imports Office = Microsoft.Office.Core

    Public Delegate Sub CloseEventHandler(ByVal id As String)

    Public Class X4UMailItem

    WithEvents _Item As Outlook.MailItem

    WithEvents _Inspector As Outlook.InspectorClass

    WithEvents _Button As Office.CommandBarButton

    Public Event Close(ByVal id As String)

    Public ReadOnly Property ID() As String

    Get

    ID = _ID

    End Get

    End Property

    Dim _ID As String

     

    Public Sub Initialize(ByVal item As Outlook.MailItem)

    _Item = item

    _ID = _Item.GetHashCode.ToString

    End Sub

    Private Sub _Item_ItemEvents_Event_Open(ByRef Cancel As Boolean) Handles _Item.Open

    _Inspector = CType(_Item.GetInspector, Outlook.InspectorClass)

    CreateToolBar()

    End Sub

     

    Private Sub _Inspector_InspectorEvents_10_Event_Close() Handles _Inspector.InspectorEvents_10_Event_Close

    _Inspector.CommandBars("X4UBar").Delete()

    _Inspector = Nothing

    RaiseEvent Close(_ID)

    End Sub

    Private Sub CreateToolBar()

    Dim commandBar As Office.CommandBar

    For Each bar As Office.CommandBar In _Inspector.CommandBars

    If bar.Name = "X4UBar" Then

    commandBar = bar

    For Each control As Office.CommandBarControl In commandBar.Controls

    control.Delete(False)

    Next

    Exit For

    End If

    Next

    If commandBar Is Nothing Then

    commandBar = _Inspector.CommandBars.Add("X4UBar", Office.MsoBarPosition.msoBarTop, False, True)

    End If

    Dim ctrl As Office.CommandBarControl

    ctrl = commandBar.Controls.Add(Office.MsoControlType.msoControlButton, 1, , , 1)

    _Button = CType(ctrl, Office.CommandBarButton)

    _Button.Caption = "X4U"

    _Button.Tag = _ID

    _Button.FaceId = 24

    _Button.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption

    _Button.Visible = True

    commandBar.Visible = True

    End Sub

     

    Private Sub _Button_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _Button.Click

    If _Button.Tag = _ID Then

    MsgBox("To: " + _Item.To + vbCrLf + "Subject: " + _Item.Subject)

    End If

    End Sub

    End Class



  • Ilske

    hi
       i have couple of problems:
    1.  i want to save mail in my custom folder without sending. i want to set its sent property to True but i m unable to access this property. How can i set my saved mail as "sent".
     
    2. i accessed the "To", "Body" and "CC" fields but i cant access the "From" field of mail message. actually i cannot found the object or event of "From" field.

    Note 
    'declare a classvariable
    with events _Folder as MAPIFolder

    'Get the Folder
    _Folder = session.GetDefaultFolder olFolderDeletedItems

    use the Folder.Items_Add event

    Mr. helmut ,the above solution is not valid as there is no
    withevents for MAPIFolder.


  • potter12

    Mr. Sahab

    I am doing just that.  Here is some code in vb.net:
    Here I create the button:

    _FCBFolderCommandBarButton = _FCBCommandBar.Controls.Add(Type:=Office.MsoControlType.msoControlButton, Temporary:=True)

    With _FCBFolderCommandBarButton

    .Caption = "FCB CMT"

    .Style = Office.MsoButtonStyle.msoButtonIconAndCaption

    .FaceId = 2126

    .TooltipText = "Open CMT EMail"

    .BeginGroup = False

    .Tag = "CMT.ADDIN.FOLDER"

    .OnAction = "ProcessEmail"

    .Visible = True

    End With

    Here is the event code:

    Public Sub ProcessEmail()

    Dim note As Outlook.MailItem

    Dim folder As Outlook.MAPIFolder

    Try

    folder = mApp.ActiveExplorer.CurrentFolder()

    note = folder.Items.Add("IPM.Note")

    note.Display()

    Catch ex As Exception

    WriteException(ex)

    End Try

    End Sub

    Hope this helps.
    Ignus


  • ChrisRichardson

    Thax Mr. Helmut
    I found Solution for my two problems olny Rest two are still to be solved. Actually solution provide by you is there already but not working.     
    The details is:

    1) I Want to Replace Send button from "New mail message" Inspector.
    while your solution is for Explorer.

    2) I have already writen code at 
    OnDisconnection or (Shutdown in VSTO) do a
    GC.Collect() and GC.WaitForPendingFinalizers()
    But In vain. what could be the problem. 

    3) how can i get the Items.Add event of the Delete Items Folder.

    Thank you again
      


  • Peter Poirier

    Hello mr. sahab,

    1. loop over the deleted folder items and delete
    or try to get the commandbar menutiem and execute if you have a visible userinterface
    Maybe it's also possible with Redemtion Library, don't know...

    2. since I'm more familiary with C# I suggest you to search on Sue Moshers site for a solution.
    Accessing the Addressbook could not be done directly from Outlook Object Model.
    Using CDO will popup security prompts.
    So I will highly recommend using the redemption library for showing AddressBook and access Recipientinformation.

    Here is a VB.Net code snippet...
    http://www.outlookcode.com/threads.aspx forumid=5&messageid=12079

    3. 
    (a): I would never save any unencrypted username and password data anywhere.

    (b): You could convert the string to binary and back , see here
    http://www.chilkatsoft.com/faq/DotNetStrToBytes.html

    (c): With .Net it's recommended not to use the Registry, e.g. on another Plattform maybe there is no Registry, Xcopy deployment, etc.
    With VSTO better use the Settings already provided with your Solution.
    In the VSTO Solution click on My Project and go to Settings.
    Here you can add Applicationwide and User settings.

    In code you can read / write it very easy like this:

    Dim Username As String = My.Settings.Username
    Dim Password As String = My.Settings.Password


    My.Settings.Password = "newPassword"

    My.Settings.Save()



    Hope this helps,
    greets, Helmut







  • arunvenkata

    Thank you Helmut Obertanner.

    Your code has solved my problem. 

    Now i have a problem in the same application i.e: i created my button in explorer's standard bar. I want to open a "new mail message" by clikcing my button . I am unable to find that (when i click my button it should call the built-in event behind "new mail message" button). 
      
    Thank you again and hope you don't mind helping me.
        

  • Gabriel Gheorghiu

    Hello farhad,

    1. sample above is for Explorer & for Inspector.

    2. No more idea.

    3. get the defaulFolder  from session object:

    'declare a classvariable
    with events _Folder as MAPIFolder

    'Get the Folder
    _Folder = session.GetDefaultFolder olFolderDeletedItems

    use the Folder.Items_Add event

    hope this helps,
    greets, Helmut

  • Sonmez

    Hello Farhad,

    1. When you only want to delete your item, you must trap which item was deleted.

    Maybe cou can catch the Item.BeforeDelete event and save some Information into the Mileage field.

    Monitor the Items.Add event of the Delete Items Folder and do a restrict of Mileage = yoursavedinfo.

    2. When create your ToolBar in Explorer, search the New Mail Message by ID and set visible to false, create your own button instead and register for the Click event.

    3. Loop over the CommandBars Collection and check the Name of each CommandBar if it's yours. (see sample above)

    4. at OnDisconnection or (Shutdown in VSTO) do a GC.Collect() and GC.WaitForPendingFinalizers()

    Here are some of my samples wich shows you some of this technics.
    http://www.x4u.de/Outlook/Samples/tabid/77/Default.aspx

    also see
    http://www.outlookcode.com


    Greets, Helmut Obertanner
    [http://www.x4u.de]

  • ucolinfo

    thank you mr. Obertanner
    I am using vb.net. 
    Actually you have used the mailitem_send event that is provided by classes already.
    My problem is actualy that i have to add a custom button in new mail inspector.This button have a code to insert the newly typed mail, in databse. i have added button sucessfully but when i write code to access the mail (currently i m typing), it gives me nothing. my code piece is: 

    private  sub MYCommandButton_click(byref ctrl as commandbarbutton) handles  MYCommandButton.click

    m_olInspector = M_olapp.activeInspector   
    m_oImailitem = ctype(m_olInspector.curritem,outlook.mailitemclass)

    'Error starts from here...
    'when i want to get Fields of mail e.g. To, CC, BCC, Subject, Body etc.
    'It does not show me the data from fields even fields have data.
     
    msgbox (m_oImailitem.To)
    msgbox (m_oImailitem.CC)
    msgbox (m_oImailitem.subject)
    msgbox (m_oImailitem.body)

    end sub      


  • DICKDDD

    Hello sahab,

    i don't know wich programming language you use, i'm using C#
    You should create an Wrapper for the Mail Item and trap the item send event.
    This could be done within an OUtlook AddIn.

    On my site i have a VSTO Sample that shows some technics programming the Outlook Object Modell.
    Maybe this could help you.

    http://www.x4u.de/Outlook/Samples/tabid/77/Default.aspx

    take also a look at

    http://www.outlookcode.com

    there are a lot of samples and information for Outlook programmers.

    Greets, Helmut Obertanner

  • Merovingian

    Hello mr. sahab,

    you are right, i can reproduce with VB.Net.
    I can only access changed fields when the item was saved or the addressselector has been opened.

    But i'm not a good VB programmer

    VSTO Sample:

    public class ThisApplication

    Dim _ActiveItems As New ActiveInspectors

    WithEvents _Inspectors As Outlook.InspectorsClass

    Private Sub ThisApplication_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

    _Inspectors = CType(Me.Inspectors, Outlook.InspectorsClass)

    End Sub

    Private Sub ThisApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

    End Sub

    Private Sub _Inspectors_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles _Inspectors.NewInspector

    Dim item As Object

    item = Inspector.CurrentItem

    If item.MessageClass = "IPM.Note" Then

    Dim x4uItem As X4UMailItem

    x4uItem = New X4UMailItem

    x4uItem.Initialize(item)

    _ActiveItems.Add(item)

    End If

    End Sub

    End Class

    Public Class ActiveInspectors

    Dim _ActiveItems As New Hashtable(25)

    Public Sub Add(ByVal Item As X4UMailItem)

    AddHandler Item.Close, AddressOf CloseEventHandler

    _ActiveItems.Add(Item.ID, Item)

    End Sub

    Sub CloseEventHandler(ByVal ID As String)

    _ActiveItems.Remove(ID)

    End Sub

    End Class

    Imports Outlook = Microsoft.Office.Interop.Outlook

    Imports Office = Microsoft.Office.Core

    Public Delegate Sub CloseEventHandler(ByVal id As String)

    Public Class X4UMailItem

    WithEvents _Item As Outlook.MailItem

    WithEvents _Inspector As Outlook.InspectorClass

    WithEvents _Button As Office.CommandBarButton

    Public Event Close(ByVal id As String)

    Public ReadOnly Property ID() As String

    Get

    ID = _ID

    End Get

    End Property

    Dim _ID As String

     

    Public Sub Initialize(ByVal item As Outlook.MailItem)

    _Item = item

    _ID = _Item.GetHashCode.ToString

    End Sub

    Private Sub _Item_ItemEvents_Event_Open(ByRef Cancel As Boolean) Handles _Item.Open

    _Inspector = CType(_Item.GetInspector, Outlook.InspectorClass)

    CreateToolBar()

    End Sub

     

    Private Sub _Inspector_InspectorEvents_10_Event_Close() Handles _Inspector.InspectorEvents_10_Event_Close

    _Inspector.CommandBars("X4UBar").Delete()

    _Inspector = Nothing

    RaiseEvent Close(_ID)

    End Sub

    Private Sub CreateToolBar()

    Dim commandBar As Office.CommandBar

    For Each bar As Office.CommandBar In _Inspector.CommandBars

    If bar.Name = "X4UBar" Then

    commandBar = bar

    For Each control As Office.CommandBarControl In commandBar.Controls

    control.Delete(False)

    Next

    Exit For

    End If

    Next

    If commandBar Is Nothing Then

    commandBar = _Inspector.CommandBars.Add("X4UBar", Office.MsoBarPosition.msoBarTop, False, True)

    End If

    Dim ctrl As Office.CommandBarControl

    ctrl = commandBar.Controls.Add(Office.MsoControlType.msoControlButton, 1, , , 1)

    _Button = CType(ctrl, Office.CommandBarButton)

    _Button.Caption = "X4U"

    _Button.Tag = _ID

    _Button.FaceId = 24

    _Button.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption

    _Button.Visible = True

    commandBar.Visible = True

    End Sub

     

    Private Sub _Button_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _Button.Click

    If _Button.Tag = _ID Then

    MsgBox("To: " + _Item.To + vbCrLf + "Subject: " + _Item.Subject)

    End If

    End Sub

    End Class





  • Alexander Klizhentas

    Hello Mr. Helmut,

    I am facing these Problems.

    1) wheni delete a mail item it goes to "Deleted Items" Folder. Now i want to delete the item from "Deleted Items" too. How can i find my Particular Item from "Deleted Items".
        
    2) I want to add a custom button in "new mail message". This button should be placed at the position of Built In Send Button.
    Actually i want to add another send button for my custom coding.

    3) i am adding my custom commandbar in Outlook Explorer.When i try to Check if my bar is already there or not, It gives me error.
    code is:
    m_olActiveExplorer.ComandBars.Item("CustomBar")        

    4) When i close my Outlook and start it again imediately. It gives me message that "Operation Failed". Can anybody tell me the reason behind that.

    Thanks in  advance.
    Professional



  • Peds


     Helmut Obertanner wrote:


    thank you mr. Obertanner
    I am using vb.net. 
    Actually you have used the mailitem_send event that is provided by classes already.
    My problem is actualy that i have to add a custom button in new mail inspector.This button have a code to insert the newly typed mail, in databse. i have added button sucessfully but when i write code to access the mail (currently i m typing), it gives me nothing. my code piece is: 

    private  sub MYCommandButton_click(byref ctrl as commandbarbutton) handles  MYCommandButton.click

    m_olInspector = M_olapp.activeInspector   
    m_oImailitem = ctype(m_olInspector.curritem,outlook.mailitemclass)

    'Error starts from here...
    'when i want to get Fields of mail e.g. To, CC, BCC, Subject, Body etc.
    'It does not show me the data from fields even fields have data.
     
    msgbox (m_oImailitem.To)
    msgbox (m_oImailitem.CC)
    msgbox (m_oImailitem.subject)
    msgbox (m_oImailitem.body)

    end sub      



  • Want to access "New Mail" editor fields of outlook in vb.net