I am trying to get the value in an Excel cell using VB.net. I get a Missing Member exception when I try to access the value

Gregory Vereschagin 1 Reputation point
2022-09-26T20:56:49.423+00:00

Here is the code that leads to the exception
Dim app As New Excel.Application
Dim wb As Excel.Workbook
wb = app.Workbooks.Open(wbName)
Dim ws As Excel.Worksheet
ws = CType(wb.Worksheets(wsName), Excel.Worksheet)
ws = wb.ActiveSheet

       Dim temval As Integer = ws.Cells(4, cc + cOff).value  'error at this statement  

I am compiling with Option Strict: Off. When I turn it on the above statement is flagged saying that late binding is not allowed with Option Strict On.

I am using Visual Studio 22 for the first time. I have run many other projects with Visual Studio 15 using the same as the foregoing. When I open these projects and turn on Option Strict there are numerous errors.

How do I get around this problem? Any help greatly appreciated.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,569 questions
Excel Management
Excel Management
Excel: A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.Management: The act or process of organizing, handling, directing or controlling something.
1,639 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 26,426 Reputation points Microsoft Vendor
    2022-10-05T03:09:14.313+00:00

    Hi @Gregory Vereschagin ,
    Late binding is not allowed when Option Strict is on.If you need to perform late binding, the only options are either to use reflection or to shut off Option Strict.
    An alternative solution is that you put most of your class in a file with Options Strict On, and then just put one method in a Partial Class in a separate file with Option Strict Off, then only that one method would be compiled loosely. The rest of the class would be compiled using the strict rules.
    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments