在 Excel 2010 中使用 VBA 代码调用 Web 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3521876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling web service using VBA code in Excel 2010
提问by Aadith Ramia
I am trying to write some VBA code in Excel 2010 that would consume a web service. I am unable to find any related resources on the internet. Can someone please tell me how to do this.
我正在尝试在 Excel 2010 中编写一些使用 Web 服务的 VBA 代码。我无法在互联网上找到任何相关资源。有人可以告诉我如何做到这一点。
采纳答案by Juliusz
I don't think the post marked as the answer is right - it links to the Excel 2003 solution, which was based on the MS Office Web Services Toolkit. That solution is no longer valid as the Toolkit is not supported any more. See: MSDN - Consuming Web Services in Excel 2007. The link provides a solution for Excel 2007 which can be transposed to Excel 2010. You will need Visual Studio 2010 with Microsoft Office Developers Tools though. It also means utilising .Net Framework and coding in C# or VB.NET.
我不认为标记为答案的帖子是正确的 - 它链接到 Excel 2003 解决方案,该解决方案基于 MS Office Web Services Toolkit。该解决方案不再有效,因为不再支持 Toolkit。请参阅:MSDN - 在 Excel 2007 中使用 Web 服务。该链接提供了 Excel 2007 的解决方案,该解决方案可以转换为 Excel 2010。不过,您将需要带有 Microsoft Office 开发人员工具的 Visual Studio 2010。它还意味着使用 .Net Framework 并使用 C# 或 VB.NET 进行编码。
回答by Tim Hall
I struggled with the same question on a recent project of mine where I wanted to talk to Trello and Salesforce in pure VBA without installing any plugins, opening up Visual Studio, or other hacks. Ended up writing my own library (based heavily on one of my favorites, RestSharp).
我最近在我的一个项目中遇到了同样的问题,我想在纯 VBA 中与 Trello 和 Salesforce 交谈,而无需安装任何插件、打开 Visual Studio 或其他黑客。最终编写了我自己的库(主要基于我的最爱之一 RestSharp)。
Warning, shameless plug: https://github.com/VBA-tools/VBA-Web
警告,不要脸的插件:https: //github.com/VBA-tools/VBA-Web
Some fun features include Mac support(!), authentication (Http Basic, OAuth1, OAuth2, and others), Async support, and JSON parsing (thanks to VBA-JSON)
一些有趣的功能包括 Mac 支持(!)、身份验证(Http Basic、OAuth1、OAuth2 等)、异步支持和 JSON 解析(感谢VBA-JSON)
It's working awesomely in Excel 2010 and 2013 (and most likely 2007) and I've got it working with Salesforce, Trello, Basecamp, Google Maps, and it should work with pretty much any webservice.
它在 Excel 2010 和 2013(最有可能是 2007 年)中运行得非常好,我已经将它与 Salesforce、Trello、Basecamp、Google Maps 一起使用,并且它几乎可以与任何网络服务一起使用。
回答by SAL
FWIW as of the time of posting I've found that the old Web References Toolkit of Excel 2003 still produces working VBA code in Excel 2007/10 (note that there have been some shaky moments with various Windows updates over the last 18 months that has stopped this code working so I wouldn't consider my solution 'reliable').
FWIW 截至发帖时,我发现 Excel 2003 的旧 Web References Toolkit 仍在 Excel 2007/10 中生成有效的 VBA 代码(请注意,在过去的 18 个月中,各种 Windows 更新出现了一些不稳定的时刻)停止此代码工作,所以我不会认为我的解决方案“可靠”)。
My horrible hack involves installing Excel 2003, then the Web References Toolkit, then installing Excel 2007 or Excel 2010. I've set up 3 PCs using this hack and all are producing working VBA code to consume asmx web services (I haven't tried connecting with other types of web service but I don't see why they wouldn't work).
我可怕的 hack 涉及安装 Excel 2003,然后是 Web References Toolkit,然后安装 Excel 2007 或 Excel 2010。我已经使用这个 hack 设置了 3 台 PC,并且所有 PC 都在生成可用的 VBA 代码来使用 asmx Web 服务(我还没有尝试过与其他类型的网络服务连接,但我不明白为什么它们不起作用)。
The official MS method, using VSTO, is a step too far for some of our clients and this VBA hack has kept them happy.
使用 VSTO 的官方 MS 方法对于我们的一些客户来说太过分了,而这个 VBA hack 让他们感到高兴。
回答by Patrick Honorez
A few years later...I found this pagewhich is the best and clearest explanation and example I have found so far, including an interesting link providing services to play with.
几年后......我发现这个页面是迄今为止我发现的最好和最清晰的解释和示例,包括一个提供服务的有趣链接。
回答by Muhammad Yaseen
Windows Communication Foundation (WCF) service moniker to integrate Web services into COM-based development environments, such as Microsoft Office Visual Basic for Applications (Office VBA) or Visual Basic 6.0. For detail see link https://docs.microsoft.com/en-us/dotnet/framework/wcf/samples/using-the-wcf-moniker-with-com-clients
Windows Communication Foundation (WCF) 服务名称将 Web 服务集成到基于 COM 的开发环境中,例如 Microsoft Office Visual Basic for Applications (Office VBA) 或 Visual Basic 6.0。有关详细信息,请参阅链接https://docs.microsoft.com/en-us/dotnet/framework/wcf/samples/using-the-wcf-moniker-with-com-clients

