Outlook2003视控制与C#
-
20-09-2019 - |
题
我想嵌入一个定义c#窗形式(或WPF)用户控制进入一个前景图。我使用Outlook2003年和Visual Studio2008年。
我没有下载的一个例子Outlook2007年在这里:
并且还在这里:
http://msdn.microsoft.com/en-us/library/aa479345.aspx
我进行了测试,并在2007年的工作,但是2003年我得到的以下错误的时候我想要打开来看:
不能操作的完成由于错误80131509
我可以开始它从Visual Studio,它是注册文件夹只是现、调试工作和所有。它创建了一个HTML网页,其中包含我的类型作为对象的参数,但初始化方法,这应被称为是或者不存在(没有显示通过JS)或它有一些错误。
的断点RegisterSafeForScripting也永远不会打的-也许有关。
解决方案 2
首先谢谢AMissico - 我会考虑的描述和链接你来提供。该follwinng代码为我做的伎俩(但控制按钮和其他人打破布局 - 但世界粮食计划署的工作,所以我将使用WPF)。我不得不重构代码,但它是工作版本,我...
在实施例I终于发现,调整它有点为我后工作: http://www.microsoft。 COM /下载/ details.aspx?FAMILYID = 078124E9-1E88-4F51-8C98-3C1999CFE743&displaylang = EN
下面的文件被或多或少从上面的例子中取
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);
}
}
与此文件一起:
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.");
}
}
}
此方法设置web视图:
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;
}
其他提示
听起来你需要创建一个 运行的安全政策 对于大会。 Visual Studio/应用程序级 设定这种自动的。你可以检查这个运行项目,从Visual Studio然后开Microsoft。净框架2.0配置下的应用程序管理工具。浏览以运行的安全政策和检查的码组。应用程序级项目正在用户>代码组>All_Code>VSTOProjects.
以下是我的笔记。
代码的访问权的安全政策(caspol)的课程
- 从创建的部署解决方案办公室使用安装Windows Version3.
- 改名为SetSecurity类CaspolSecurityPolicyInstaller.
代码的访问权的安全政策工具(Caspol.exe)
代码的访问的安全政策的工具,使用户和管理人员修改安全政策的机的政策级别,用户策一级和企业政策的水平。
部署
部署Visual Studio2005年的工具,为办公室系统本身的解决方案使用的Windows安装(第1部分的2)
- 文章: http://msdn.microsoft.com/en-us/library/bb332051.aspx
- 下载: http://go.microsoft.com/fwlink/?LinkId=83721
- Visual Studio2005年的工具,为办公室第二版样本:部署办公室的解决方案使用安装Windows Version3(OfficeVSTO2005SEWindowsInstallerv3.msi)