Converting Uipath WorkbookApplication to Excel.Application for Invoke Code

Hi All,

Been trying to figure out how to be able to pass in an existing workbook output to my Invoke VB.Net Code. I know how to pass it via arguments and such, but in my code I use:
Microsoft.Office.Interop.Excel.Application

opposed to the UiPath library:
Uipath.Excel.WorkbookApplication

I basically want to be able to sick my invoke vb.net code into the excel application scope without having to read the file again and write some data into specific cells. But right now my code needs to handle the opening of the workbook. So there will always be a conflict-ion with the excel file being open in the excel scope as well as in my VBnet code and will throw an error. Anyone have any ideas on how to pass the workbook output to VBnet code to accomplish write jobs? My code is like this:

My code is like this For some reason it had to be separated from the post since I can’t post more than to links:
'Open the workbook you need
Dim xlApp As New Microsoft.Office.Interop.Excel.Application

'set visibility
xlApp.Visible = True
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook = xlApp.Workbooks.Open(“C:\Users\somefiletest.xlsx”)

'Reference the sheet
Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet = CType(xlBook.Worksheets(“Sheet1”), Microsoft.Office.Interop.Excel.Worksheet)

'enter data and write
Dim test(,) As String = {{“04”, “h1Text”}, {“00000079”, “h2Text”}, {“03”, “054”}}

Dim xlRange As Microsoft.Office.Interop.Excel.Range = xlSheet.Range(“A12:A15”,“B12:B15”)
xlRange.Value = test

Hi @CoreOfSmores,

You can try this code as this worked for me:

Dim worksheet As Microsoft.Office.Interop.Excel.Worksheet
Dim workbook As Microsoft.Office.Interop.Excel.Workbook
workbook = In_Workbook.CurrentWorkbook()
worksheet =DirectCast(workbook.Worksheets(“Sheet1”), Microsoft.Office.Interop.Excel.Worksheet)

Where In_Workbook is the UiPath WorkbookApplication passed as an argument in the Invoke code activity.

5 Likes

Thanks @jcvsalinas I will give that a try and report back to see how it goes for me. Did not know about the DirectCast function to convert the type.

You’re welcome hope this helps

Thanks a lot, I was trying this logic from two day. Finally got.

1 Like

Do you know how to convert the Excel.workbook to Uipath workbook application or workbook??