VBA code lost after save an xlsm to a stream

Hi Support,


I need to save an xlsm workbook on other destination files several times. I also need to maintain the original VBA macros in the new files. However, as you can see in the attachment example, I’m not able to maintain macros. If I save the workbook using Workbook.Save (stream, FileFormat) I get macros only in the first file that I save. If I use Workbook.Save (stream, SaveOptions.SaveFormat) I get no macros.

Am I doing something wrong?

Best regards

Hi Andrea,

Thanks for using Aspose.Cells.

When you save to stream, then specify the SaveFormat.Xlsm. It will then save the workbook in xlsm format inside the stream.

The following code works fine and generates two xlsm files from the source xlsm file and macros are preserved. I have attached the output xlsm files for your reference.

C#


string filePath = @“F:\Shak-Data-RW\Downloads\Test_Excel\Orig.xlsm”;


Workbook WBOrig = new Workbook(filePath);


using (MemoryStream stream1 = new MemoryStream())

{

WBOrig.Save(stream1, SaveFormat.Xlsm);

Workbook Destination1 = new Workbook(stream1);

Destination1.Save(filePath + @“Destination_SaveFormat1.xlsm”);

}


using (MemoryStream stream2 = new MemoryStream())

{

WBOrig.Save(stream2, SaveFormat.Xlsm);

Workbook Destination2 = new Workbook(stream2);

Destination2.Save(filePath + @“Destination_SaveFormat2.xlsm”);

}