Ändra VBA eller makrokod med Aspose.Cells
Contents
[
Hide
]
Du kan ändra VBA eller makrokod med Aspose.Cells. Aspose.Cells har lagt till följande klasser för att läsa och ändra VBA-projektet i Excel-filen.
- VbaProject
- VbaModuleCollection
- VbaModule
Den här artikeln visar dig hur du ändrar VBA- eller makrokoden i källfilen i Excel med Aspose.Cells.
Exempel
Följande exempelkod laddar källfilen i Excel som har en följande VBA- eller makrokod inuti
Sub Button1_Click()
MsgBox "This is test message."
End Sub
Efter exekvering av Aspose.Cells exempelkod kommer VBA- eller makrokoden att ändras så här
Sub Button1_Click()
MsgBox "This is Aspose.Cells message."
End Sub
Du kan ladda nerkäll Excel-fil och denutdata Excel-fil från de angivna länkarna.
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"); |