Search code examples
excelvb.netuipathlocked

UiPath - VB Net - Excel - Unlock Specific Column


I am trying to Unlock a Specific Column Range (Column I & J) by using an Invoke Code and I am also running other functions in this same block, like Hiding a specific Column etc.

I am able to Unlock all the Columns using "excelWS.Columns.Locked = False But whenever I try to specify a specific range I am just met with the following Exception

Invoke Code - Format Excel Worksheet: Exception has been thrown by the target of an invocation

This is how my Invoke Code is Setup:

Dim excelWB As Microsoft.Office.Interop.Excel.Workbook
excelApp = New Microsoft.Office.Interop.Excel.ApplicationClass
excelWB = excelApp.Workbooks.Open(excelFilePath)
excelWS = CType(excelWB.Worksheets(excelSheet), Microsoft.Office.Interop.Excel.Worksheet)
'Hide the Column'
excelWS.Range("M:M").EntireColumn.Hidden = True
'Set Bottom Margin to 1.1'
excelWS.PageSetup.BottomMargin = excelApp.InchesToPoints(0.433070866141732)
'Fit to One Page Width'
excelWS.PageSetup.Zoom = False
excelWS.PageSetup.FitToPagesWide = 1
'Print Tile Rows'
excelWS.PageSetup.PrintTitleRows = "$1:$12"
'Header Page 1'
excelWS.PageSetup.CenterFooter = "Page &P"
'Unlock Columns'
'excelWS.Columns.Locked = False'
excelWS.Range("I:J").EntireColumn.Locked = False
excelWB.Save
excelWB.Close
excelApp.Quit

The problem line is the below:

excelWS.Range("I:J").EntireColumn.Locked = False

Any recommendations?

Thanks


Solution

  • So update on this; turns out there was a couple merged cells throughout the column, so once these were removed it started working again.