Invoke Code For Each Loop "Option Strict On Disallows Late Binding At Line 7"

No matter what variable type, IEnumerable, List, String, this for each loop throws the same “Option Strict On disallows late binding. At line 7”. As far as I can tell, this is the proper syntax for the for each loop where c is meant to act as the element for the list that is being iterated over. I’ve tried explicitly declaring the datatype with an As statement and I’ve tried it without and neither works. I’m beginning to think the invoke code activity is just broken. For a while, it was saying that I was trying to implicitly convert a string to an integer and now it doesn’t even give me that detail. I’m not sure if that’s better or worse because I never found out what integer it was referring to… all I did between then and now was take the columnlist variable out of the arguments and declared it directly within the invoke code block. Anyone have a clue what’s going on here?

Hi @Brian_Biggio

Try the below code in Invoke Code:

Dim columnlist As New List(Of String) From {"A", "B", "C"}
Dim xl As New Microsoft.Office.Interop.Excel.Application
Dim wb As Microsoft.Office.Interop.Excel.Workbook = xl.Workbooks.Open(excel_file_path)
Dim ws As Microsoft.Office.Interop.Excel.Worksheet = CType(wb.Worksheets(1), Microsoft.Office.Interop.Excel.Worksheet)
Dim column_range As Microsoft.Office.Interop.Excel.Range = ws.Columns
column_range.EntireColumn.Hidden = True

For Each c As String In columnlist
    Dim columnRange As Microsoft.Office.Interop.Excel.Range = CType(ws.GetType().InvokeMember("Columns", Reflection.BindingFlags.GetProperty, Nothing, ws, New Object() {c}), Microsoft.Office.Interop.Excel.Range)
    columnRange.EntireColumn.Hidden = False
Next

wb.Save()
wb.Close(False)
xl.Quit()

Invoke Code Arguments:

Regards

1 Like

That worked, thank you! I don’t understand the logic behind why it was saying the error was at line 7 (the for each loop), but if it works it works! Much appreciated.

Hi @Brian_Biggio

Hope your query is resolved, please mark my post as solution to close the loop.

Regards

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.