我使用以下代码在Powerpoint演示文稿中创建了一个宏。当运行这一小段代码时,我得到一个例外“对可视化基础项目的编程访问不受信任”。我知道可以通过更改选项中的信任中心设置来解决此问题。但任何人都可以帮助我如何通过代码更改这些设置。任何powerpoint互操作API? pl建议..提前致谢。
我的示例代码:
PowerPoint.Application oPPT = new PowerPoint.Application();
oPPT.Visible = Office.MsoTriState.msoTrue;
//Add New Presentation
PowerPoint.Presentations oPresSet = oPPT.Presentations;
PowerPoint.Presentation oPres = oPresSet.Add(Office.MsoTriState.msoTrue);
//Add Slides to the Presentation
PowerPoint.Slides oSlides = oPres.Slides;
PowerPoint.Slide oSlide = oSlides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitle);
VBComponent vbc = oPres.VBProject.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule);
string code = "sub VBAMacro()\r\n" + "ActivePresentation.Close\n" + "End Sub";
vbc.CodeModule.AddFromString(code);
答案 0 :(得分:2)
最后,该设置只存储在注册表项中,因此您可以执行此类操作,但您需要运行具有访问该注册表项的权限。我做了一个基本的测试,它似乎在我的机器上工作。
using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Office\14.0\PowerPoint\Security",true))
{
int origVal = (int)key.GetValue("AccessVBOM", 0);
if (origVal != 1)
key.SetValue("AccessVBOM", 1);
PowerPoint.Application oPPT = new PowerPoint.Application();
oPPT.Visible = Office.MsoTriState.msoTrue;
//Add New Presentation
PowerPoint.Presentations oPresSet = oPPT.Presentations;
PowerPoint.Presentation oPres = oPresSet.Add(Office.MsoTriState.msoTrue);
//Add Slides to the Presentation
PowerPoint.Slides oSlides = oPres.Slides;
PowerPoint.Slide oSlide = oSlides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitle);
VBComponent vbc = oPres.VBProject.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule);
string code = "sub VBAMacro()\r\n" + "ActivePresentation.Close\n" + "End Sub";
vbc.CodeModule.AddFromString(code);
if (origVal != 1)
{
key.SetValue("AccessVBOM", origVal);
}
key.Close();
}