VBA マクロ UserForm DesignerStorage をテンプレートからターゲット ワークブックにコピーする
Contents
[
Hide
]
考えられる使用シナリオ
Aspose.Cells を使用すると、VBA プロジェクトを 1 つの Excel ファイルから別の Excel ファイルにコピーできます。 VBA プロジェクトは、Document、Procedural、Designer などのさまざまなタイプのモジュールで構成されています。すべてのモジュールは単純なコードでコピーできますが、Designer モジュールの場合、アクセスまたはコピーする必要がある Designer Storage と呼ばれる追加データがいくつかあります。次の 2 つの方法は、デザイナー ストレージを処理します。
VBA マクロ UserForm DesignerStorage をテンプレートからターゲット ワークブックにコピーする
以下のサンプルコードをご覧ください。から VBA プロジェクトをコピーします。テンプレートエクセルファイル空のワークブックに保存し、出力エクセルファイル.テンプレートの Excel ファイル内で VBA プロジェクトを開くと、次のようなユーザー フォームが表示されます。ユーザー フォームは Designer Storage で構成されているため、次を使用してコピーされます。VbaModuleCollection.GetDesignerStorage()とVbaModuleCollection.AddDesignerStorage()メソッド。
次のスクリーンショットは、テンプレート Excel ファイルからコピーされた出力 Excel ファイルとその内容を示しています。ボタン 1 をクリックすると、クリックするとメッセージ ボックスを表示するコマンド ボタンを持つ VBA ユーザー フォームが開きます。
サンプルコード
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 | |
//Create empty target workbook | |
Workbook target = new Workbook(); | |
//Load the Excel file containing VBA-Macro Designer User Form | |
Workbook templateFile = new Workbook(sourceDir + "sampleDesignerForm.xlsm"); | |
//Copy all template worksheets to target workboook | |
foreach (Worksheet ws in templateFile.Worksheets) | |
{ | |
if (ws.Type == SheetType.Worksheet) | |
{ | |
Worksheet s = target.Worksheets.Add(ws.Name); | |
s.Copy(ws); | |
//Put message in cell A2 of the target worksheet | |
s.Cells["A2"].PutValue("VBA Macro and User Form copied from template to target."); | |
} | |
}//foreach | |
//----------------------------------------------- | |
//Copy the VBA-Macro Designer UserForm from Template to Target | |
foreach (VbaModule vbaItem in templateFile.VbaProject.Modules) | |
{ | |
if (vbaItem.Name == "ThisWorkbook") | |
{ | |
//Copy ThisWorkbook module code | |
target.VbaProject.Modules["ThisWorkbook"].Codes = vbaItem.Codes; | |
} | |
else | |
{ | |
//Copy other modules code and data | |
System.Diagnostics.Debug.Print(vbaItem.Name); | |
int vbaMod = 0; | |
Worksheet sheet = target.Worksheets.GetSheetByCodeName(vbaItem.Name); | |
if (sheet == null) | |
{ | |
vbaMod = target.VbaProject.Modules.Add(vbaItem.Type, vbaItem.Name); | |
} | |
else | |
{ | |
vbaMod = target.VbaProject.Modules.Add(sheet); | |
} | |
target.VbaProject.Modules[vbaMod].Codes = vbaItem.Codes; | |
if ((vbaItem.Type == VbaModuleType.Designer)) | |
{ | |
//Get the data of the user form i.e. designer storage | |
byte[] designerStorage = templateFile.VbaProject.Modules.GetDesignerStorage(vbaItem.Name); | |
//Add the designer storage to target Vba Project | |
target.VbaProject.Modules.AddDesignerStorage(vbaItem.Name, designerStorage); | |
} | |
} | |
}//foreach | |
//Save the target workbook | |
target.Save(outputDir + "outputDesignerForm.xlsm", SaveFormat.Xlsm); |