【问题标题】:Adding a VBA macro to powerpoint将 VBA 宏添加到 powerpoint
【发布时间】:2019-06-11 06:43:00
【问题描述】:

我正在尝试使用 interop.powerpoint 以编程方式在 powerpoint 中创建一个新宏

我尝试过使用 Presentation 对象,但 VB Project 属性只包含 get 属性。

PowerPoint.Application oPP = new PowerPoint.Application()
        {
            DisplayAlerts = Microsoft.Office.Interop.PowerPoint.PpAlertLevel.ppAlertsNone,
            Visible = MsoTriState.msoTrue
        };

        PowerPoint.Presentations oPresSet = oPP.Presentations;
        PowerPoint._Presentation _activePres = oPresSet.Open(@"‪C:\Users\Mohit\Desktop\Archive\Working_Session_S&OP.pptx",
        MsoTriState.msoTrue, MsoTriState.msoFalse,
        MsoTriState.msoTrue);

我想从 C# 发送 VBA 宏代码

【问题讨论】:

    标签: c# interop powerpoint


    【解决方案1】:

    您可以使用 Microsoft.Vbe.Interop 参考和下一个代码从互操作项目创建宏(在这种情况下,在新打开的演示文稿上添加宏):

    Globals.ThisAddIn.Application.PresentationOpen += pres =>
            {
                var presentationVb = pres.VBProject;
                var vbComponent = presentationVb.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule);
                var vbComponentCodeModule = vbComponent.CodeModule;
    
                var lineNum = vbComponentCodeModule.CountOfLines + 1;
                var codeText = "Public Sub Test(link as String)" + "\r\n";
                //any VBA code
                codeText += "End Sub";
    
                vbComponentCodeModule.InsertLines(lineNum, codeText);
                pres.Save();
            };
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-23
      • 2015-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多