''Microsoft.Office.Interop.Excel.Worksheet' does not contain a definition for 'Range''

Santosh Umarani 81 Reputation points
2021-08-06T10:03:20.317+00:00

Hi,

While using the code :
Worksheet WSheet { get; }
WSheet.Range["A1", WSheet.Cells[content.GetLength(0), content.GetLength(1)]].Value = content;
This code is running perfectly fine in the machine where I am running the code.
But when I try to run this exe in another machine, I am getting the following exception:

Exception: Type='RuntimeBinderException'. Message=''Microsoft.Office.Interop.Excel.Worksheet' does not contain a definition for 'Range''

at CallSite.Target(Closure , CallSite , Worksheet )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1T0,TRet

Does usage of WSheet.Range dependent on the version of Office ? or any other such dependency is there ? How can I resolve this issue ?
Kindly waiting for your response.

Thanks,
Santosh

Developer technologies | .NET | Other
Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,656 Reputation points
    2021-08-09T05:47:47.107+00:00

    Hi SantoshUmarani-1390,
    From this document, you can know that the Range property is for an application-level add-in.
    And maybe you can try get_Range method that McLean Schofield has pointed in this thread.
    Best Regards,
    Daniel Zhang


    If the response 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.

    1 person found this answer helpful.
    0 comments No comments

  2. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-08-06T10:43:10.71+00:00

    This is one reason not to use Interop.Excel as versions can cause issues such as your current issue. A better solution when working with .xlsx is to either use Open XML for Excel or a third party library such as SpreadSheetLight or EPPlus, both are free.

    Some examples https://github.com/karenpayneoregon/ExcelUnleashed

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.