使用Visual Studio 2015开发MS Access 2016外接程序(功能区/VSTO) [英] Develop MS Access 2016 AddIn (Ribbon / VSTO) with Visual Studio 2015

查看:94
本文介绍了使用Visual Studio 2015开发MS Access 2016外接程序(功能区/VSTO)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望您能帮助我.我期待使用Visual Studio 2015(带状功能区栏,如VSTO)对我的第一个MS Access加载项进行编程,但还没有开始,我必须已经停止了. Visual Studio为几乎所有办公产品(MS Access)提供模板.我听说可以更改"例如Excel VSTO-Template,以便可以将其用于开发MS Access功能区. 有谁知道如何处理这个好的指示?您如何开发用于MS Access的VSTO?

Hope you can help me. I am looking forward to programming my first MS Access AddIn with Visual Studio 2015 (a Ribbon-Bar as VSTO), but not having started I have to stop already. Visual Studio provides templates for almost every office product, but MS Access. I heard it is possible to "change" for example the Excel VSTO-Template so it can be used to develop an MS Access Ribbon. Does anyone know a good instruction how to handle this? How are you developing VSTO for MS Access?

感谢您的帮助

推荐答案

There exists a tutorial for this. I haven't tried it, don't know if it works, but it sounds promising.

这里是多汁的食物.

  1. 要开始构建Access加载项,我可以创建Word(或InfoPath,PowerPoint,Project或Visio)加载项(Excel或Outlook也可以,但是它们具有其他特定于主机的冗余代码) ).

  1. To start building an Access add-in, I could create a Word (or InfoPath, PowerPoint, Project or Visio) add-in (Excel or Outlook would also work, but they have additional redundant host-specific code).

然后,我将在COM选项卡上添加对Microsoft Access对象库的引用(这还将引入对ADODB和DAO的引用).它还会引入Microsoft.Office.Core.dll,它会复制默认情况下已引用的Office.dll,因此,我将删除这两个重复项之一.

Then, I'll add a reference to the Microsoft Access Object Library, on the COM tab (this also pulls in references to ADODB and DAO). It also pulls in Microsoft.Office.Core.dll, which duplicates the Office.dll already referenced by default - so I'll delete one of these two duplicates.

在解决方案资源管理器中,我可以选择项目并单击显示所有文件"按钮.这样可以更轻松地打开ThisAddIn.Designer.cs文件-在这里,我可以将Application字段的声明和初始化从M.O.I.Word.Application更改为M.O.I.Access.Application.请注意,此步骤将更改自动生成的文件:通常不会重新生成该文件,但是如果我损坏了项目,可能会发生此情况(要点是,如果重新生成该文件,我的手动更改将会丢失):

In Solution Explorer, I can select the project and click the "show all files" button. This makes it easier to open the ThisAddIn.Designer.cs file – here I can change the declaration and initialization of the Application field from M.O.I.Word.Application to M.O.I.Access.Application. Note that this step changes a file that is auto-generated: the file is not normally re-generated, but can be if I corrupt the project (the point being that my manual changes will be lost if the file is re-generated):

//internal Microsoft.Office.Interop.Word.Application Application;
internal Microsoft.Office.Interop.Access.Application Application;

//this.Application = this.GetHostItem<Microsoft.Office.Interop.Word.Application>(typeof(Microsoft.Office.Interop.Word.Application), "Application");
this.Application = this.GetHostItem<Microsoft.Office.Interop.Access.Application>(typeof(Microsoft.Office.Interop.Access.Application), "Application");

  1. 这就是所有代码更改.现在对项目进行更改.有两种方法可以进行这些更改-通过IDE覆盖或抵消默认设置.或者直接手动编辑.csproj文件以替换默认设置.让我们看一下这两种方法:首先通过IDE,然后手动进行.

  1. That's all the code changes. Now for the project changes. There are two ways to do these changes – through the IDE in a way that overrides or counters the default settings; or by manually editing the .csproj file directly, to replace the default settings. Let's look at both approaches: first through the IDE, then manually.

首先,我将更改项目属性|调试|启动操作,以启动外部程序",并指定Access的路径,例如:

First, I'll change the Project Properties | Debug | Start action, to "Start external program", and specify the path to Access, for example:

C:\ Program Files(x86)\ Microsoft Office \ Office12 \ MSACCESS.EXE

C:\Program Files (x86)\Microsoft Office\Office12\MSACCESS.EXE

然后,我将创建一个与我的外接解决方案同名的.reg文件,并将其放在解决方案文件夹中.此reg文件用于注册用于Access的加载项(并为Word取消注册).下面列出的示例reg文件只是标准VSTO构建任务针对每种加载项类型所做的工作的转储,带有额外的一行.附加行(下面的第一个reg条目)只是删除了构建任务为Word放入的条目.其余条目对于Word和Access都是相同的,唯一的变化是将"Word"替换为"Access":

Then, I'll create a .reg file with the same name as my add-in solution, and put it in the solution folder. This reg file is used to register the add-in for Access (and unregister it for Word). The example reg file listed below is simply a dump of what the standard VSTO build task does for each add-in type, with an additional line. The additional line (the first reg entry below) simply removes the entry that the build task puts in for Word. The remaining entries are identical for Word and Access, with the only changing being to replace "Word" with "Access":

Windows Registry Editor Version 5.00
[-HKEY_CURRENT_USER\Software\Microsoft\Office\Word\Addins\MyAddIn]
[HKEY_CURRENT_USER\Software\Microsoft\Office\Access\Addins\MyAddIn]
"Description"="MyAddIn"
"FriendlyName"="MyAddIn"
"LoadBehavior"=dword:00000003
"Manifest"="C:\\Temp\\MyAddIn\\bin\\Debug\\MyAddIn.vsto|vstolocal"

  1. 在项目属性中|生成事件,我添加了一个生成后事件命令行,以将.reg文件合并到注册表中:

  1. In Project Properties | Build Events, I add a Post-build event commandline to merge the .reg file into the registry:

regedit/s"$(SolutionDir)$(SolutionName).reg"

regedit /s "$(SolutionDir)$(SolutionName).reg"

就是这样.现在,我可以按F5键来构建解决方案:这将为Access注册该加载项,并在加载该加载项的情况下运行Access进行调试.

That's it. I can now press F5 to build the solution: this will register the add-in for Access, and run Access for debugging with the add-in loaded.

请注意,除了将Debug属性设置为外部程序(上面的第4步)外,我还可以直接修改.csproj文件,以将Word设置为Access.例如,更改此:

Note that instead of setting the Debug property to an external program (step 4 above), I could modify the .csproj file directly, to set the from Word to Access. For example, change this:

<ProjectProperties HostName="Word"
  HostPackage="{D2B20FF5-A6E5-47E1-90E8-463C6860CB05}" OfficeVersion="12.0" VstxVersion="3.0"
  ApplicationType="Word" Language="cs" TemplatesPath="" DebugInfoExeName="#Software\Microsoft\Office\12.0\Word\InstallRoot\Path#WINWORD.EXE"
  AddItemTemplatesGuid="{147FB6A7-F239-4523-AE65-B6A4E49B361F}" />

…对此:

<ProjectProperties HostName="Access"
  HostPackage="{D2B20FF5-A6E5-47E1-90E8-463C6860CB05}" OfficeVersion="12.0" VstxVersion="3.0"
  ApplicationType="Access" Language="cs" TemplatesPath="" DebugInfoExeName="#Software\Microsoft\Office\12.0\Access\InstallRoot\Path#MSACCESS.EXE"
  AddItemTemplatesGuid="{147FB6A7-F239-4523-AE65-B6A4E49B361F}" />

请注意,如上所示,更改值会更改解决方案资源管理器中使用的图标.

Note that changing the value, as show above, changes the icons used in the Solution Explorer .

  1. 我还可以更改元素的Name值,以更改解决方案资源管理器中ThisAddIn.cs的父节点的名称.更改此:

<Host Name="Word" GeneratedCodeNamespace="MyAddIn" IconIndex="0">
  <HostItem Name="ThisAddIn" Code="ThisAddIn.cs" CanonicalName="AddIn" CanActivate="false" IconIndex="1" Blueprint="ThisAddIn.Designer.xml" GeneratedCode="ThisAddIn.Designer.cs" />
</Host>

…对此:

<Host Name="Access" GeneratedCodeNamespace="MyAddIn" IconIndex="0">
  <HostItem Name="ThisAddIn" Code="ThisAddIn.cs" CanonicalName="AddIn" CanActivate="false" IconIndex="1" Blueprint="ThisAddIn.Designer.xml" GeneratedCode="ThisAddIn.Designer.cs" />
</Host>

  1. 此外,注册由元素值确定.因此,除了将.reg文件设置为构建后任务(上述步骤5-6)之外,我还可以直接编辑.csproj来更改此设置:

<OfficeApplication>Word</OfficeApplication>

…对此:

<OfficeApplication>Access</OfficeApplication>

这篇关于使用Visual Studio 2015开发MS Access 2016外接程序(功能区/VSTO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆