How to use Office from Visual Studio C#?

白昼怎懂夜的黑 提交于 2019-12-03 05:48:57

Wow that's a huge number of questions. I think that in general if your app is using the PIAs then you're assuming that your target audience has some version of Office installed. The PIAs will be installed in the GAC when the target user installs Office. If they don't have Office installed then why are you targeting Office?

Yes, the Office dlls are the correct way to automate Office. There's a list of the assemblies here, including some for Office 2007.

The answer is to "Copy Local" whatever assembly dll you get for the interop. Once you have the assembly dll in your output folder, add a reference to it, and check it into source control.

Now everyone has the referenced assembly dll.

do you use VSTO (Visual studio tools for office)?

http://msdn.microsoft.com/en-us/office/aa905533.aspx

An old thread, and probably most people would be happy with CopyLocal=True, however here's another way.. Use both (or more..? thinking Office 2010 if the problem still exists..) references in your project files, and either ignore or just tell MSBuild to ignore the "MSB3284" warning (Library not found). So include this in your .csproj file:

<COMReference Include="Microsoft.Office.Core">
   <Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
   <VersionMajor>2</VersionMajor>
   <VersionMinor>3</VersionMinor>
   <Lcid>0</Lcid>
   <WrapperTool>primary</WrapperTool>
   <Isolated>False</Isolated>
</COMReference>

Followed by:

<COMReference Include="Microsoft.Office.Core">
   <Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
   <VersionMajor>2</VersionMajor>
   <VersionMinor>4</VersionMinor>
   <Lcid>0</Lcid>
   <WrapperTool>primary</WrapperTool>
   <Isolated>False</Isolated>
</COMReference>

I would be interested to see if Microsoft provides a NuGet library for this - just to get everyone on to the same approach. I think that would remove the necessity for people to search the web for these answers... I believe this would be against Microsoft Office's license so they are the only ones to supply it.

BTW with copy local you have to be careful not to redistribute these library by mistake.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!