Copia VBA Macro UserForm DesignerStorage dal modello alla cartella di lavoro di destinazione
Possibili scenari di utilizzo
Aspose.Cells consente di copiare il 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 codice semplice, 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 Excelin 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-Java | |
//Create empty target workbook | |
Workbook target = new Workbook(); | |
//Load the Excel file containing VBA-Macro Designer User Form | |
Workbook templateFile = new Workbook(srcDir + "sampleDesignerForm.xlsm"); | |
//Copy all template worksheets to target workboook | |
int sheetCount = templateFile.getWorksheets().getCount(); | |
for(int idx=0; idx<sheetCount; idx++) | |
{ | |
Worksheet ws = templateFile.getWorksheets().get(idx); | |
if (ws.getType() == SheetType.WORKSHEET) | |
{ | |
Worksheet s = target.getWorksheets().add(ws.getName()); | |
s.copy(ws); | |
//Put message in cell A2 of the target worksheet | |
s.getCells().get("A2").putValue("VBA Macro and User Form copied from template to target."); | |
} | |
}//for | |
//----------------------------------------------- | |
//Copy the VBA-Macro Designer UserForm from Template to Target | |
int modCount = templateFile.getWorksheets().getCount(); | |
for(int idx=0; idx<modCount; idx++) | |
{ | |
VbaModule vbaItem = templateFile.getVbaProject().getModules().get(idx); | |
if (vbaItem.getName().equals("ThisWorkbook")) | |
{ | |
//Copy ThisWorkbook module code | |
target.getVbaProject().getModules().get("ThisWorkbook").setCodes(vbaItem.getCodes()); | |
} | |
else | |
{ | |
//Copy other modules code and data | |
System.out.println(vbaItem.getName()); | |
int vbaMod = 0; | |
Worksheet sheet = target.getWorksheets().getSheetByCodeName(vbaItem.getName()); | |
if (sheet == null) | |
{ | |
vbaMod = target.getVbaProject().getModules().add(vbaItem.getType(), vbaItem.getName()); | |
} | |
else | |
{ | |
vbaMod = target.getVbaProject().getModules().add(sheet); | |
} | |
target.getVbaProject().getModules().get(vbaMod).setCodes(vbaItem.getCodes()); | |
if ((vbaItem.getType() == VbaModuleType.DESIGNER)) | |
{ | |
//Get the data of the user form i.e. designer storage | |
byte[] designerStorage = templateFile.getVbaProject().getModules().getDesignerStorage(vbaItem.getName()); | |
//Add the designer storage to target Vba Project | |
target.getVbaProject().getModules().addDesignerStorage(vbaItem.getName(), designerStorage); | |
} | |
}//else | |
}//for | |
//Save the target workbook | |
target.save(outDir + "outputDesignerForm.xlsm", SaveFormat.XLSM); |