Hi,
Hi,
Thank you please let me know - this feature is very important to us - and I am sure many users who want to manipulate VBA code will also want to be able to add new modules,
Hi,
Hi,
Thank you - I look forward to it!
Hi,
Thanks for your using Aspose.Cells.
Please download and try the latest fix: Aspose.Cells for .NET v8.4.1.2 and let us know your feedback.
Thanks! I see the Modules.Add method, but I don’t understand how to use it. Can you provide documentation or a simple example how to use it to copy the code from a worksheet and duplicate it, or copy a module and add it as a new module?
Code samples appreciated. I call Modules.Add adding a worksheet or a moduletype and name, and keep getting errors when saving the workbook - that there are duplicate keys - Sheet11 was already present for example.
First I add the worksheet via AddCopy, then I try and add the worksheet to the modules collection. How should I be doing this, to add a copy of a worksheet, and then add a module for the new worksheet, based on the old worksheet’s module>?
Hi,
Thanks for using Aspose.Cells.
I tried the following code and it worked fine. I have attached the output xlsm file for your reference.
C#
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
workbook.VbaProject.Modules.Add(worksheet);
workbook.Save(“output.xlsm”);
I think I see at least part of the problem - when I change the name of the module, there is still a non-public property that keeps the old name, Sheet 11, so there will be multiples of that I guess. Can you provide a code snippet that works?
Hi,
Thanks for your posting and using Aspose.Cells.
Please provide us your code and source Excel file so that we could look into this issue further and update you.
I will try to get a sample together by tomorrow. In the meantime, if you:
Hi,
Thanks for your steps and using Aspose.Cells.
We were able to replicate the exception by following your steps using the source xlsm file attached by me with this post and the following sample code.
We have logged this issue in our database for investigation. We will look into it and fix this issue. Once, the issue is resolved or we have some other update for you, we will let you know asap.
This issue has been logged as
- CELLSNET-43614 - Adding module causes duplicate keys exception on saving workbook
C#
string filePath = @“F:\Shak-Data-RW\Downloads\Book1.xlsm”;
Workbook workbook = new Workbook(filePath);
int idx;
Worksheet worksheet=null;
VbaModule mod = null;
//Set One
idx = workbook.Worksheets.AddCopy(1);
worksheet = workbook.Worksheets[idx];
idx = workbook.VbaProject.Modules.Add(worksheet);
mod = workbook.VbaProject.Modules[idx];
mod.Name = “newNameString”;
//Set Two
idx = workbook.Worksheets.AddCopy(1);
worksheet = workbook.Worksheets[idx];
idx = workbook.VbaProject.Modules.Add(worksheet);
mod = workbook.VbaProject.Modules[idx];
mod.Name = “newNameString2”;
//This will throw exception
workbook.Save(“output.xlsm”);
at Aspose.Cells.Workbook.Save(String fileName, SaveOptions saveOptions)
at Aspose.Cells.Workbook.Save(String fileName)
Additional information: Item has already been added. Key in dictionary: 'Sheet3' Key being added: 'Sheet3'
Thanks Shakeel! As I mentioned earlier, I found that though the Name property of the new module is correctly changed, there is a non-public member (you can see it if you view the module in the watch window) that maintains the “Sheet3” designation. Perhaps this is the problem.
Hi,
Thanks for your elaboration and using Aspose.Cells.
We understands this issue now and hopefully this issue will be fixed soon. Once, there is some news for you, we will let you know asap.
Hi Shakeel! Any update on this issue?
Hi,
This is great news - we look forward to the updated DLL. Thanks!