Aspose.Cellsを使用してVBAまたはマクロコードを修正する
Contents
[
Hide
]
Aspose.Cellsを使用してVBAまたはマクロコードを修正できます。Aspose.CellsはExcelファイル内のVBAプロジェクトを読み取り、修正するための次のクラスを追加しました。
- VbaProject
- VbaModuleCollection
- VbaModule
この記事では、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ファイルと出力Excelファイルをこちらのリンクからダウンロードできます。
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 | |
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"); |