翻譯|使用教程|編輯:李顯亮|2021-04-02 09:20:29.150|閱讀 331 次
概述:VBA宏用于自動執行PowerPoint演示文稿中的不同功能。在本文中,將學習如何以編程方式使用PowerPoint VBA宏。特別是,本文將介紹如何使用C#在PowerPoint演示文稿中添加,提取或刪除VBA宏。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
VBA宏用于自動執行PowerPoint演示文稿中的不同功能。例如,可以使用VBA執行重復性任務,生成圖表和表格等。在本文中,將學習如何以編程方式使用PowerPoint VBA宏。特別是,本文將介紹如何使用C#在PowerPoint演示文稿中添加,提取或刪除VBA宏。
Aspose.Slides for .NET是功能豐富的API,可讓您使用C#創建,編輯和轉換PowerPoint演示文稿。此外,該API允許您無縫使用VBA宏。
>>你可以點擊這里下載Aspose.Slides 最新版測試體驗。
以下是使用C#在PowerPoint演示文稿中添加VBA宏的步驟。
下面的代碼示例演示如何使用C#在PowerPoint演示文稿中添加VBA宏。
// Load presentation using (Presentation presentation = new Presentation("presentation.pptm")) { // Create new VBA Project presentation.VbaProject = new VbaProject(); // Add empty module to the VBA project IVbaModule module = presentation.VbaProject.Modules.AddEmptyModule("Module"); // Set module's source code module.SourceCode = @"Sub Test(oShape As Shape) MsgBox ""Test"" End Sub"; // Create reference toVbaReferenceOleTypeLib stdoleReference = new VbaReferenceOleTypeLib("stdole", "*\\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\\Windows\\system32\\stdole2.tlb#OLE Automation"); // Create reference to Office VbaReferenceOleTypeLib officeReference = new VbaReferenceOleTypeLib("Office", "*\\G{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}#2.0#0#C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE14\\MSO.DLL#Microsoft Office 14.0 Object Library"); // Add references to the VBA project presentation.VbaProject.References.Add(stdoleReference); presentation.VbaProject.References.Add(officeReference); // Save Presentation presentation.Save("AddVBAMacros.pptm", SaveFormat.Pptm); }
Aspose.Slides for .NET還允許您從PowerPoint演示文稿中的VBA項目中提取VBA模塊。以下是執行此操作的步驟。
下面的代碼示例演示如何使用C#提取PowerPoint VBA宏。
// Load presentation using (Presentation pres = new Presentation("presentation.pptm")) { if (pres.VbaProject != null) // check if Presentation contains VBA Project { foreach (IVbaModule module in pres.VbaProject.Modules) { Console.WriteLine(module.Name); Console.WriteLine(module.SourceCode); } } }
還可以從PowerPoint演示文稿中刪除特定的VBA宏。為此,您將通過VBA項目中的索引訪問和刪除VBA模塊。以下是執行此操作的步驟。
下面的代碼示例演示如何刪除PowerPoint VBA宏。
// Load presentation using (Presentation presentation = new Presentation("Presentation.pptm")) { // Remove the VBA module presentation.VbaProject.Modules.Remove(presentation.VbaProject.Modules[0]); // Save presentation presentation.Save("RemovedVBAMacros.pptm", SaveFormat.Pptm); }
如果你想試用Aspose的全部完整功能,可 聯系在線客服獲取30天臨時授權體驗。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn