• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

VB.net / Excell application Manipulation

I am trying to write code that will open two Excel workbooks and paste one file to the "Master" file as a new sheet in VB.net.
Along with this request, I would like some help in trying to find a good resource for the microsoft object model.  What I have found so far is 100 ways to do one thing.  I would like to find something that is streamlined.

The code I have so far is:

            Dim oExcel, wbk, wbk2 As Object
            oExcel = CreateObject("Excel.Application")

            Dim oMain, w2 As Excel.Workbook
            Dim oSheet As Excel.Sheets
            Dim MMrpt As String = Me.Rpt
            Dim MMpath As String = Me.Path

            'Try
            Connection.Open()
            Dim gCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(sql, Connection)
            Dim ReportInfo As SqlClient.SqlDataReader = gCommand.ExecuteReader
            While ReportInfo.Read
                Me.Rpt = ReportInfo(0).ToString
                Me.Initialize()
                Me.SetParameters()
                Me.ExportType()
                wbk = oExcel.Workbooks.Open(Filename:=MMpath, UpdateLinks:=False, ReadOnly:=False)
                wbk2 = oExcel.Workbooks.Open(Filename:=Me.Path, UpdateLinks:=False, ReadOnly:=False)

This is where the problem is so far:   ****************************

                wbk.sheets.add(wbk2.activesheet)  
                wbk.save()
                wbk.close()
                wbk2.save()
                wbk2.close()
*******************************************************

            End While
            Connection.Close()
            Me.Rpt = MMrpt
            Me.Path = MMpath
0
tdsully
Asked:
tdsully
  • 2
  • 2
1 Solution
 
tdsullyAuthor Commented:
Is there a way to do this without instantiating excel at all?
0
 
RonaldBiemansCommented:
Hi tfsully, what is your question exactly, because what you are doing seems to be the correct way
0
 
tdsullyAuthor Commented:
Hi,

Thanks for your feed back

wbk.sheets.add(wbk2.activesheet)  

I come up with an error on this line.  What I would like to do is append two excel files together without opening the excel
application.  I would give you the exact error message, but I cannot test the app right now because of our month end reporting.

0
 
RonaldBiemansCommented:
Hi TDsully, I tested this and this seems to work

Dim oExcel As New Excel.Application
Dim oMain, w2 As Excel.Workbook

        oMain = oExcel.Workbooks.Open(Filename:="c:\t1.xls", UpdateLinks:=False, ReadOnly:=False)
        w2 = oExcel.Workbooks.Open(Filename:="c:\t2.xls", UpdateLinks:=False, ReadOnly:=False)
        w2.Sheets(1).Copy(After:=oMain.Sheets(1))
        oMain.Save()
        oMain.Close()
        w2.Save()
        w2.Close()
        oExcel = Nothing
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now