نسخ مصمم مستخدم النموذج التقديمي للماكرو VBA من القالب إلى كتاب العمل الهدف

سيناريوهات الاستخدام المحتملة

تسمح Aspose.Cells لك بنسخ مشروع VBA من ملف Excel واحد إلى ملف Excel آخر. يتكون مشروع VBA من أنواع مختلفة من الوحدات مثل المستند والوظيفة والمصمم وما إلى ذلك. يمكن نسخ جميع الوحدات بكود بسيط ولكن بالنسبة لوحدة المصمم، هناك بعض البيانات الإضافية تسمى تخزين المصمم التي يجب الوصول إليها أو نسخها. تتعامل الطرقتان التاليتان مع تخزين المصمم.

نسخ تصميم الاستوديو Form UserForm VBA Macro من القالب إلى دفتر العمل الهدف

يرجى الاطلاع على الشفرة النموذجية التالية. تقوم بنسخ مشروع VBA من ملف Excel النموذجي إلى مصنف فارغ وتحفظه كـ ملف Excel الناتج. إذا فتحت المشروع VBA داخل ملف Excel النموذجي، سترى نموذج مستخدم كما هو موضح أدناه. يتألف نموذج المستخدم من مخزن المصمم، لذا سيتم نسخه باستخدام الطرق VbaModuleCollection.GetDesignerStorage() و VbaModuleCollection.AddDesignerStorage().

todo:image_alt_text

تُظهر الصورة الشاشية التالية ملف Excel الناتج ومحتوياته التي تم نسخها من ملف Excel النموذجي. عند النقر فوق Button 1 ، يفتح نافذة المشروع الماكرو VBA التي تحتوي بدورها على زر أمر يعرض مربع رسالة عند النقر.

todo:image_alt_text

الكود المثالي

// 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);