小脚本编译错误

时间:2013-05-02 14:58:27

标签: excel vba excel-vba excel-2007

我正在尝试在Excel 2007中编写一个cls,这是代码:

Dim JiraService As MSXML2.XMLHTTP60
Set JiraService = New MSXML2.XMLHTTP60

JiraService.Open "GET", "URL", False
JiraService.SetRequestHeader "Content-Type", "application/json"
JiraService.SetRequestHeader "Accept", "application/json"
JiraService.Send

sRestResponse = JiraService.ResponseText

MsgBox sRestResponse

我收到编译错误,表示Set似乎无效。

enter image description here

它基本上表示外部程序无效。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:4)

我猜你错过了相应的参考资料。单击工具~~> VBA中的参考

添加项目引用;

  • Microsoft WinHTTP服务,版本5.1
  • Microsoft XML,v6.0

也在这一行

JiraService.Open "GET", "URL", False

如果URL是保存实际网址的变量,则"URL"将被视为字符串,而不是变量,因为它位于引号之间。我想你是在尝试这个?

JiraService.Open "GET", URL, False

关注(来自评论)

问题是您已将上述代码粘贴到类模块中。您可以将代码粘贴到模块中并在子模块中调用它。见这个例子

Sub Sample()
    Dim JiraService As MSXML2.XMLHTTP60
    Set JiraService = New MSXML2.XMLHTTP60

    JiraService.Open "GET", URL, False
    JiraService.SetRequestHeader "Content-Type", "application/json"
    JiraService.SetRequestHeader "Accept", "application/json"
    JiraService.Send

    sRestResponse = JiraService.ResponseText

    MsgBox sRestResponse
End Sub

<强>截图

enter image description here