We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Adding vba code contained in exported VBA Files

I’m loading VBA code that has been extracted as files that are then added back to excel files on creation of a new instance of the file. These files are self contained classes, modules, forms, references,worksheets and the 'thisworkbook’components.
Here is my code which is contained inside a loop of the records telling me which items need to be added to this Excel file.

            Select Case FileType.ToUpper
                Case "MODULE", "FORM", "CLASS"
                    ExcelApp.VBE.ActiveVBProject.VBComponents.Import(PathName & FileName)

                Case "REFERENCE"
                    ExcelApp.VBE.ActiveVBProject.References.AddFromFile(PathName & FileName)

                Case "THISWORKBOOK"
                    ExcelApp.VBE.ActiveVBProject.VBComponents.Item("ThisWorkBook").CodeModule.AddFromFile(PathName & FileName)

                Case "WORKSHEET"
                    'Worksheets must be added prior to any code modules, because once
                    'the code is added it tries to execute against the worksheets as
                    'you add them.
                    OriginalWorkBook = ExcelApp.WorkBook.Open(PathName & FileName)
                    OriginalWorkSheet = CType(OriginalWorkBook.Worksheets(AddinName), Worksheet)
                    OriginalWorkSheet.Copy(After:=WorkBook.Sheets(WorkBook.Worksheets.Count))
                    OriginalWorkBook.Close()

                Case Else
                    'Do Nothing
            End Select

All I can see that Aspose has is the ability to add a Module and for me to then add the code to the module. Please notice that each type of file takes a specific syntax to add it. Ignore the THISWORKBOOK because that is simply adding a sheet and I can do that.
Thanks.

Hi Craig,


Thank you for contacting Aspose support.

I believe your requirement is to add the VBA code from the file. If this is correct, I am afraid, the current implementation of Aspose.Cells APIs do not offer any means to add the macros from files. If you can share a sample file, we can surely pass it on to the product team and log a formal request for feasibility analysis of required feature.
                Select Case FileType.ToUpper
                    Case "MODULE", "FORM", "CLASS"
                        ExcelApp.VBE.ActiveVBProject.VBComponents.Import(PathName & FileName)

                    Case "REFERENCE"
                        ExcelApp.VBE.ActiveVBProject.References.AddFromFile(PathName & FileName)

                    Case "THISWORKBOOK"
                        ExcelApp.VBE.ActiveVBProject.VBComponents.Item("ThisWorkBook").CodeModule.AddFromFile(PathName & FileName)

                    Case "WORKSHEET"
                        'Worksheets must be added prior to any code modules, because once
                        'the code is added it tries to execute against the worksheets as
                        'you add them.
                        OriginalWorkBook = New Workbook(PathName & FileName)
                        For Each OriginalWorkSheet In OriginalWorkBook.Worksheets
                            WorkBook.Worksheets(WorkBook.Worksheets.Count).Copy(OriginalWorkSheet)
                        Next
                        WorkBook.Worksheets(WorkBook.Worksheets.Count).Copy(OriginalWorkBook.Worksheets(0))
                        OriginalWorkBook = Nothing

                    Case Else
                        'Do Nothing
                End Select

Hi Craig,


Could you please also provide us a few sample files as mentioned in your following comments?

I’m loading VBA code that has been extracted as files that are then added back to excel files on creation of a new instance of the file. These files are self contained classes, modules, forms, references,worksheets and the ‘thisworkbook’ components.