More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 22-Jan-2019 17:57

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

Code level: advanced    Code area: Outlook Expert Techniques Printer Friendly Version
Title: Set the default form for a folder (OL2007)
Description: This VBA sample for Outlook 2007 uses the new PropertyAccessor object to set the default form for a folder. Call it by passing the folder and message class string (e.g. "IPM.Contact.MyForm") as arguments. For a version appropriate for earlier versions, see http://www.outlookcode.com/codedetail.aspx?id=1303.
Date: 07-May-2007  12:27
Code level: advanced
Code area: Outlook Expert Techniques
Posted by: Sue Mosher
This message is displayed as VB.NET
 Sub SetFolderDefaultForm(fld As Outlook.Folder, _
                         msgClass As String)
    Dim objPA As Outlook.PropertyAccessor
    Dim strBaseType As String
    Dim strMsg As String
    Dim intLoc As Integer
    Dim blnBadForm As Boolean
    Dim arrSchema()
    Dim arrValues()
    Dim arrErrors()
    Dim i As Integer
    Const PR_DEF_POST_MSGCLASS = _
      "http://schemas.microsoft.com/mapi/proptag/0x36E5001E"
    Const PR_DEF_POST_DISPLAYNAME = _
      "http://schemas.microsoft.com/mapi/proptag/0x36E6001E"
    On Error Resume Next
    Select Case Left(UCase(msgClass), 8)
        Case "IPM.NOTE" ' cannot be default for any folder
            blnBadForm = True
        Case "IPM.POST" ' default only for mail/post folders
            If StrComp(fld.DefaultMessageClass, _
                       "IPM.NOTE", vbTextCompare) <> 0 Then
                blnBadForm = True
            End If
        Case Else
            If InStr(1, msgClass, fld.DefaultMessageClass, _
                     vbTextCompare) <> 1 Then
                blnBadForm = True
            End If
    End Select
    If Not blnBadForm Then
        intLoc = InStrRev(msgClass, ".")
        arrSchema = Array _
          (PR_DEF_POST_MSGCLASS, PR_DEF_POST_DISPLAYNAME)
        arrValues = _
          Array(msgClass, CStr(Mid(msgClass, intLoc + 1)))
        Err.Clear
        Set objPA = fld.PropertyAccessor
        arrErrors = objPA.SetProperties(arrSchema, arrValues)
        If Err <> 0 Then
            strMsg = "Error " & Err.Number & ": " & _
            Err.Description
        End If
        If Not (IsEmpty(arrErrors)) Then
            For i = 0 To UBound(arrErrors)
                If IsError(arrErrors(i)) Then
                    strMsg = strMsg & vbCrLf & _
                             arrSchema(i) & vbCrLf & _
                             CStr(arrErrors(i)) & " - " & _
                             Error(Mid(CStr(arrErrors(i)), 7))
                End If
            Next
        End If
    Else
        strMsg = msgClass & " cannot be used as the " & _
                 "default form for the """ & fld.Name & _
                 """ folder."
    End If
    If strMsg <> "" Then
        MsgBox strMsg, vbExclamation, _
               "Problem Processing Form Class"
    End If
    Set objPA = Nothing
End Sub
All 44comments
Page [ 1 2 3 4 5 Next >>  
  12-Oct-2007  03:08   
Hello, I'm just started to customized OL2007 and I tried your function to set my new Contact Form as default... I didn't get it working yet :-( Could you be more specific regarding the call of this function? Where and how do I have to call it? Thanks for your help...
  12-Oct-2007  06:20   
This is VBA code, so you would call it from a VBA macro, passing a Folder object and form message class string as arguments.
  18-Oct-2007  01:57   
That's what I did... I copied your function in Outlook's Macro und also created a new function in ThisOutlookSession called Application_Startup() which pass the contact folder to your function. But it's not working sofar... I'm not an expert with Macros and I would appriciate some help. Thank you in advance...
  18-Oct-2007  07:48   
Does any VBA code run at all? Have you looked at the basics at http://outlookcode.com/article.aspx?id=49 ? What happens when you step through the code? If you comment out the On Error Resume Next statement, do you get any errors? Exactly how are you calling the function -- with what parameters?
  22-Oct-2007  03:47   
Thanks, looks like it's working now... But old contacts still open in the previous default window! Do you have any idea how to change the "contact window" for existing contacts?
  22-Oct-2007  07:30   
Changing the default form for a folder has no effect on existing items. To change existing items so that they display using a published custom message form, you need to change the value of the MessageClass property on each item so that it points to the published form's class, e.g. IPM.Contact.MyForm. The page at http://www.outlookcode.com/article.aspx?id=39 has links to scripts and tools to do this.
 
  03-Jul-2008  16:53   
I tried to call this from VBScript and VB Executable to deploy new form to corporate users. After it successfully sets the form for calender it reports error saying "http://schemas.microsoft.com/mapi/proptag/0x36E5001E
Error 5 - Invalid Procedure call or argument"

I tried to debug the code and identified something goes wrong on arrErrors = objPA.SetProperties(arrSchema, arrValues) statement and control falls into Error condition.

fails right
Please help me on this. OL 2003 version works just fine.
  03-Jul-2008  17:27   
Selvam, are you using this code with Outlook 2007? What folder are you trying to change? You can read more about the conditions under which SetProperties may raise an error at http://msdn.microsoft.com/en-us/library/bb207523.aspx . This article also shows how to check for errors after a SetProperties call.
  05-Jul-2008  00:43   
Sue,

Thanks for prompt response. Yes I'm using the same code posted above to set Calender folder's default form. The form already got published into Organizational forms library. OL 2003 version code you have posted works just fine. But this one fails right at the SetProperties() call. I have "." as part of my form name. May be that cause this behavior. Thoughts please?

FYI: I didn't change anything on the given code except to get correct form name (even if the form name has ".")

I plan to try SetProperty rather than SetProperties and the other option I have is renaming the form name without period (.) on it.

I refered the MSDN resource you have provided me but that didn't help much. Anyway I appreciate your help and the knowledge you share with community through outlookcode.com. Thanks!
 
  05-Jul-2008  09:48   
Selvam, a custom form name must have not one but two periods in it, because it is always something like "IPM.Contact.MyForm" -- what form name are you using?
Page [ 1 2 3 4 5 Next >>