如何在Excel中使用VBA进行REST调用

如何在Excel中使用VBA进行REST调用

本文介绍了如何在Excel中使用VBA进行REST调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按照这个例子:



但是它出现了这个错误:





此行:

 打印objHTTP.Status 

那么如何在VBA中进行POST REST调用?如何使用PUT多部分/表单数据文件在VBA中上传REST调用?



工具>参考文献



代码 $ b
$ b

  Sub SendEmail()
'Dim objHTTP As New MSXML2.XMLHTTP
'Set objhttp = CreateObject(WinHttp.WinHttpRequest.5.1 )
设置objHTTP = CreateObject(MSXML2.ServerXMLHTTP)
URL =http:// localhost:8888 / rest / mail / send
objHTTP.OpenPOST,URL ,False
objHTTP.send({key:null,[email protected]至:null,cc null,bcc:null,date:null,subject:我的主题body:null,附件 b $ b打印objHTTP.Status
打印objHTTP.ResponseText

End Sub



参考



WinHttpRequest对象:

解决方案

您可能尚未添加对 Microsoft XML (任何版本)的引用 Dim objHTTP As New MSXML2.XMLHTTP 在VBA窗口的工具/引用...对话框中。



此外,避免使用后期绑定( CreateObject ...);更好地使用早期绑定( Dim objHTTP As New MSXML2.XMLHTTP ),因为早期绑定允许您使用Intellisense列出成员并进行各种设计时验证。


I tried to follow this example: http://libkod.info/officexml-CHP-9-SECT-5.shtml - Archive.org - Donate

but it gave this error

on this line:

I tried to use this example: How can I send an HTTP POST request to a server from Excel using VBA?

but it gave this error:

on this line:

Print objHTTP.Status

So how do I make a POST REST call in VBA? How do I make a PUT multi-part/form-data file upload REST call in VBA?

Tools > References

Code

Sub SendEmail()
    'Dim objHTTP As New MSXML2.XMLHTTP
    'Set objhttp = CreateObject("WinHttp.WinHttpRequest.5.1")
    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
    URL = "http://localhost:8888/rest/mail/send"
    objHTTP.Open "POST", URL, False
    objHTTP.send ("{""key"":null,""from"":""[email protected]"",""to"":null,""cc"":null,""bcc"":null,""date"":null,""subject"":""My Subject"",""body"":null,""attachments"":null}")
    Print objHTTP.Status
    Print objHTTP.ResponseText

End Sub

Reference

WinHttpRequest object: http://msdn.microsoft.com/en-us/library/windows/desktop/aa384106(v=vs.85).aspx

解决方案

You probably haven't added a reference to Microsoft XML (any version) for Dim objHTTP As New MSXML2.XMLHTTP in the VBA window's Tools/References... dialog.

Also, it's a good idea to avoid using late binding (CreateObject...); better to use early binding (Dim objHTTP As New MSXML2.XMLHTTP), as early binding allows you to use Intellisense to list the members and do all sorts of design-time validation.

这篇关于如何在Excel中使用VBA进行REST调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:24