Copia VBA Macro UserForm DesignerStorage dal modello alla cartella di lavoro di destinazione
Possibili scenari di utilizzo
Aspose.Cells consente di copiare un progetto VBA da un file Excel in un altro file Excel. Il progetto VBA è costituito da vari tipi di moduli, ad esempio Documento, Procedurale, Designer, ecc. Tutti i moduli possono essere copiati con un semplice codice, ma per il modulo Designer sono presenti alcuni dati aggiuntivi chiamati Designer Storage a cui è necessario accedere o copiare. I due metodi seguenti si occupano di Designer Storage.
Copia VBA Macro UserForm DesignerStorage dal modello alla cartella di lavoro di destinazione
Vedere il seguente codice di esempio. Copia il progetto VBA dal filemodello di file Excel in una cartella di lavoro vuota e la salva come filefile Excel di output. Se apri il progetto VBA all’interno del file Excel modello, vedrai un modulo utente come mostrato di seguito. Il modulo utente è costituito da Designer Storage, quindi verrà copiato utilizzandoVbaModuleCollection.GetDesignerStorage()eVbaModuleCollection.AddDesignerStorage()metodi.
Lo screenshot seguente mostra il file Excel di output e il suo contenuto che sono stati copiati dal file Excel modello. Quando fai clic sul pulsante 1, si apre il modulo utente VBA che a sua volta ha un pulsante di comando che mostra una finestra di messaggio al clic.
Codice d’esempio
// 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); |