Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Aspose.Cells allows you to copy a VBA project from one Excel file into another Excel file. A VBA project consists of various types of modules, such as Document, Procedural, Designer, etc. All modules can be copied with simple code, but for the Designer module there is extra data called Designer Storage that must be accessed or copied. The following two methods deal with Designer Storage:
Please see the following sample code. It copies the VBA project from the template Excel file into an empty workbook and saves it as the output Excel file. If you open the VBA project inside the template Excel file, you will see a User Form as shown below. The User Form consists of Designer Storage, so it will be copied using VbaModuleCollection.GetDesignerStorage() and VbaModuleCollection.AddDesignerStorage() methods.

The following screenshot shows the output Excel file and its contents, which were copied from the template file. When you click Button 1, it opens the VBA User Form, which contains a command button that displays a message box when clicked.

#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
Workbook target;
Workbook templateFile(srcDir + u"sampleDesignerForm.xlsm");
WorksheetCollection templateSheets = templateFile.GetWorksheets();
WorksheetCollection targetSheets = target.GetWorksheets();
for (int i = 0; i < templateSheets.GetCount(); ++i)
{
Worksheet ws = templateSheets.Get(i);
if (ws.GetType() == SheetType::Worksheet)
{
Worksheet s = targetSheets.Add(ws.GetName());
s.Copy(ws);
s.GetCells().Get(u"A2").PutValue(u"VBA Macro and User Form copied from template to target.");
}
}
VbaProject templateVbaProject = templateFile.GetVbaProject();
VbaProject targetVbaProject = target.GetVbaProject();
VbaModuleCollection templateModules = templateVbaProject.GetModules();
for (int i = 0; i < templateModules.GetCount(); ++i)
{
VbaModule vbaItem = templateModules.Get(i);
if (vbaItem.GetName() == u"ThisWorkbook")
{
targetVbaProject.GetModules().Get(u"ThisWorkbook").SetCodes(vbaItem.GetCodes());
}
else
{
std::wcout << reinterpret_cast<const wchar_t*>(vbaItem.GetName().GetData()) << std::endl;
int vbaMod = 0;
Worksheet sheet = targetSheets.GetSheetByCodeName(vbaItem.GetName());
if (sheet.IsNull())
{
vbaMod = targetVbaProject.GetModules().Add(vbaItem.GetType(), vbaItem.GetName());
}
else
{
vbaMod = targetVbaProject.GetModules().Add(sheet);
}
targetVbaProject.GetModules().Get(vbaMod).SetCodes(vbaItem.GetCodes());
if (vbaItem.GetType() == VbaModuleType::Designer)
{
Vector<uint8_t> designerStorage = templateVbaProject.GetModules().GetDesignerStorage(vbaItem.GetName());
targetVbaProject.GetModules().AddDesignerStorage(vbaItem.GetName(), designerStorage);
}
}
}
target.Save(outDir + u"outputDesignerForm.xlsm", SaveFormat::Xlsm);
Aspose::Cells::Cleanup();
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.