Publishing Custom Forms in Outlook RRS feed

  • Question

  • Good day!

    I want to publish Custom Forms to the Personal Folder of the outlook based in .oft files. The VB code its very easy, the hardest thing its to pass it to C# because the outlook object library is very tight.

    This is the VB original Code:

    'Add Project  Reference: Microsoft Outlook 11.0 Object Library (mstoul.olb)

    Sub PublishFromFile()
        Dim filename As String
        Dim Temp As String
        Dim endpos As Integer
        Dim mylength As Integer
        Set myOlApp = GetObject("", "Outlook.Application")
        filename = Dir$("C:\OutlookForms\*.oft", vbNormal)
        Do Until filename = ""
            mylength = Len(filename)
            endpos = InStr(1, filename, ".oft", 1)
            strTemp = Trim(Mid(filename, 1, endpos - 1))
            Set myItem = myOlApp.CreateItemFromTemplate("C:\OutlookForms\" & filename)
            Set myForm = myItem.FormDescription
            myForm.Name = strTemp
            myForm.PublishForm olPersonalRegistry
            filename = Dir$
    End Sub

    This is my C# code:

                while (filename!="")
                    mylenght = filename.Length;
                    endpos = filename.IndexOf(".oft",1,1);
                    strTemp= filename.Substring(1,endpos -1);

                    aux=path2 + filename;               
                    Folders folder ;
                    //Here is the hard stuff
                    //Create item from template file (.oft)
                    Items  myItem = (Items)outLookAPP.CreateItemFromTemplate(aux,folder);
                    //Create a form to publish
                    FormDescription myForm =(FormDescription)myItem.FormDescription;                             //Assign the name
                    //Publish de Form
                    myForm.PublishForm(Outlook.OlFormRegistry.olPersonalRegistry,folder );

    I dont have compile errors, just a runtime error that sayts : cannot cast object , exactly in this line:

    Items  myItem = (Items)outLookAPP.CreateItemFromTemplate(aux,folder);

    Has somebody done something like this before.

    I have tryed different code to publish Contacts, Tasks, etc in Outlook, the problem comes when publishing Custom Forms, I don't know what object to use. I only have Mail, Item, Contact, Task, etc option, but not a Form object.

    Thanks in advance.!
    Tuesday, July 19, 2005 9:13 PM


  • Try casting it as a MailItem and see if that helps.

    Wednesday, July 20, 2005 3:01 PM