Reputation: 87
I have pasted a code snippet below which was written by someone some years ago. This is regarding exporting some data into an excel file based on a template. Can someone please help we with what the below code means as I am not able to figure it out? Thanks in advance.
_xlTmp = New Excel.Application
_xlTmp.Workbooks.Open(_fileName, , True, , , , , , , True)
Dim xlNewSheet = CType(_xlTmp.Worksheets.Item("SHEET1"), Excel.Worksheet) xlNewSheet.Copy(CType(_xlTmp.Worksheets("SHEET2"), Excel.Worksheet))
xlNewSheet = CType(_xlTmp.Worksheets.Item("SHEET1 (2)"), Excel.Worksheet)
Upvotes: 0
Views: 142
Reputation: 710
'Opens Excel and stores a reference to Excel process
xlTmp = New Excel.Application
'Opens the excel file with path '_fileName'
_xlTmp.Workbooks.Open(_fileName, , True, , , , , , , True)
'Stores a reference to Sheet named "SHEET1"
Dim xlNewSheet = CType(_xlTmp.Worksheets.Item("SHEET1"), Excel.Worksheet)
'Copies the refered sheet to sheet named "SHEET2" - replacing it
xlNewSheet.Copy(CType(_xlTmp.Worksheets("SHEET2"), Excel.Worksheet))
'Reference to the copied sheet
xlNewSheet = CType(_xlTmp.Worksheets.Item("SHEET1 (2)"), Excel.Worksheet)
However, if you use Excel COM you should code in a different way, so you can dispose all objects "referencing something" in Excel. You will also get exceptions if the filename or sheets with given names are not found.
Upvotes: 1