繁体   English   中英

在Excel文件中添加宏

[英]Add macro in excel file

我正在尝试使用c#.net代码生成Excel时添加宏。 生成Excel时出现此错误

不信任以编程方式访问Visual Basic Project

我知道我必须使宏在excel选项中受信任。 如何在c#.code中激活它?

这是我使用的代码。

StringBuilder sb;
VBIDE.VBComponent xlModule; 
sb = new StringBuilder();

xlApp.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityByUI;
 sb.AppendLine("Private Sub Worksheet_Change(ByVal Target As Range)");
sb.AppendLine("Target.Offset(0, 2).ClearContents");
xlModule = wrkBook.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule); 
xlModule.CodeModule.AddFromString(sb.ToString());

可以通过注册表进行更改:

Microsoft.Win32.Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Office\" 
                                   + xlApp.Version + "\Excel\Security", "AccessVBOM", 1);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM