Aggiunta di modulo e codice VBA utilizzando Aspose.Cells
Contents
[
Hide
]
Aspose.Cells consente di aggiungere un nuovo modulo VBA e codice macro utilizzando Aspose.Cells. Utilizzare ilCartella di lavoro.getVbaProject().getModules().add() metodo per aggiungere il nuovo modulo VBA all’interno della cartella di lavoro
Aggiunta di modulo e codice VBA utilizzando Aspose.Cells
Il seguente codice di esempio crea una nuova cartella di lavoro e aggiunge un nuovo modulo VBA e codice macro e salva l’output nel formato XLSM. Una volta, aprirai il file di output XLSM in Microsoft Excel e fai clic sulSviluppatore > Visual Basic comandi di menu, vedrai un modulo chiamato “TestModule” e al suo interno vedrai il seguente codice macro.
Sub ShowMessage()
MsgBox "Welcome to Aspose!"
End Sub
Codice d’esempio
Ecco un codice di esempio per generare il file di output XLSM con il modulo VBA e il codice macro.
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 | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(AddVBAModuleAndCode.class); | |
// Create new workbook | |
Workbook workbook = new Workbook(); | |
// Access first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Add VBA Module | |
int idx = workbook.getVbaProject().getModules().add(worksheet); | |
// Access the VBA Module, set its name and codes | |
VbaModule module = workbook.getVbaProject().getModules().get(idx); | |
module.setName("TestModule"); | |
module.setCodes("Sub ShowMessage()" + "\r\n" + " MsgBox \"Welcome to Aspose!\"" + "\r\n" + "End Sub"); | |
// Save the workbook | |
workbook.save(dataDir + "output.xlsm", SaveFormat.XLSM); | |