Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 686031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:58:58+00:00 2026-05-14T01:58:58+00:00

I want to embed a custom c# windows form (or WPF) user control into

  • 0

I want to embed a custom c# windows form (or WPF) user control into an outlook view.
I am using Outlook 2003 and Visual Studio 2008.

I did download an example for Outlook 2007 here:

http://blogs.msdn.com/e2eblog/archive/2008/01/09/outlook-folder-homepage-hosting-wpf-activex-and-windows-forms-controls.aspx

and also here:

http://msdn.microsoft.com/en-us/library/aa479345.aspx

I tested it and under 2007 it is working, but for 2003 i am getting the following error when i want to open the view:

Could not complete the operation due to error 80131509

I can start it from Visual Studio, it is registering the folder just fine, debugging works and all that. It creates an HTML page that contains my type as an object parameter – but the Initialize method that should be called is either not present (not shown via JS) or it has some errors.

The breakpoints for RegisterSafeForScripting are also never hit – maybe related to that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:58:58+00:00Added an answer on May 14, 2026 at 1:58 am

    First thank you AMissico – and i will look into the description and links you provided as well. The follwinng code did for me the trick (but the button controls and others have broken layout – but WFP works so i will use wpf). I have to refactor the code but it was the working version for me…

    The example i finally found that worked after tweaking it a bit for me:
    http://www.microsoft.com/downloads/details.aspx?familyid=078124E9-1E88-4F51-8C98-3C1999CFE743&displaylang=en

    The following file is more or less taken from the above example

    using System;
    using System.IO;
    using System.Collections.Generic;
    using MSOutlook = Microsoft.Office.Interop.Outlook;
    using System.Runtime.InteropServices;
    
    namespace Outlook2003KnowledgeBaseAddIn.Setup
    {
        public sealed class FolderHomePage
        {
            /// <summary>
            /// List of web view files that have been written out during this Outlook intance
            /// </summary>
            private static List<string> listWebViewFiles = new List<string>();
    
            /// <summary>
            /// Registers a specific managed type as a folder home page. Returns a file path for the folder home page
            /// </summary>
            /// <param name="viewType">Type of the home page control. 
            /// Control must be ComVisible should be registered safe for scripting</param>
            /// <returns>file path to the folder home page</returns>
            public static string RegisterType(Type viewType)
            {
                if (viewType == null)
                    return null;
    
                //TODO: ensure that viewType inherits from System.Windows.Forms.Control
    
                //Create the Local App Data directory for the Web view files to reside in
                string webViewDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Properties.Resources.WebViewDirectoryName);
                if (Directory.Exists(webViewDirectory) == false)
                    Directory.CreateDirectory(webViewDirectory);
    
                //Create the web view file name based on the viewType guid in the web view directory
                string webViewFile = Path.Combine(webViewDirectory, viewType.GUID.ToString("N") + ".htm");
    
                //if the file has been written out already in this session, return
                if (listWebViewFiles.Contains(webViewFile))
                    return webViewFile;
    
                //If the file exists, delete it (for versioning reasons)
                if (File.Exists(webViewFile))
                    File.Delete(webViewFile);
    
                //Open a file stream and text writer for the Web view stream
                FileStream stm = new FileStream(webViewFile, FileMode.Create, FileAccess.Write);
                TextWriter writer = new StreamWriter(stm, System.Text.Encoding.ASCII);
    
                //Look to see if the viewType has an init method that takes a single Outlook App parameter
                System.Reflection.MethodInfo initInfo = viewType.GetMethod("Initialize", new Type[] { typeof(MSOutlook.Application) });
    
                //If the viewType doesn't have an Init method, just write out the html page header
                //TODO move HTML code to resource strings
                if (initInfo == null)
                {
                    writer.WriteLine("<html><body rightmargin = '0' leftmargin ='0' topmargin ='0' bottommargin = '0'>");
                }
                //If the viewType does have an Init method, write script to trap the Body.OnLoad event and call the Init method
                //passing in the window.external.OutlookApplication object as the parameter
                else
                {
                    writer.WriteLine("<html><body rightmargin = '0' leftmargin ='0' topmargin ='0' bottommargin = '0' onload='OnBodyLoad()'>");
                    writer.WriteLine("<script>\n\tfunction OnBodyLoad()\n\t{\n\t\tvar oApp = window.external.OutlookApplication;");
                    writer.WriteLine("\t\t{0}.Initialize(oApp);", viewType.Name);
                    writer.WriteLine("\t}\n</script>");
                }
    
                //Write out an object tag that loads up the viewType as a com object via its class id
                writer.WriteLine("<object classid='clsid:{0}' ID='{1}' VIEWASTEXT width='100%' height='100%'/>", viewType.GUID, viewType.Name);
                writer.WriteLine("</body></html>");
    
                //Close the file
                writer.Close();
                stm.Close();
    
                //save this file name so we don't write it out multiple times per outlook session
                listWebViewFiles.Add(webViewFile);
    
                return webViewFile;
            }
    
            private const string CATID_SafeForScripting = "7DD95801-9882-11CF-9FA9-00AA006C42C4";
            private const string CATID_SafeForInitializing = "7DD95802-9882-11CF-9FA9-00AA006C42C4";
    
            /// <summary>
            /// Registers a managed type that's exposed for COM interop as safe for initializing and scripting
            /// </summary>
            /// <param name="comType"></param>
            public static void RegisterSafeForScripting(Type comType)
            {
                Guid clsid = comType.GUID;
                Guid interfaceSafeScripting = new Guid(CATID_SafeForScripting);
                Guid interfaceSafeForInitializing = new Guid(CATID_SafeForInitializing);
    
                ICatRegister reg = (ICatRegister)new ComComponentCategoriesManager();
                reg.RegisterClassImplCategories(ref clsid, 1, new Guid[] { interfaceSafeScripting });
                reg.RegisterClassImplCategories(ref clsid, 1, new Guid[] { interfaceSafeForInitializing });
            }
    
            /// <summary>
            /// Unregisters a managed type that's exposed for COM interop as safe for initializing and scripting
            /// </summary>
            /// <param name="comType"></param>
            public static void UnregisterSafeForScripting(Type comType)
            {
                Guid clsid = comType.GUID;
                Guid interfaceSafeScripting = new Guid(CATID_SafeForScripting);
                Guid interfaceSafeForInitializing = new Guid(CATID_SafeForInitializing);
    
                ICatRegister reg = (ICatRegister)new ComComponentCategoriesManager();
                reg.UnRegisterClassImplCategories(ref clsid, 1, new Guid[] { interfaceSafeScripting });
                reg.UnRegisterClassImplCategories(ref clsid, 1, new Guid[] { interfaceSafeForInitializing });
            }
    
        }
    
    
        [ComImport(), Guid("0002E005-0000-0000-C000-000000000046")]
        class ComComponentCategoriesManager
        {
        }
    
        [ComImport(), Guid("0002E012-0000-0000-C000-000000000046")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        interface ICatRegister
        {
            void RegisterCategories(int cCategories, IntPtr rgCategoryInfo);
    
            void UnRegisterCategories(int cCategories, IntPtr rgcatid);
    
            void RegisterClassImplCategories(
                    [In()] ref Guid rclsid,
                    int cCategories,
                    [In(), MarshalAs(UnmanagedType.LPArray)] Guid[] rgcatid);
    
            void UnRegisterClassImplCategories(
                    [In()] ref Guid rclsid,
                    int cCategories,
                    [In(), MarshalAs(UnmanagedType.LPArray)] Guid[] rgcatid);
    
            void RegisterClassReqCategories(
                [In()] ref Guid rclsid,
                int cCategories,
                [In(), MarshalAs(UnmanagedType.LPArray)] Guid[] rgcatid);
    
            void UnRegisterClassReqCategories(
                [In()] ref Guid rclsid,
                int cCategories,
                [In(), MarshalAs(UnmanagedType.LPArray)] Guid[] rgcatid);
        }
    
    }
    

    together with this file:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Security.Policy;
    using System.Security;
    using Outlook2003KnowledgeBaseAddIn.Setup;
    using Outlook2003KnowledgeBaseAddIn.UI;
    using Outlook2003KnowledgeBaseAddIn.UI.OutlookIntegration;
    
    namespace Outlook2003KnowledgeBaseAddIn
    {
        [System.ComponentModel.RunInstaller(true)]
        public class Installer : System.Configuration.Install.Installer
        {
            public Installer()
            {
            }
    
            public override void Install(System.Collections.IDictionary stateSaver)
            {
                base.Install(stateSaver);
    
                try
                {
                    ConfigureSecurityPolicy();
                    RegisterFolderHomePages();
                }
                catch (Exception ex)
                {
                    throw new System.Configuration.Install.InstallException("Custom Installer Failed", ex);
                }
            }
    
            public override void Uninstall(System.Collections.IDictionary savedState)
            {
                base.Uninstall(savedState);
    
                try
                {
                    DeleteSecurityPolicy();
                    UnregisterFolderHomePages();
                }
                catch (Exception ex)
                {
                    throw new System.Configuration.Install.InstallException("Custom Installer Failed", ex);
                }
            }
    
            private void RegisterFolderHomePages()
            {
                //Utility.FolderHomePage.RegisterSafeForScripting(typeof(FolderHomePages.AccountToday));
                FolderHomePage.RegisterSafeForScripting(typeof(WebViewControl));
            }
    
            private void UnregisterFolderHomePages()
            {
                //Utility.FolderHomePage.UnregisterSafeForScripting(typeof(FolderHomePages.AccountToday));
                FolderHomePage.UnregisterSafeForScripting(typeof(WebViewControl));
            }
    
            private void ConfigureSecurityPolicy()
            {
                // Find the machine policy level
                PolicyLevel machinePolicyLevel = GetMachinePolicyLevel();
    
                // Get the install directory of the current installer
                string assemblyPath = this.Context.Parameters["assemblypath"];
                string installDirectory =
                    assemblyPath.Substring(0, assemblyPath.LastIndexOf("\\"));
    
                if (!installDirectory.EndsWith(@"\"))
                    installDirectory += @"\";
    
                installDirectory += "*";
    
                // Create the code group
                CodeGroup codeGroup = new UnionCodeGroup(
                    new UrlMembershipCondition(installDirectory),
                    new PolicyStatement(new NamedPermissionSet("FullTrust")));
                codeGroup.Description = Properties.Resources.CasPolicyDescription;
                codeGroup.Name = Properties.Resources.CasPolicyName;
    
                // Add the code group
                machinePolicyLevel.RootCodeGroup.AddChild(codeGroup);
    
                // Save changes
                SecurityManager.SavePolicy();
            }
    
            private static void DeleteSecurityPolicy()
            {
                PolicyLevel machinePolicy = GetMachinePolicyLevel();
    
                foreach (CodeGroup codeGroup in machinePolicy.RootCodeGroup.Children)
                {
                    if (codeGroup.Name == Properties.Resources.CasPolicyName)
                        machinePolicy.RootCodeGroup.RemoveChild(codeGroup);
                }
    
                SecurityManager.SavePolicy();
            }
    
            private static PolicyLevel GetMachinePolicyLevel()
            {
                System.Collections.IEnumerator policyHierarchy = SecurityManager.PolicyHierarchy();
    
                while (policyHierarchy.MoveNext())
                {
                    PolicyLevel level = (PolicyLevel)policyHierarchy.Current;
                    if (level.Type == PolicyLevelType.Machine)
                        return level;
                }
    
                throw new ApplicationException("Could not find Machine Policy level. Code Access Security is not configured for this application.");
            }
        }
    }
    

    This method sets up the web view:

    private void CreateWebViewFolders()
    {
        MSOutlook.MAPIFolder root = Folder.GetRootFolder(Application.Session);
    
        MSOutlook.MAPIFolder webViewFolder = Folder.CreateFolder(root, Properties.Resources.WebViewFolderName);
        webViewFolder.WebViewURL = FolderHomePage.RegisterType(typeof(WebViewControl));
        webViewFolder.WebViewOn = true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to embed the native camera application into custom form. The RECT r
Is there a way to embed HTML into a windows forms custom usercontrol? I
I've been able to embed webkit control into a windows (c++) application, and registered
I want to Embed SVG into XUL. I tried to use the this tutorial
I want to embed a Flex application (i.e. a Flash SWF) into a PDF
I want to embed some youtube videos at my site using Video.Flash helper. Microsoft.Web.Helpers
I am using strict doctype and i want to embed a page within a
In iOS5 using storyboard feature I want to create a custom container which will
I am trying to embed a flash video into a custom setup of the
I have a custom list which I want to embed as a resource so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.