Add a library reference to VBA project in workbook
In Microsoft Excel, you can add a library reference to the VBA project by clicking the Tools > References… manually. It will open the following dialog box which will help you to select from existing references or browse your library yourself.
But sometimes, you need to add or register the library reference to the VBA project through code. You can do it using Aspose.Cells VbaProject.getReferences().addRegisteredReference() method.
How to Add a library reference to VBA project in workbook
The following sample code adds or registers two library references to the VBA project of the workbook using VbaProject.getReferences().addRegisteredReference() method.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(AddLibraryReference.class); | |
Workbook workbook = new Workbook(); | |
VbaProject vbaProj = workbook.getVbaProject(); | |
vbaProj.getReferences().addRegisteredReference("stdole", | |
"*\\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\\Windows\\system32\\stdole2.tlb#OLE Automation"); | |
vbaProj.getReferences().addRegisteredReference("Office", | |
"*\\G{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}#2.0#0#C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE14\\MSO.DLL#Microsoft Office 14.0 Object Library"); | |
workbook.save(dataDir + "output.xlsm"); |