تعديل VBA أو Macro Code باستخدام Aspose.Cells
Contents
[
Hide
]
يمكنك تعديل VBA أو Macro Code باستخدام Aspose.Cells. قام Aspose.Cells بإضافة الفئات التالية لقراءة وتعديل مشروع VBA في ملف Excel.
- VbaProject
- مجموعة VbaModuleCollection
- VbaModule
ستوضح لك هذه المقالة كيفية تغيير VBA أو Macro Code داخل ملف Excel المصدر باستخدام Aspose.Cells.
مثال
يقوم نموذج التعليمات البرمجية التالي بتحميل ملف Excel المصدر الذي يحتوي على رمز VBA أو Macro التالي بداخله
Sub Button1_Click()
MsgBox "This is test message."
End Sub
بعد تنفيذ رمز عينة Aspose.Cells ، سيتم تعديل رمز VBA أو ماكرو بهذا الشكل
Sub Button1_Click()
MsgBox "This is Aspose.Cells message."
End Sub
يمكنك تنزيل ملفملف Excel المصدر و الإخراج ملف Excel من الروابط المعطاة.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getDataDir(ModifyVBAorMacroCode.class); | |
// Create workbook object from source Excel file | |
Workbook workbook = new Workbook(dataDir + "sample.xlsm"); | |
// Change the VBA Module Code | |
VbaModuleCollection modules = workbook.getVbaProject().getModules(); | |
for (int i = 0; i < modules.getCount(); i++) { | |
VbaModule module = modules.get(i); | |
String code = module.getCodes(); | |
// Replace the original message with the modified message | |
if (code.contains("This is test message.")) { | |
code = code.replace("This is test message.", "This is Aspose.Cells message."); | |
module.setCodes(code); | |
} | |
} | |
// Save the output Excel file | |
workbook.save(dataDir + "output.xlsm"); |