Have you added a reference to Microsoft.CSharp
as well as Microsoft.Office.Interop.Excel
? That fixed this issue for me in the past - the exception is saying that type information required to resolve the dynamic
type of the Value
property is missing. The exception usually tries to suggest the missing references as well.
What is happening with Embed Interop Types?
.NET (C#) 4.0 introduced dynamic
and the ability to treat COM host return object values as the dynamic
type. This is supposed to make our lives easier when writing code as we don't have to explicitly cast return objects to the type we want when writing code. This happens if "Embed Interop Types" is set to true
on your assembly; this is also the default value for references you add in VS.
Essentially VS is embedding the subset of the PIA for the types that you use in your assembly, at compile time (see the answer to "what is the advantage of .net4's new no pia feature [deploying PIA's]" for why this is good). But, it needs access to all the PIAs in the reference tree to do this.
dynamic
allows late binding. So I'm guessing in your case Value
has become a dynamic type which will be resolved at runtime, and that is going wrong as it can't get to the correct assembly and PIA reference. If you turn "Embed Interop Types" to false
then Value
becomes an object
type and this prevents the issue (but you now need to cast to the correct type at some point).
Why embed PIA information?
It can make it easier to deploy and support multiple versions of Office (as long as you don't use unsupported features in earlier/later versions than you tested). This is not easy, though; lots can go wrong.
MSDN - Walkthrough: Office Programming (C# and Visual Basic) mentions this, but doesn't make big deal of it other than to say it is a good thing.
As an example: if I fire up a project in VS2010 and reference the VSTO Excel 2010 interop located at
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office12\Microsoft.Office.Interop.Excel.dll
then I do not get this problem with Embed Interop Types set to true
as it automatically adds
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\Microsoft.CSharp.dll
as a reference as well. If I then remove the Microsoft.CSharp
reference, I get the compiler error, but it states the following error, which was how I found my solution originally:
One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll?