New and Lost - Outlook Add-In in C#

Add-in Express™ Support Service
That's what is more important than anything else

New and Lost - Outlook Add-In in C#
Converting VB script to C#.NET with Add-In Express 
Karissa Story




Posts: 3
Joined: 2006-08-31
I am extremely new to C# and do not have a familiarity with its syntax. I purchased this tool in order to be able to not have to worry about all the back-end and extremely technical issues related to packaging and deploying COM Add-ins for multiple versions of Outlook. I have a VB Macro that does what I need; however, it needs to be turned into a C# (our standard) COM Add-In so that it is more stable and can be packaged and deployed. I have no clue how to do this. Any help would be monumentally appreciated because I do not know where to go as a source for C# handling Outlook host application events. The VB Script is below:



'=============================================================
' Purpose : Prompts the user to choose the storage folder for
'           an outgoing item
' Notes   : If this routine is present in the built-in
'           ThisOutlookSession module, it will run
'           automatically when Outlook sends an item.
'           Uses the IsInDefaultStore() function
'           to ensure that the new folder is in
'           the default store, as Outlook requires.
'-------------------------------------------------------------
' Arguments :
'-----------
' Item (Object)     = item being send
' Cancel (Boolean)  = set to True to cancel the send
'=============================================================
Private Sub Application_ItemSend(ByVal Item As Object, _
                                Cancel As Boolean)
   Dim objNS As Outlook.NameSpace
   Dim objFolder As Outlook.MAPIFolder
   On Error Resume Next
   If TypeName(Item) <> "MailItem" Then Exit Sub
   Set objNS = Application.GetNamespace("MAPI")
   If MsgBox("Do you want to save this outgoing email in Outlook?", _
             vbYesNo, "Save?") = vbNo Then
       Set objFolder = objNS.GetDefaultFolder(olFolderDeletedItems)
       Set Item.SaveSentMessageFolder = objFolder
       Exit Sub
   End If
DoAgain:
   Set objFolder = objNS.PickFolder
   If Not objFolder Is Nothing Then
       If IsInDefaultStore(objFolder) Then
           Set Item.SaveSentMessageFolder = objFolder
       Else
          MsgBox "That folder is not allowed"
          GoTo DoAgain
       End If
   Else
       Set objFolder = objNS.GetDefaultFolder(olFolderDeletedItems)
       Set Item.SaveSentMessageFolder = objFolder
   End If
   
   Set objFolder = Nothing
   Set objNS = Nothing
End Sub


'=============================================================
' Purpose : Test whether an item or folder is in the default
'           information store for the current profile
'-------------------------------------------------------------
' Arguments :
'-----------
' objOL (Object)    = item or folder to be tested
'-------------------------------------------------------------
' Returns : True if item or folder is in default store
'           False if item or folder is not in default store
'               or if object tested is not an item or folder
'=============================================================
Public Function IsInDefaultStore(objOL As Object) As Boolean
   Dim objApp As Outlook.Application
   Dim objNS As Outlook.NameSpace
   Dim objInbox As Outlook.MAPIFolder
   On Error Resume Next
   
   Set objApp = CreateObject("Outlook.Application")
   Set objNS = objApp.GetNamespace("MAPI")
   Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
   IsInDefaultStore = False 'Unless changed to True below
   Select Case objOL.Class
       Case olFolder
           If objOL.StoreID = objInbox.StoreID Then
               IsInDefaultStore = True
           End If
       Case olAppointment, olContact, olDistributionList, _
            olJournal, olMail, olNote, olPost, olTask
           If objOL.Parent.StoreID = objInbox.StoreID Then
               IsInDefaultStore = True
           End If
   End Select
   
   Set objApp = Nothing
   Set objNS = Nothing
   Set objInbox = Nothing
End Function


Posted 31 Aug, 2006 09:47:01 Top
Sergey Grischenko


Add-in Express team


Posts: 7224
Joined: 2004-07-05
Hi Karissa.

Just right click on the addinmodule designer, choose the 'Properties option' and go to the Properties window. Click the Events tab to add events of the add-in module.
You can also add specific events of Outlook application via the 'Add events' option of the context menu of the addinmodule designer.
Posted 31 Aug, 2006 16:52:30 Top