Cool Blue Cool Blue - 7 months ago 43
C# Question

VS 2017 Document-level add-in build fails with com visible components

I have VS 2013 installed and I added 2017 RC.

I made a document-level modified excel solution with some customisation in C# and this works fine.

Then I added some VBA code in the spreadsheet and added a com-visible class following this link (which fails if I try to open it in chrome but works ok in edge).

In ThisWorkbook.cs I have the following code...

using System.Runtime.InteropServices;

namespace WeekendingTabsNET40
{
[ComVisible(true)]
public interface IWeekending
{
string DisplayTaggedSheets ();
string DisplayDates ();
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class QueryWeekending : IWeekending
{
private readonly WeekEnding.WeekEnding _we;
public QueryWeekending ()
{
_we = Globals.ThisWorkbook?.WeekEnding;
}
string IWeekending.DisplayTaggedSheets ()
{
return _we.DisplayTaggedSheets();
}
string IWeekending.DisplayDates ()
{
return _we.DisplayDates();
}
}

public partial class ThisWorkbook
{
public WeekEnding.WeekEnding WeekEnding;

private void ThisWorkbook_Startup(object sender, System.EventArgs e)
{
WeekEnding = new WeekEnding.WeekEnding(this, Globals.Factory);
}

private void ThisWorkbook_Shutdown(object sender, System.EventArgs e)
{
}

protected override object GetAutomationObject ()
{
return new QueryWeekending();
}

#region VSTO Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisWorkbook_Startup);
this.Shutdown += new System.EventHandler(ThisWorkbook_Shutdown);
}

#endregion
}
}


After I set
ReferenceAssemblyFromVbaProject
to true and build the project, I get the following error...


Error MSB3217: Cannot register assembly "obj\Debug\WeekendingTabsNET40.dll". Could not load file or assembly 'Microsoft.VisualStudio.Tools.Office.BuildTasks, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. (1, 1)


enter image description here

This with an elevated instance of VS 2017 RC.

Can anybody advise what is the problem?




Build output



Rebuild All started ...
Build started 21/02/2017 2:26:37 AM.
1>Project "C:\Users\Admin\Documents\Visual Studio 2017\Projects\WeekendingTabsNET40\WeekendingTabsNET40.sln" on node 1 (WeekendingTabsNET40:Rebuild target(s)).
1>ValidateSolutionConfiguration:
Building solution configuration "Debug|Any CPU".
1>Project "C:\Users\Admin\Documents\Visual Studio 2017\Projects\WeekendingTabsNET40\WeekendingTabsNET40.sln" (1) is building "C:\Users\Admin\Documents\Visual Studio 2017\Projects\WeekendingTabsNET40\WeekendingTabsNET40\WeekendingTabsNET40.csproj" (2) on node 1 (Rebuild target(s)).
2>Project "C:\Users\Admin\Documents\Visual Studio 2017\Projects\WeekendingTabsNET40\WeekendingTabsNET40\WeekendingTabsNET40.csproj" (2) is building "C:\Users\Admin\Documents\Visual Studio 2017\Projects\WeekEndingTabs\WeekEnding\WeekEnding.csproj" (3:3) on node 1 (default targets).
3>GenerateTargetFrameworkMonikerAttribute:
Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
CoreCompile:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn\csc.exe /noconfig /nowarn:1701,1702 /nostdlib+ /platform:x86 /errorreport:prompt /warn:4 /define:DEBUG;TRACE /highentropyva+ /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Microsoft.CSharp.dll" /link:"C:\Program Files (x86)\Microsoft Visual Studio\Shared\Visual Studio Tools for Office\PIA\Office15\Microsoft.Office.Interop.Excel.dll" /link:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.Office.Tools.Common.dll" /reference:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.Office.Tools.Common.v4.0.Utilities.dll" /link:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.Office.Tools.dll" /reference:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.Office.Tools.Excel.dll" /reference:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.Office.Tools.Excel.v4.0.Utilities.dll" /reference:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.Office.Tools.v4.0.Framework.dll" /reference:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.VisualStudio.Tools.Applications.Runtime.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Core.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Windows.Forms.dll" /debug+ /debug:full /filealign:512 /optimize- /out:obj\Debug\WeekEnding.dll /ruleset:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Team Tools\Static Analysis Tools\\Rule Sets\MinimumRecommendedRules.ruleset" /subsystemversion:6.00 /target:library /utf8output Properties\AssemblyInfo.cs WeekEnding.cs "C:\Users\Admin\AppData\Local\Temp\.NETFramework,Version=v4.5.AssemblyAttributes.cs"
Using shared compilation with compiler from directory: C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn
_CopyFilesMarkedCopyLocal:
Copying file from "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.Office.Tools.Common.v4.0.Utilities.dll" to "bin\Debug\Microsoft.Office.Tools.Common.v4.0.Utilities.dll".
Copying file from "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.Office.Tools.Excel.dll" to "bin\Debug\Microsoft.Office.Tools.Excel.dll".
Copying file from "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.Office.Tools.Excel.v4.0.Utilities.dll" to "bin\Debug\Microsoft.Office.Tools.Excel.v4.0.Utilities.dll".
Copying file from "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.Office.Tools.v4.0.Framework.dll" to "bin\Debug\Microsoft.Office.Tools.v4.0.Framework.dll".
Copying file from "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.VisualStudio.Tools.Applications.Runtime.dll" to "bin\Debug\Microsoft.VisualStudio.Tools.Applications.Runtime.dll".
CopyFilesToOutputDirectory:
Copying file from "obj\Debug\WeekEnding.dll" to "bin\Debug\WeekEnding.dll".
WeekEnding -> C:\Users\Admin\Documents\Visual Studio 2017\Projects\WeekEndingTabs\WeekEnding\bin\Debug\WeekEnding.dll
Copying file from "obj\Debug\WeekEnding.pdb" to "bin\Debug\WeekEnding.pdb".
3>Done Building Project "C:\Users\Admin\Documents\Visual Studio 2017\Projects\WeekEndingTabs\WeekEnding\WeekEnding.csproj" (default targets).
2>CoreResGen:
"C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\resgen.exe" /useSourcePath /r:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Microsoft.CSharp.dll" /r:"C:\Users\Admin\Documents\Visual Studio 2017\Projects\WeekendingTabsNET40\packages\Microsoft.Office.Interop.Excel.15.0.4795.1000\lib\net20\Microsoft.Office.Interop.Excel.dll" /r:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.Office.Tools.Common.dll" /r:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.Office.Tools.Common.v4.0.Utilities.dll" /r:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.Office.Tools.dll" /r:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.Office.Tools.Excel.dll" /r:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.Office.Tools.Excel.v4.0.Utilities.dll" /r:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.VisualStudio.Tools.Applications.Runtime.dll" /r:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll" /r:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Core.dll" /r:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.dll" /r:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Windows.Forms.dll" /r:"C:\Users\Admin\Documents\Visual Studio 2017\Projects\WeekEndingTabs\WeekEnding\bin\Debug\WeekEnding.dll" /compile Properties\Resources.resx,obj\Debug\WeekendingTabsNET40.Properties.Resources.resources Sheet1.resx,obj\Debug\WeekendingTabsNET40.Sheet1.resources ThisWorkbook.resx,obj\Debug\WeekendingTabsNET40.QueryWeekending.resources
Processing resource file "Properties\Resources.resx" into "obj\Debug\WeekendingTabsNET40.Properties.Resources.resources".
Processing resource file "Sheet1.resx" into "obj\Debug\WeekendingTabsNET40.Sheet1.resources".
Processing resource file "ThisWorkbook.resx" into "obj\Debug\WeekendingTabsNET40.QueryWeekending.resources".
GenerateTargetFrameworkMonikerAttribute:
Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
CoreCompile:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn\csc.exe /noconfig /nowarn:1701,1702 /nostdlib+ /platform:x86 /warn:4 /define:VSTO40;DEBUG;TRACE /highentropyva+ /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Microsoft.CSharp.dll" /link:"C:\Users\Admin\Documents\Visual Studio 2017\Projects\WeekendingTabsNET40\packages\Microsoft.Office.Interop.Excel.15.0.4795.1000\lib\net20\Microsoft.Office.Interop.Excel.dll" /reference:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.Office.Tools.Common.dll" /reference:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.Office.Tools.Common.v4.0.Utilities.dll" /reference:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.Office.Tools.dll" /reference:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.Office.Tools.Excel.dll" /reference:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.Office.Tools.Excel.v4.0.Utilities.dll" /reference:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.VisualStudio.Tools.Applications.Runtime.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Core.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Windows.Forms.dll" /reference:"C:\Users\Admin\Documents\Visual Studio 2017\Projects\WeekEndingTabs\WeekEnding\bin\Debug\WeekEnding.dll" /debug+ /debug:full /optimize- /out:obj\Debug\WeekendingTabsNET40.dll /ruleset:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Team Tools\Static Analysis Tools\\Rule Sets\MinimumRecommendedRules.ruleset" /subsystemversion:6.00 /resource:obj\Debug\WeekendingTabsNET40.Properties.Resources.resources /resource:obj\Debug\WeekendingTabsNET40.Sheet1.resources /resource:obj\Debug\WeekendingTabsNET40.QueryWeekending.resources /target:library /utf8output Chart1.cs Chart1.Designer.cs Properties\AssemblyInfo.cs Sheet1.cs Sheet1.Designer.cs Sheet10.cs Sheet10.Designer.cs Sheet11.cs Sheet11.Designer.cs Sheet2.cs Sheet2.Designer.cs Sheet3.cs Sheet3.Designer.cs Sheet4.cs Sheet4.Designer.cs Sheet5.cs Sheet5.Designer.cs Sheet6.cs Sheet6.Designer.cs Sheet7.cs Sheet7.Designer.cs Sheet8.cs Sheet8.Designer.cs Sheet9.cs Sheet9.Designer.cs ThisWorkbook.cs ThisWorkbook.Designer.cs Properties\Resources.Designer.cs Properties\Settings.Designer.cs "C:\Users\Admin\AppData\Local\Temp\.NETFramework,Version=v4.5.AssemblyAttributes.cs"
Using shared compilation with compiler from directory: C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\VisualStudio\v15.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets(628,9): error MSB3217: Cannot register assembly "obj\Debug\WeekendingTabsNET40.dll". Could not load file or assembly 'Microsoft.VisualStudio.Tools.Office.BuildTasks, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
2>Done Building Project "C:\Users\Admin\Documents\Visual Studio 2017\Projects\WeekendingTabsNET40\WeekendingTabsNET40\WeekendingTabsNET40.csproj" (Rebuild target(s)) -- FAILED.
1>Done Building Project "C:\Users\Admin\Documents\Visual Studio 2017\Projects\WeekendingTabsNET40\WeekendingTabsNET40.sln" (WeekendingTabsNET40:Rebuild target(s)) -- FAILED.

Build FAILED.

Time Elapsed 00:00:01.59
========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========


Errors




Error MSB3217 Cannot register assembly
"obj\Debug\WeekendingTabsNET40.dll". Could not load file or assembly
'Microsoft.VisualStudio.Tools.Office.BuildTasks, Version=15.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its
dependencies. The system cannot find the file specified.
C:\Users\Admin\Documents\Visual Studio
2017\Projects\WeekendingTabsNET40\WeekendingTabsNET40\WeekendingTabsNET40.csproj C:\Program
Files (x86)\Microsoft Visual
Studio\2017\Professional\MSBuild\Microsoft\VisualStudio\v15.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets 628


Update



I was able to clear all of the duplicates in the downloads cache using gacutil but it made no difference.

It seems like the problem is that I don't have Microsoft.VisualStudio.Tools.Office.BuildTasks.dll in the GAC.



I can see this file here...


C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualStudio.Tools.Office.BuildTasks\v4.0_12.0.0.0__b03f5f7f11d50a3a
\Microsoft.VisualStudio.Tools.Office.BuildTasks.dll


But it's not in the GAC (C:\Windows\assembly\GAC_MSIL)

So... I added it but of course its ver 12.0.0.0 that I have so I guess I need to find a way to download the 15.0.0.0 version.

Answer Source

I managed to solve this.
My main problem was understanding the configuration diaspora around VS. The DLL I needed was actually in

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\VisualStudio\v15.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.BuildTasks.dll

because I have VS 2017 RC installed, this is where the ver 15 DLLs are and the one that MSBuild is expecting to find.
So, finally, I just added that version of Microsoft.VisualStudio.Tools.Office.BuildTasks.dll to the GAC by opening the VS Command Prompt (as admin) and running

gacutil /i C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\VisualStudio\v15.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.BuildTasks.dll

So, apart from understanding the configuration friction (which is not really described properly in the msdn docs rubic's cube), I just needed to realise that the correct version of VisualStudio.Tools.Office.BuildTasks.dll needed to be registered in the GAC.
As mentioned by @HansPassant, the VS 2017 RC install failed to take care of this.