首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >处理Excel VBA宏中的REST API处理

处理Excel VBA宏中的REST API处理
EN

Stack Overflow用户
提问于 2014-11-01 00:01:20
回答 1查看 4.5K关注 0票数 2

下面的VBA代码应该调用REST API来授予用户对文件的访问权限。如果REST API返回允许,则代码应正常关闭并授予访问权限;如果REST API返回不允许,则程序应通知用户并关闭工作簿。如果无法访问Internet,则应通知用户并关闭工作簿。

我的问题是,我如何编写代码,以便宏正确地处理REST API响应,以便它将正常结束或由于来自url的不允许响应而关闭?

以下是目前为止的VBA代码:

代码语言:javascript
复制
Private Sub Workbook_activate()

Application.EnableCancelKey = xlDisabled

' Run the Error handler "ErrHandler" when an error occurs.

On Error GoTo Errhandler


ActiveWorkbook.FollowHyperlink Address:="https://mysite.com/licensing/getstatus.php?", NewWindow:=True

' If response is allow

' Exit the macro so that the error handler is not executed.

****what goes here??****

      Exit Sub

' If response is disallow

****what goes here??****

      MsgBox "Your license key is not valid. Please check your key or contact customer service."

       ActiveWorkbook.Close SaveChanges:=False


Errhandler:

      ' If no Internet Access, display a message and end the macro.
      MsgBox "An error has occurred. You need Internet access to open the software."

       ActiveWorkbook.Close SaveChanges:=False

End Sub
EN

回答 1

Stack Overflow用户

发布于 2014-11-01 03:00:44

下面是一个简单HttpWebRequest的示例:

代码语言:javascript
复制
Dim oRequest As Object
Set oRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
oRequest.Open "GET", "https://mysite.com/licensing/getstatus.php?"
oRequest.Send
MsgBox oRequest.ResponseText

如果你在代理后面,你可以使用类似这样的东西:

代码语言:javascript
复制
Const HTTPREQUEST_PROXYSETTING_PROXY = 2
Dim oRequest As Object
Set oRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
oRequest.setProxy HTTPREQUEST_PROXYSETTING_PROXY, "http://proxy.intern:8080"
oRequest.Open "GET", "https://mysite.com/licensing/getstatus.php?"
oRequest.Send
MsgBox oRequest.ResponseText

如果您想使用POST (而不是GET方法)将一些值传递给GET服务器,您可以尝试这样做:

代码语言:javascript
复制
Dim oRequest As Object
Set oRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
oRequest.Open "POST", "https://mysite.com/licensing/getstatus.php"
oRequest.SetRequestHeader "Content-Typ", "application/x-www-form-urlencoded"
oRequest.Send "var1=123&anothervar=test"
MsgBox oRequest.ResponseText
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26678817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档