将VBA宏添加到Powerpoint

时间:2019-06-11 06:43:00

标签: c# interop powerpoint

我正在尝试使用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宏代码

1 个答案:

答案 0 :(得分:0)

您可以使用Microsoft.Vbe.Interop参考和下一个代码(在这种情况下,在新打开的演示文稿上添加宏)从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();
        };
    }