How to use Office from Visual Studio C#?

前端 未结 4 1035
栀梦
栀梦 2021-02-08 01:21

The technique for adding a reference to the COM interop of Office in Visual Studio is to go to:

  1. References
  2. Add Reference
  3. Select the COM
相关标签:
4条回答
  • 2021-02-08 01:50

    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.

    0 讨论(0)
  • 2021-02-08 02:02

    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.

    0 讨论(0)
  • 2021-02-08 02:07

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

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

    0 讨论(0)
  • 2021-02-08 02:10

    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.

    0 讨论(0)
提交回复
热议问题