给窗体控件分配宏

以下示例代码创建一个新的工作簿,为一个表单按钮分配宏代码,并将输出保存为XLSM格式。一旦你在Microsoft Excel中打开输出的XLSM文件,你将看到以下宏代码。

 Sub ShowMessage()

    MsgBox "Welcome to Aspose!"

End Sub

在C#中为表单控件分配宏

以下是生成输出XLSM文件的示例代码

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
if (!System.IO.Directory.Exists(dataDir))
{
System.IO.Directory.CreateDirectory(dataDir);
}
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
Aspose.Cells.Worksheet sheet = workbook.Worksheets[0];
int moduleIdx = workbook.VbaProject.Modules.Add(sheet);
Aspose.Cells.Vba.VbaModule module = workbook.VbaProject.Modules[moduleIdx];
module.Codes =
"Sub ShowMessage()" + "\r\n" +
" MsgBox \"Welcome to Aspose!\"" + "\r\n" +
"End Sub";
Aspose.Cells.Drawing.Button button = sheet.Shapes.AddButton(2, 0, 2, 0, 28, 80);
button.Placement = Aspose.Cells.Drawing.PlacementType.FreeFloating;
button.Font.Name = "Tahoma";
button.Font.IsBold = true;
button.Font.Color = System.Drawing.Color.Blue;
button.Text = "Aspose";
button.MacroName = sheet.Name + ".ShowMessage";
dataDir = dataDir + "Output.out.xlsm";
workbook.Save(dataDir);