BC30574 Option Strict On

Guys, I want to change ColumnWidth using Microsoft.Office.Interop.Excel
Help me to solve 2 same errors in line 14, please
image

Full Code

Hey @YoungFave

Can you post the error that you’re getting on line 14th?

image
does not allow late binding

@YoungFave

Hello Young,
the directive Option Strict On disallows the using of late binding. As far as I know is it not possible to disable that in the Invoke Code Activity with VBdotNET language. You can use C# instead. You can find an approach to do that here.
Best regards
Stefan

@YoungFave
can you please share entire code with us. Please use the format button </> from editor. Thanks

Idea is about to check if surrounding the failing part with a CType would help

Yes, I know.
Maybe I can solve this error otherwise?

Dim Exc As Application
Dim Wb As Workbook
Dim MainWs As Worksheet

Exc = New ApplicationClass

Wb = exc.Workbooks.Open(in_FilePath)

MainWs = CType(Wb.Worksheets(in_MainSheet), Worksheet)

For Each Ws As Worksheet In Wb.Worksheets
	If Ws.Name <> in_MainSheet Then
		For Idx As Int32 = 0 To Ws.Columns.Count - 1
                Ws.Columns(Idx).ColumnWidth = MainWs.Columns(Idx).ColumnWidth
		Next
	End If
Next

Wb.Save()
Wb.Close()

This error usually come when types of variable and value are not the same.
But I don’t know, what’s wrong now

Thank you a lot.

It works:

For Idx As Int32 = 1 To Ws.Columns.Count
            CType(Ws.Columns(Idx), Range).ColumnWidth = CType(MainWs.Columns(Idx), Range).ColumnWidth
Next

I failed a bit with index. In Columns it starts with “1”.

It’s slow a bit.
I will try to speed it up in the future.
I suppose, it’s slow because of big ranges, maybe I could take only one cell from each column and get ColumnWidth. I don’t know.

It’s faster:

For Idx As Int32 = 1 To in_MeaningColumnsCount
            CType(Ws.Columns(Idx), Range).ColumnWidth = CType(MainWs.Columns(Idx), Range).ColumnWidth
		Next

Default Worksheet.Columns.Count = 16 thousands

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