تعيين رمز ماكرو للتحكم في النموذج
Contents
[
Hide
]
Aspose.Cells يسمح لك بتخصيص كود ماكرو لعنصر تحكم نموذج مثل الزر. الرجاء استخدامShapeCollection.addShape () طريقة لتعيين رمز ماكرو جديد لعنصر تحكم النموذج داخل المصنف.
تعيين رمز ماكرو للتحكم في النموذج باستخدام Aspose.Cells
يقوم نموذج التعليمات البرمجية التالي بإنشاء مصنف جديد ، وتعيين “رمز ماكرو” إلى “نموذج زر” وحفظ الإخراج بتنسيق XLSM. بمجرد فتح ملف الإخراج XLSM في Microsoft Excel ، سترى رمز الماكرو التالي.
Sub ShowMessage()
MsgBox "Welcome to Aspose!"
End Sub
فيما يلي نموذج التعليمات البرمجية لإنشاء ملف الإخراج XLSM برمز الماكرو.
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(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"); |