使用 Python 管理簡報中的 VBA 專案

簡介

Aspose.Slides 提供用於操作巨集與 VBA 程式碼的類別和介面。

新增 VBA 巨集

Aspose.Slides 提供 VbaProject 類別,使您能建立 VBA 專案(及其參考)並編輯現有模組。您可以使用 VbaProject 類別來管理嵌入於簡報中的 VBA。

  1. 建立 Presentation 類別的實例。
  2. 使用 VbaProject 建構函式新增 VBA 專案。
  3. 將模組新增至 VBA 專案。
  4. 設定模組的原始碼。
  5. 加入對 stdole 的參考。
  6. 加入對 Microsoft Office 的參考。
  7. 將參考與 VBA 專案關聯。
  8. 儲存簡報。
import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpime.startJVM()

from asposeslides.api import Presentation, SaveFormat, VbaProject, VbaReferenceOleTypeLib

presentation = Presentation()
try:
    # 建立新的 VBA 專案。
    vba_project = VbaProject()
    presentation.setVbaProject(vba_project)

    # 新增空白模組並設定其來源程式碼。
    module = vba_project.getModules().addEmptyModule("Module")
    module.setSourceCode('Sub Test(oShape As Shape)\n    MsgBox "Test"\nEnd Sub')

    # 建立對 stdole 與 Microsoft Office 的參考。
    stdole_reference = VbaReferenceOleTypeLib("stdole", r"*\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\Windows\system32\stdole2.tlb#OLE Automation")
    office_reference = VbaReferenceOleTypeLib("Office", r"*\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")

    # 將參考加入 VBA 專案。
    vba_project.getReferences().add(stdole_reference)
    vba_project.getReferences().add(office_reference)

    # 儲存簡報。
    presentation.save("test.pptm", SaveFormat.Pptm)
finally:
    presentation.dispose()

移除 VBA 巨集

使用 Presentation 類別的 getVbaProject 方法,即可移除 VBA 巨集。

  1. 建立 Presentation 類別的實例並載入包含巨集的簡報。
  2. 存取巨集模組並將其移除。
  3. 儲存已修改的簡報。
import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, SaveFormat

# 載入包含巨集的簡報。
presentation = Presentation("VBA.pptm")
try:
    # 取得 VBA 模組並將其移除。
    vba_project = presentation.getVbaProject()
    if vba_project is not None and len(list(vba_project.getModules())) > 0:
        module = vba_project.getModules().get_Item(0)
        vba_project.getModules().remove(module)

    # 儲存簡報。
    presentation.save("test.pptm", SaveFormat.Pptm)
finally:
    presentation.dispose()

擷取 VBA 巨集

  1. 建立 Presentation 類別的實例並載入包含巨集的簡報。
  2. 檢查簡報是否包含 VBA 專案。
  3. 遍歷 VBA 專案中所有模組以檢視巨集。
import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation

# 載入包含巨集的簡報。
presentation = Presentation("VBA.pptm")
try:
    # 檢查簡報是否包含 VBA 專案。
    vba_project = presentation.getVbaProject()
    if vba_project is not None:
        for module in vba_project.getModules():
            print(module.getName())
            print(module.getSourceCode())
finally:
    presentation.dispose()

檢查 VBA 專案是否受密碼保護

使用 VbaProject.isPasswordProtected 方法,可判斷專案的屬性是否受密碼保護。

  1. 建立 Presentation 類別的實例並載入包含巨集的簡報。
  2. 檢查簡報是否包含 VBA project
  3. 檢查 VBA 專案是否受密碼保護以檢視其屬性。
import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation

presentation = Presentation("VBA.pptm")
try:
    # 檢查簡報是否包含 VBA 專案。
    vba_project = presentation.getVbaProject()
    if vba_project is not None:
        if vba_project.isPasswordProtected():
            print(f"The VBA project '{vba_project.getName()}' is password-protected for viewing its properties.")
finally:
    presentation.dispose()

常見問題

如果我將簡報另存為 PPTX,巨集會發生什麼情況?

巨集將被移除,因為 PPTX 不支援 VBA。若要保留巨集,請選擇 PPTM、PPSM 或 POTM。

Aspose.Slides 能在簡報內執行巨集,例如刷新資料嗎?

不能。此函式庫永不執行 VBA 程式碼;執行僅能在具備適當安全設定的 PowerPoint 中完成。

是否支援操作與 VBA 程式碼連結的 ActiveX 控制項?

是,您可以存取現有的 ActiveX controls,修改其屬性,並將其移除。這在巨集與 ActiveX 互動時非常有用。