Aspose.Cells を使用して VBA またはマクロ コードを変更する
Contents
[
Hide
]
Aspose.Cells を使用して VBA またはマクロ コードを変更できます。
- VbaProject
- VbaModuleCollection
- Vbaモジュール
この記事では、Aspose.Cells を使用して、ソース Excel ファイル内の VBA またはマクロ コードを変更する方法について説明します。
例
次のサンプル コードは、次の VBA またはマクロ コードを含むソース Excel ファイルを読み込みます。
Sub Button1_Click()
MsgBox "This is test message."
End Sub
Aspose.Cells サンプルコードを実行すると、VBA またはマクロコードは次のように変更されます。
Sub Button1_Click()
MsgBox "This is Aspose.Cells message."
End Sub
ダウンロードできますソースの Excel ファイルそしてその出力エクセルファイル指定されたリンクから。
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-Java | |
String dataDir = Utils.getDataDir(ModifyVBAorMacroCode.class); | |
// Create workbook object from source Excel file | |
Workbook workbook = new Workbook(dataDir + "sample.xlsm"); | |
// Change the VBA Module Code | |
VbaModuleCollection modules = workbook.getVbaProject().getModules(); | |
for (int i = 0; i < modules.getCount(); i++) { | |
VbaModule module = modules.get(i); | |
String code = module.getCodes(); | |
// Replace the original message with the modified message | |
if (code.contains("This is test message.")) { | |
code = code.replace("This is test message.", "This is Aspose.Cells message."); | |
module.setCodes(code); | |
} | |
} | |
// Save the output Excel file | |
workbook.save(dataDir + "output.xlsm"); |