Modifica VBA o codice macro utilizzando Aspose.Cells
Contents
[
Hide
]
È possibile modificare VBA o Macro Code utilizzando Aspose.Cells. Aspose.Cells ha aggiunto le seguenti classi per leggere e modificare il progetto VBA nel file Excel.
- VbaProject
- VbaModuleCollection
- Modulo Vba
Questo articolo ti mostrerà come modificare il VBA o il codice macro all’interno del file Excel di origine utilizzando Aspose.Cells.
Esempio
Il seguente codice di esempio carica il file Excel di origine che contiene un codice VBA o macro seguente
Sub Button1_Click()
MsgBox "This is test message."
End Sub
Dopo l’esecuzione del codice di esempio Aspose.Cells, il codice VBA o Macro verrà modificato in questo modo
Sub Button1_Click()
MsgBox "This is Aspose.Cells message."
End Sub
Puoi scaricare ilfile Excel di origine e ilfile Excel di output dai link indicati.
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"); |