Bekerja dengan Makro VBA

Visual Basic for Applications (VBA) untuk Microsoft Word adalah bahasa pemrograman sederhana namun kuat yang dapat digunakan untuk memperluas fungsionalitas. Aspose.Words API menyediakan tiga kelas untuk mendapatkan akses ke kode sumber proyek VBA:

  • Kelas VBAProject menyediakan akses ke informasi proyek VBA
  • Kelas VBAModuleCollection mengembalikan koleksi modul proyek VBA
  • Kelas VbaModule menyediakan akses ke modul proyek VBA
  • Pencacahan VbaModuleType mendefinisikan tipe model dalam proyek VBA. Modul tersebut dapat berupa modul prosedural, modul dokumen, modul kelas, atau modul desainer

Buat Proyek VBA

Aspose.Words API menyediakan properti vba_project untuk mendapatkan atau mengatur VbaProject dalam dokumen.

Contoh kode berikut menunjukkan cara membuat proyek VBA dan Modul VBA beserta properti dasar misalnya name dan type:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
project = aw.vba.VbaProject()
project.name = "AsposeProject"
doc.vba_project = project
# Create a new module and specify a macro source code.
module = aw.vba.VbaModule()
module.name = "AsposeModule"
module.type = aw.vba.VbaModuleType.PROCEDURAL_MODULE
module.source_code = "New source code"
# Add module to the VBA project.
doc.vba_project.modules.add(module)
doc.save(docs_base.artifacts_dir + "WorkingWithVba.create_vba_project.docm")

Baca Makro

Aspose.Words juga memberi pengguna kemampuan untuk membaca makro VBA.

Contoh kode berikut menunjukkan cara membaca Makro VBA dari dokumen:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "VBA project.docm")
if (doc.vba_project != None) :
for module in doc.vba_project.modules :
print(module.source_code)

Tulis atau Ubah Makro

Dengan menggunakan Aspose.Words, pengguna dapat memodifikasi makro VBA.

Contoh kode berikut menunjukkan cara memodifikasi Makro VBA menggunakan properti source_code:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "VBA project.docm")
project = doc.vba_project
newSourceCode = "Test change source code"
project.modules[0].source_code = newSourceCode
doc.save(docs_base.artifacts_dir + "WorkingWithVba.modify_vba_macros.docm")

Proyek Klon VBA

Dengan Aspose.Words juga dimungkinkan untuk mengkloning proyek VBA.

Contoh kode berikut menunjukkan cara mengkloning Proyek VBA menggunakan properti clone yang membuat salinan proyek yang sudah ada:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "VBA project.docm")
destDoc = aw.Document()
destDoc.vba_project = doc.vba_project.clone()
destDoc.save(docs_base.artifacts_dir + "WorkingWithVba.clone_vba_project.docm")

Modul Klon VBA

Anda juga dapat mengkloning modul VBA jika diperlukan.

Contoh kode berikut menunjukkan cara mengkloning Modul VBA menggunakan properti clone yang membuat salinan proyek yang sudah ada:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "VBA project.docm")
destDoc = aw.Document()
destDoc.vba_project = aw.vba.VbaProject()
copyModule = doc.vba_project.modules.get_by_name("Module1").clone()
destDoc.vba_project.modules.add(copyModule)
destDoc.save(docs_base.artifacts_dir + "WorkingWithVba.clone_vba_module.docm")

Bekerja dengan Referensi Proyek VBA

Aspose.Words API menyediakan kelas VbaReferenceCollection untuk bekerja dengan Referensi Proyek VBA yang mewakili kumpulan referensi proyek VBA.

Contoh kode berikut menunjukkan cara menghapus beberapa referensi dari kumpulan referensi dari proyek VBA:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "VBA project.docm")
# Find and remove the reference with some LibId path.
brokenPath = "brokenPath.dll"
references = doc.vba_project.references
for i in range(references.count - 1, 0) :
reference = doc.vba_project.references.element_at(i)
path = get_lib_id_path(reference)
if (path == brokenPath) :
references.remove_at(i)
doc.save(docs_base.artifacts_dir + "WorkingWithVba.remove_broken_ref.docm")
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
# <summary>
# Returns string representing LibId path of a specified reference.
# </summary>
def get_lib_id_path(self, reference) :
if reference.type == aw.vba.VbaReferenceType.REGISTERED or reference.type == aw.vba.VbaReferenceType.ORIGINAL or reference.type == aw.vba.VbaReferenceType.CONTROL :
return self.get_lib_id_reference_path(reference.lib_id)
elif reference.type == aw.vba.VbaReferenceType.PROJECT :
return self.get_lib_id_project_path(reference.lib_id)
else :
raise RuntimeError()
# <summary>
# Returns path from a specified identifier of an Automation type library.
# </summary>
# <remarks>
# Please see details for the syntax at [MS-OVBA], 2.1.1.8 LibidReference.
# </remarks>
@staticmethod
def get_lib_id_reference_path(libIdReference : str) :
if (libIdReference != None) :
refParts = libIdReference.split('#')
if (refParts.length > 3) :
return refParts[3]
return ""
# <summary>
# Returns path from a specified identifier of an Automation type library.
# </summary>
# <remarks>
# Please see details for the syntax at [MS-OVBA], 2.1.1.12 ProjectReference.
# </remarks>
@staticmethod
def get_lib_id_project_path(libIdProject : str) :
if (libIdProject != None) :
return libIdProject.substring(3)
return ""