Microsoft Access Add-In 使用 VSTO
Microsoft Access Add-In using VSTO
我正在尝试扩展 MS Access 的功能。我希望用户能够从 Access 应用程序中触发此功能,最好是通过单击我介绍的按钮。我希望能够使用 C# 实现此功能。
如果我的目标是 MS Word、Excel、Outlook、PowerPoint、Visio、InfoPath,
或 Project,然后是 Visual Studio Office 工具(请参阅 here) would be ideal for this. However, Access was either never (officially) part of this initiative or was dropped from being officially supported by it quite some time ago. There is a company called Add-In Express that seems to support VSTO with Access, for example, but the license is quite expensive and I would prefer not to have third-party software involved if it can be done directly. There is an MS blog article from 2008 with a hack,它利用 "largely host-agnostic nature of VSTO add-in projects" 并将 Word add-in 转换为 Access add-in。它附带一个警告:
Note, however, that I'm not encouraging people to use this approach in production – we have not tested this behavior, and it is expressly not supported in any way. What I've done is to explore how VSTO is designed to be optimally host-agnostic, so that the add-in model is as flexible as possible – without going to the extreme of loose typing offered by the old "shared" add-in model.
这种方法是针对 最近的 SO 问题而提出的,并且显然已被提问者成功使用。虽然我担心缺乏官方支持,但这对于原型来说可能就足够了,我已经尝试使用它。
在没有任何问题的情况下执行步骤 1 到 8 后,我点击调试,MS Access 启动,然后抛出以下错误对话框:
Microsoft Office Customization Installer
There was an error during installation.
Downloading file:///C:/Temp/MyAddIn/bin/Debug/MyAddIn.vsto did not succeed.
Details:
************** Exception Text **************
System.Deployment.Application.DeploymentDownloadException: Downloading
file:///C:/Temp/MyAddIn/bin/Debug/MyAddIn.vsto did not succeed. --->
System.Net.WebException: Could not find a part of the path
'C:\Temp\MyAddIn\bin\Debug\MyAddIn.vsto'. ---> System.Net.WebException:
Could not find a part of the path 'C:\Temp\MyAddIn\bin\Debug\MyAddIn.vsto'.
---> System.IO.DirectoryNotFoundException: Could not find a part of the path
'C:\Temp\MyAddIn\bin\Debug\MyAddIn.vsto'.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess
access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize,
FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean
bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share, Int32 bufferSize, FileOptions options, String
msgPath, Boolean bFromProxy)
at System.Net.FileWebStream..ctor(FileWebRequest request, String path,
FileMode mode, FileAccess access, FileShare sharing, Int32 length, Boolean
async)
at System.Net.FileWebResponse..ctor(FileWebRequest request, Uri uri,
FileAccess access, Boolean asyncHint)
--- End of inner exception stack trace ---
at System.Net.FileWebResponse..ctor(FileWebRequest request, Uri uri,
FileAccess access, Boolean asyncHint)
at System.Net.FileWebRequest.GetResponseCallback(Object state)
--- End of inner exception stack trace ---
at System.Net.FileWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.FileWebRequest.GetResponse()
at
System.Deployment.Application.SystemNetDownloader.DownloadSingleFile
(DownloadQueueItem next)
--- End of inner exception stack trace ---
谷歌搜索 "vsto downloading file did not succeed" 带给我 here 这似乎是相关的:
The problem is that the Office application is looking for the deployment manifest (.vsto) and application manifest (.dll.manifest) and is unable to find them.
我在我的机器上搜索了一个 vsto 文件扩展名,唯一找到的是在我的 VS 解决方案文件夹中。所以我认为博客方法中的第 6 步可能是错误的,最后一行应该替换为这个 vsto 文件的路径。然而,这似乎不是问题。
然后我发现这个关于解决常见 VSTO 问题的 MSDN article 和常见错误 4 是匹配的。列出的 "solution" 是:
This issue is generally seen when the VSTO solution is trying to get certificate information (publisher name and other data) from Domain Controller and it times out. To resolve the issue, please install this hotfix :- KB 981574.
这个 links here 这是一个标题为
的页面
The splash screen stays open longer than usual when you try to start Excel on a computer that has the .NET Framework 3.5 SP1 installed
这似乎完全无关。这是破link吗?我不确定如何进行。
我认为 VSTO 工具真的不值得麻烦,此外,如果您创建一个“加载项”,那么这样的加载项将在 Access 启动时为所有 Access 应用程序加载,并且不限于您的一个应用程序。必须在办公室注册加载项通常会导致在 Access 中启动时出现问题。因此,您的加载项将必须加载,并且在出于任何原因只是启动 Access 的任何和所有情况下都必须加载。
所有 VSTO 工具真正做的就是设置一个办公室的 com 接口,在 .net 中构建一个“简单的”class,然后从你的 VBA 访问按钮。
因此,一种简单的方法是“仅”使用 VBA/office 代码中的 .net class。无论如何,弄乱 VSTO 真的没有多大帮助,当你让所有 VSTO 东西工作时,你只需在 .net 中编写一个简单的 class 就可以完成并完成。
几点提示:
确保选中 .net“注册”中的 COM 互操作框。请记住,此选项仅在您的开发计算机上的开发过程中为您的方便执行重气。
另一个选项(无论如何默认设置!!!)在程序集区域中,您只需确保“使程序集 COM 可见”。
要分发,您必须包含一个小的批处理文件或使用安装程序。那个简单的批处理文件或安装程序必须在目标机器(你非开发计算机)上执行“regasm”。
并确保将项目强制为 x86(假设您使用的是 office x32,这很可能就是这种情况)。所以不要使用“任何”CPU,而是强制项目到 x86 CPU。
除此之外,仅此而已。
这是一个简单的 .net class。假设 .net 4.5 或更高版本,这允许您从 Access VBA.
创建 zip 文件
(您需要 system.IO.Compression 和 system.io.compression.Filesystem 作为 .net 项目中的引用。结果是在没有任何第 3 方工具的情况下从 Access 压缩文件。
.net中的class是这样的:
Imports System.Runtime.InteropServices
Imports System.IO.Compression
Imports System.IO
<ClassInterface(ClassInterfaceType.AutoDual)>
Public Class AlbertCom1
Private m_Times2 As Integer
Public Sub MsgHello()
MsgBox("Hello world", MsgBoxStyle.Information, "VB.net example")
End Sub
Public Sub MyZipper(strFileName As String, strZipFile As String)
Using archive As ZipArchive = ZipFile.Open(strZipFile, ZipArchiveMode.Update)
archive.CreateEntryFromFile(strFileName, Path.GetFileName(strFileName), CompressionLevel.Fastest)
End Using
End Sub
Public Function GetConValue(strSetting As String) As String
' read a simple value from config file
Return My.Settings(strSetting).ToString
End Function
Public Property Times2 As Integer
Get
Return m_Times2
End Get
Set(value As Integer)
m_Times2 = value * 2
End Set
End Property
End Class
所以上面的内容很简单,也很简短。
在 Access VBA 中,在我们的按钮后面,我们可以使用这些代码:
Call/use 上面的 Hello 消息框方法:
Sub TestCOMHello()
Dim mycom As Object
Set mycom = CreateObject("AlbertCom1.AlbertCom1")
mycom.MsgHello
End Sub
注意上面的 LATE 绑定是如何工作的——这将在 VBA 中没有引用对象的情况下工作。 (因此需要 createObject())。
但是,以下示例使用了早期绑定,但都可以按照上述方式创建对象。
压缩来自 VBA 的文件:
Sub TestCOMZip()
Dim strFromFile As String
Dim strToFile As String
Dim mycom As New AlbertCom1.AlbertCom1
strFromFile = "c:\test\data.txt"
strToFile = "c:\test\data.zip"
mycom.MyZipper strFromFile, strToFile
End Sub
更多提示:
您实际上不必在 class 中构建自定义界面,虽然许多人对使用 AutoDual “不满意”,但我认为这种方法很好。互联网上花费所有时间创建自定义界面的“示例”实际上只是世界贫困的借口。
最后一个重要提示:
不要在 class 中公开任何不兼容的数据类型。如果您(无意或有意)暴露任何与标准“com”对象不兼容的 .net 对象类型,那么您的 .net class 将编译得很好,但 Access 将无法正确查看或使用该对象.因此,在上面的实际 class 中,将这些变量和例程保持(声明)为私有。 (其他代码和部分无关紧要 - 只有一个 class 就是我所说的)。
所以不要暴露 .net long 数据类型。如果你坚持使用字符串、标准集合、数组等,那么你应该没问题。您实际上“可以”将不兼容的对象类型公开为 .net 中的“对象”。 (Access 中的 Intel-sense 不会显示对象方法,但您仍然可以使用它们)。
虽然 public 函数甚至 public 变量对于您的对象方法来说是最简单的,这里是根据“标准”方法使用 get/set 如何class 方法已创建。
所以在 .net 中我们有:
Public Property Times2 As Integer
Get
Return m_Times2
End Get
Set(value As Integer)
m_Times2 = value * 2
End Set
End Property
在您的 VBA 代码中,我们有:
Sub TestTimes()
Dim mycom As New AlbertCom1.AlbertCom1
mycom.Times2 = 40
Debug.Print mycom.Times2
End Sub
output: 80
注意提前绑定,然后Access VBA编辑器会“吐出”class的属性和方法:
例如:
总的来说,使用 VSTO 是一个巨大的矫枉过正。只需构建一个简单的 .net class 来公开您需要从 VBA 到 use/call 的内容,然后您就可以参加比赛了。另一个大好处是这个 COM 对象可以从 windows 脚本、VB6、FoxPro、Excel、Word、power-point 等中使用。换句话说,你不限于任何一个平台或只是访问消费 + 使用您作为 COM 对象公开的简单 class。在大多数情况下,我很难为 VSTO 辩护。因此,现在可以从任何办公应用程序(VBA 代码)轻松使用此对象。
我正在尝试扩展 MS Access 的功能。我希望用户能够从 Access 应用程序中触发此功能,最好是通过单击我介绍的按钮。我希望能够使用 C# 实现此功能。
如果我的目标是 MS Word、Excel、Outlook、PowerPoint、Visio、InfoPath, 或 Project,然后是 Visual Studio Office 工具(请参阅 here) would be ideal for this. However, Access was either never (officially) part of this initiative or was dropped from being officially supported by it quite some time ago. There is a company called Add-In Express that seems to support VSTO with Access, for example, but the license is quite expensive and I would prefer not to have third-party software involved if it can be done directly. There is an MS blog article from 2008 with a hack,它利用 "largely host-agnostic nature of VSTO add-in projects" 并将 Word add-in 转换为 Access add-in。它附带一个警告:
Note, however, that I'm not encouraging people to use this approach in production – we have not tested this behavior, and it is expressly not supported in any way. What I've done is to explore how VSTO is designed to be optimally host-agnostic, so that the add-in model is as flexible as possible – without going to the extreme of loose typing offered by the old "shared" add-in model.
这种方法是针对
在没有任何问题的情况下执行步骤 1 到 8 后,我点击调试,MS Access 启动,然后抛出以下错误对话框:
Microsoft Office Customization Installer
There was an error during installation.
Downloading file:///C:/Temp/MyAddIn/bin/Debug/MyAddIn.vsto did not succeed.
Details:
************** Exception Text **************
System.Deployment.Application.DeploymentDownloadException: Downloading
file:///C:/Temp/MyAddIn/bin/Debug/MyAddIn.vsto did not succeed. --->
System.Net.WebException: Could not find a part of the path
'C:\Temp\MyAddIn\bin\Debug\MyAddIn.vsto'. ---> System.Net.WebException:
Could not find a part of the path 'C:\Temp\MyAddIn\bin\Debug\MyAddIn.vsto'.
---> System.IO.DirectoryNotFoundException: Could not find a part of the path
'C:\Temp\MyAddIn\bin\Debug\MyAddIn.vsto'.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess
access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize,
FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean
bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share, Int32 bufferSize, FileOptions options, String
msgPath, Boolean bFromProxy)
at System.Net.FileWebStream..ctor(FileWebRequest request, String path,
FileMode mode, FileAccess access, FileShare sharing, Int32 length, Boolean
async)
at System.Net.FileWebResponse..ctor(FileWebRequest request, Uri uri,
FileAccess access, Boolean asyncHint)
--- End of inner exception stack trace ---
at System.Net.FileWebResponse..ctor(FileWebRequest request, Uri uri,
FileAccess access, Boolean asyncHint)
at System.Net.FileWebRequest.GetResponseCallback(Object state)
--- End of inner exception stack trace ---
at System.Net.FileWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.FileWebRequest.GetResponse()
at
System.Deployment.Application.SystemNetDownloader.DownloadSingleFile
(DownloadQueueItem next)
--- End of inner exception stack trace ---
谷歌搜索 "vsto downloading file did not succeed" 带给我 here 这似乎是相关的:
The problem is that the Office application is looking for the deployment manifest (.vsto) and application manifest (.dll.manifest) and is unable to find them.
我在我的机器上搜索了一个 vsto 文件扩展名,唯一找到的是在我的 VS 解决方案文件夹中。所以我认为博客方法中的第 6 步可能是错误的,最后一行应该替换为这个 vsto 文件的路径。然而,这似乎不是问题。
然后我发现这个关于解决常见 VSTO 问题的 MSDN article 和常见错误 4 是匹配的。列出的 "solution" 是:
This issue is generally seen when the VSTO solution is trying to get certificate information (publisher name and other data) from Domain Controller and it times out. To resolve the issue, please install this hotfix :- KB 981574.
这个 links here 这是一个标题为
的页面The splash screen stays open longer than usual when you try to start Excel on a computer that has the .NET Framework 3.5 SP1 installed
这似乎完全无关。这是破link吗?我不确定如何进行。
我认为 VSTO 工具真的不值得麻烦,此外,如果您创建一个“加载项”,那么这样的加载项将在 Access 启动时为所有 Access 应用程序加载,并且不限于您的一个应用程序。必须在办公室注册加载项通常会导致在 Access 中启动时出现问题。因此,您的加载项将必须加载,并且在出于任何原因只是启动 Access 的任何和所有情况下都必须加载。
所有 VSTO 工具真正做的就是设置一个办公室的 com 接口,在 .net 中构建一个“简单的”class,然后从你的 VBA 访问按钮。
因此,一种简单的方法是“仅”使用 VBA/office 代码中的 .net class。无论如何,弄乱 VSTO 真的没有多大帮助,当你让所有 VSTO 东西工作时,你只需在 .net 中编写一个简单的 class 就可以完成并完成。
几点提示: 确保选中 .net“注册”中的 COM 互操作框。请记住,此选项仅在您的开发计算机上的开发过程中为您的方便执行重气。
另一个选项(无论如何默认设置!!!)在程序集区域中,您只需确保“使程序集 COM 可见”。
要分发,您必须包含一个小的批处理文件或使用安装程序。那个简单的批处理文件或安装程序必须在目标机器(你非开发计算机)上执行“regasm”。
并确保将项目强制为 x86(假设您使用的是 office x32,这很可能就是这种情况)。所以不要使用“任何”CPU,而是强制项目到 x86 CPU。
除此之外,仅此而已。
这是一个简单的 .net class。假设 .net 4.5 或更高版本,这允许您从 Access VBA.
创建 zip 文件(您需要 system.IO.Compression 和 system.io.compression.Filesystem 作为 .net 项目中的引用。结果是在没有任何第 3 方工具的情况下从 Access 压缩文件。
.net中的class是这样的:
Imports System.Runtime.InteropServices
Imports System.IO.Compression
Imports System.IO
<ClassInterface(ClassInterfaceType.AutoDual)>
Public Class AlbertCom1
Private m_Times2 As Integer
Public Sub MsgHello()
MsgBox("Hello world", MsgBoxStyle.Information, "VB.net example")
End Sub
Public Sub MyZipper(strFileName As String, strZipFile As String)
Using archive As ZipArchive = ZipFile.Open(strZipFile, ZipArchiveMode.Update)
archive.CreateEntryFromFile(strFileName, Path.GetFileName(strFileName), CompressionLevel.Fastest)
End Using
End Sub
Public Function GetConValue(strSetting As String) As String
' read a simple value from config file
Return My.Settings(strSetting).ToString
End Function
Public Property Times2 As Integer
Get
Return m_Times2
End Get
Set(value As Integer)
m_Times2 = value * 2
End Set
End Property
End Class
所以上面的内容很简单,也很简短。
在 Access VBA 中,在我们的按钮后面,我们可以使用这些代码:
Call/use 上面的 Hello 消息框方法:
Sub TestCOMHello()
Dim mycom As Object
Set mycom = CreateObject("AlbertCom1.AlbertCom1")
mycom.MsgHello
End Sub
注意上面的 LATE 绑定是如何工作的——这将在 VBA 中没有引用对象的情况下工作。 (因此需要 createObject())。
但是,以下示例使用了早期绑定,但都可以按照上述方式创建对象。
压缩来自 VBA 的文件:
Sub TestCOMZip()
Dim strFromFile As String
Dim strToFile As String
Dim mycom As New AlbertCom1.AlbertCom1
strFromFile = "c:\test\data.txt"
strToFile = "c:\test\data.zip"
mycom.MyZipper strFromFile, strToFile
End Sub
更多提示:
您实际上不必在 class 中构建自定义界面,虽然许多人对使用 AutoDual “不满意”,但我认为这种方法很好。互联网上花费所有时间创建自定义界面的“示例”实际上只是世界贫困的借口。
最后一个重要提示: 不要在 class 中公开任何不兼容的数据类型。如果您(无意或有意)暴露任何与标准“com”对象不兼容的 .net 对象类型,那么您的 .net class 将编译得很好,但 Access 将无法正确查看或使用该对象.因此,在上面的实际 class 中,将这些变量和例程保持(声明)为私有。 (其他代码和部分无关紧要 - 只有一个 class 就是我所说的)。
所以不要暴露 .net long 数据类型。如果你坚持使用字符串、标准集合、数组等,那么你应该没问题。您实际上“可以”将不兼容的对象类型公开为 .net 中的“对象”。 (Access 中的 Intel-sense 不会显示对象方法,但您仍然可以使用它们)。
虽然 public 函数甚至 public 变量对于您的对象方法来说是最简单的,这里是根据“标准”方法使用 get/set 如何class 方法已创建。
所以在 .net 中我们有:
Public Property Times2 As Integer
Get
Return m_Times2
End Get
Set(value As Integer)
m_Times2 = value * 2
End Set
End Property
在您的 VBA 代码中,我们有:
Sub TestTimes()
Dim mycom As New AlbertCom1.AlbertCom1
mycom.Times2 = 40
Debug.Print mycom.Times2
End Sub
output: 80
注意提前绑定,然后Access VBA编辑器会“吐出”class的属性和方法:
例如:
总的来说,使用 VSTO 是一个巨大的矫枉过正。只需构建一个简单的 .net class 来公开您需要从 VBA 到 use/call 的内容,然后您就可以参加比赛了。另一个大好处是这个 COM 对象可以从 windows 脚本、VB6、FoxPro、Excel、Word、power-point 等中使用。换句话说,你不限于任何一个平台或只是访问消费 + 使用您作为 COM 对象公开的简单 class。在大多数情况下,我很难为 VSTO 辩护。因此,现在可以从任何办公应用程序(VBA 代码)轻松使用此对象。