フォーム コントロールへのマクロの割り当て
Contents
[
Hide
]
Aspose.Cells を使用すると、ボタンのようなフォーム コントロールにマクロ コードを割り当てることができます。をご利用くださいShape.MarcoNameプロパティを使用して、ブック内のフォーム コントロールに新しいマクロ コードを割り当てます。
次のサンプル コードは、新しいワークブックを作成し、マクロ コードをフォーム ボタンに割り当て、出力を XLSM 形式で保存します。出力 XLSM ファイルを Microsoft Excel で開くと、次のマクロ コードが表示されます。
Sub ShowMessage()
MsgBox "Welcome to Aspose!"
End Sub
C# でフォーム コントロールにマクロを割り当てます。
マクロ コードを使用して出力 XLSM ファイルを生成するサンプル コードを次に示します。
This file contains hidden or 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-.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); |