-3

enter image description hereI just can't find the property, but I need the code to work without castingenter image description here

Vita
  • 55
  • 7
  • What error is it giving you? It seems like that should work. Could you give more context to your code? How are you initializing "workSheet"? – mattjr747 Oct 11 '22 at 18:36
  • There is not an error, I just don`t wanna cast all the time, so I read it should be the embed interop property set as true so the compiler will cast it itself, but I can`t find where it`s located. – Vita Oct 11 '22 at 20:21
  • Oh I think I see what you are saying. I saw the red underlined "Autofit" and thought there was an error. See my answer and let me know if it works as you want. – mattjr747 Oct 11 '22 at 21:21

1 Answers1

1

The EmbedInteropTypes property can be found for each project Reference as follows:

  1. In Solution Explorer go to your project -> References
  2. Right click the desired Reference -> Properties
  3. Adjust the "Embed Interop Types" property as desired.

I am not sure this will actually fix your problem. The property is set to true by default for any Microsoft.Office.Interop references. Also I have seen some people with very high reputation recommending against doing this. See the following link: What's the difference setting Embed Interop Types true and false in Visual Studio?

If C# is requiring explicit casts, it could also be that you are using an older version of C#. See the following link where the following is mentioned:

Earlier versions of C# require explicit casting for these operations because ExcelApp.Columns[1] returns an Object, and AutoFit is an Excel Range method. The following lines show the casting.

mattjr747
  • 84
  • 4
  • I don't have references, it's dependencies. There are nuget packages, and no properties but "name" and "path", so I thought it`s located in another place, I use vs 2022, c# 10, dot.net 6, maybe this is another problem here, but thank you anyway, your answer can be useful for others) – Vita Oct 12 '22 at 17:38
  • Ah, your project type must be different than mine. Under "Dependencies" do you have a "COM" section? If not, are you using a Nuget package for this Excel stuff or are you using the Microsoft Office 16.0 Object Library? Cause if I create a .NET standard project I have dependencies instead of references. However, I can go to COM -> right click Interop.Microsoft.Office.Core -> Properties -> Change Embed Interop Types as desired – mattjr747 Oct 12 '22 at 18:21