Copying sheets from one Excel to another

I have used invoke code activity to copy data from one excel to another excel with retaining excel data format

Dim excel As Microsoft.Office.Interop.Excel.Application
Dim wb As Microsoft.Office.Interop.Excel.Workbook
Dim nb As Microsoft.Office.Interop.Excel.Workbook
Dim ws1 As Microsoft.Office.Interop.Excel.Worksheet
Dim p_ws As Microsoft.Office.Interop.Excel.Worksheet
Dim rng As Microsoft.Office.Interop.Excel.Range ‘capturing the range of sheet’
Dim p_rng As Microsoft.Office.Interop.Excel.Range ‘capturing the range of SHeet’
Try
excel = New Microsoft.Office.Interop.Excel.ApplicationClass’create the instance of excel work book’
wb = excel.Workbooks.Open(filepath)‘Open the excel the file from where data to copy’
excel.Visible=True
ws1=CType(wb.Sheets(sheetName),Microsoft.Office.Interop.Excel.Worksheet) ‘excel sheet name from where data to copy’
ws1.cells.Copy()

nb = excel.Workbooks.Open(filepath1)‘Open the excel the file where data to be paste’
excel.Visible=True

p_ws=CType(nb.Sheets(“SheetName”),Microsoft.Office.Interop.Excel.Worksheet)‘excel sheet name where data to paste’
p_ws.cells.PasteSpecial(XlPasteType.xlPasteValues)

Catch es As Exception
System.Windows.MessageBox.Show(es.Message)
End Try

1 Like