Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have been working on transcribing a vba code into a usable vb.net code.

Here is the VBA code I am working with:

VB
Sub simpleXlsMerger()
Dim bookList As Workbook
Dim mergeObj As Object, dirObj As Object, filesObj As Object, everyObj As Object
Application.ScreenUpdating = False
Set mergeObj = CreateObject("Scripting.FileSystemObject")
 
'change folder path of excel files here
Set dirObj = mergeObj.Getfolder("folder of excel docs")
Set filesObj = dirObj.Files
For Each everyObj In filesObj
Set bookList = Workbooks.Open(everyObj)
 
'change "A2" with cell reference of start point for every files here
'for example "B3:IV" to merge all files start from columns B and rows 3
'If you're files using more than IV column, change it to the latest column
'Also change "A" column on "A65536" to the same column as start point
Range("A7:IV" & Range("A1048500").End(xlUp).Row).Copy
ThisWorkbook.Worksheets(1).Activate

'Do not change the following column. It's not the same column as above
Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial
Application.CutCopyMode = False
bookList.Close savechanges:=False


Next
End Sub


This is what I have come up with so far:

VB
Private Sub excelmerger()

        Dim dialog As New FolderBrowserDialog()
        dialog.RootFolder = Environment.SpecialFolder.DesktopDirectory
        dialog.Description = "Select Application Configeration Files Path"
        dialog.ShowNewFolderButton = False

        dialog.ShowDialog()

        Dim workb As Excel.Workbooks
        Dim exapp As New Excel.Application
        Dim bookList As Microsoft.Office.Interop.Excel.Workbook
        Dim ThisWorkbook As Excel.Workbook = exapp.Workbooks.Add()

        Dim mergeObj As Object = CreateObject("Scripting.FileSystemObject")
        Dim dirObj As Object = mergeObj.Getfolder(dialog.SelectedPath)
        Dim filesObj As Object = dirObj.Files
        Dim everyObj As Object
        bookList.Application.ScreenUpdating = False

        'change folder path of excel files here
        For Each everyObj In filesObj
            bookList = workb.Open(everyObj)

            'change "A2" with cell reference of start point for every files here
            'for example "B3:IV" to merge all files start from columns B and rows 3
            'If you're files using more than IV column, change it to the latest column
            'Also change "A" column on "A65536" to the same column as start point
            Dim exr As Excel.Range
            exr = exapp.Range("A7:IV" & exapp.Range("A1048500").End(Excel.XlDirection.xlUp).Row).Copy
            ThisWorkbook.Worksheets(1).Activate

            'Do not change the following column. It's not the same column as above
            exapp.Range("A65536").End(Excel.XlDirection.xlUp).Offset(1, 0).PasteSpecial()
            bookList.Application.CutCopyMode = False
            bookList.Close()

            'bookList.Save
            ' SaveChanges:=False

        Next
    End Sub


at the Dim ThisWorkbook As Excel.Workbook = exapp.Workbooks.Add() I keep getting a null object reference error. Not sure why.

As this is my first time doing this I was hoping to get some help.

Thanks in advance

What I have tried:

I have researched quite a few articles about vba to vb.net. As you can imagine most do not give great detail how to do this type of project.
Posted
Updated 10-Jul-17 10:05am
v3
Comments
Maciej Los 10-Jul-17 13:55pm    
And what's your question?
Member 11856456 10-Jul-17 14:01pm    
Sorry, I forgot to put that in my vb.net code its giving me a null object reference error at Dim ThisWorkbook As Excel.Workbook = exapp.Workbooks.Add(). I cant seem to get past this point.
Member 11856456 10-Jul-17 14:03pm    
just added that to my question, thanks for bringing that to my attention.
RedDk 10-Jul-17 14:40pm    
Please don't take this as anything other than an "in addition to" suggestion, but have you tried looking for "VSTO" at the microsoft MSDN? Since there are tons of sample code available for download, each targeting flavors of plugin development for the OFFICE platform, it's not very hard to find something which targets specific methods. If not for use as-is, in elucidating specific error keyword. It's worked for me.
Michael_Davies 10-Jul-17 15:31pm    
Advice : Before you exit your sub make sure you disconnect from the exapp;

While System.Runtime.InteropServices.Marshal.ReleaseComObject(exapp) <> 0
Application.DoEvents()
End While

exapp = Nothing

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900