Assegnare Codice Macro al Controllo Modulo

Assegnare Codice Macro al Controllo Modulo usando Aspose.Cells

Il codice di esempio seguente crea un nuovo workbook, assegna un Codice Macro a un Pulsante di Form e salva l’output nel formato XLSM. Una volta aperto il file XLSM di output in Microsoft Excel vedrai il seguente codice macro.

 Sub ShowMessage()

    MsgBox "Welcome to Aspose!"

End Sub

Ecco un codice di esempio per generare un file XLSM di output con Codice Macro.

// 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(AssignMacroToFormControl.class);
Workbook workbook = new Workbook();
Worksheet sheet = workbook.getWorksheets().get(0);
int moduleIdx = workbook.getVbaProject().getModules().add(sheet);
VbaModule module = workbook.getVbaProject().getModules().get(moduleIdx);
module.setCodes("Sub ShowMessage()" + "\r\n" +
" MsgBox \"Welcome to Aspose!\"" + "\r\n" +
"End Sub");
Button button = (Button) sheet.getShapes().addShape(MsoDrawingType.BUTTON, 2, 0, 2, 0, 28, 80);
button.setPlacement(PlacementType.FREE_FLOATING);
button.getFont().setName("Tahoma");
button.getFont().setBold(true);
button.getFont().setColor(Color.getBlue());
button.setText("Aspose");
button.setMacroName(sheet.getName() + ".ShowMessage");
workbook.save(dataDir + "Output.xlsm");
System.out.println("File saved");