将VBA类置于行动中

时间:2014-05-28 07:11:51

标签: excel vba excel-vba

我的问题与问题有关:JSON import to Excel

我理解这篇文章的大部分内容,但我不明白如何将它放在一个excel模块或类模块中。有人请帮助我理解如何组装这只野兽来实现我的目的吗?

编辑29-5-14 @ 7.44AM -

这是我到目前为止所做的尝试:

安装为ClassModule syncWebRequest

'BEGIN CLASS syncWebRequest

Private Const REQUEST_COMPLETE = 4

Private m_xmlhttp As Object
Private m_response As String

Private Sub Class_Initialize()
    Set m_xmlhttp = CreateObject("Microsoft.XMLHTTP")
End Sub

Private Sub Class_Terminate()
    Set m_xmlhttp = Nothing
End Sub


Property Get Response() As String
    Response = m_response
End Property

Property Get Status() As Long
    Status = m_xmlhttp.Status
End Property

Public Sub AjaxPost(Url As String, Optional postData As String = "")
    m_xmlhttp.Open "POST", Url, False
    m_xmlhttp.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
    m_xmlhttp.setRequestHeader "Content-length", Len(postData)
    m_xmlhttp.setRequestHeader "Connection", "close"
    m_xmlhttp.send (postData)
    If m_xmlhttp.readyState = REQUEST_COMPLETE Then
        m_response = m_xmlhttp.responseText
    End If
End Sub

Public Sub AjaxGet(Url As String)
    m_xmlhttp.Open "GET", Url, False
    m_xmlhttp.setRequestHeader "Connection", "close"
    m_xmlhttp.send
    If m_xmlhttp.readyState = REQUEST_COMPLETE Then
        m_response = m_xmlhttp.responseText
    End If
End Sub

'END CLASS syncWebRequest

安装为模块测试

Sub Test()
    Dim request As New syncWebRequest
    request.AjaxGet "http://crimson/php/SiteEnquiryAjax.php?action=aisFeed&brandCode=s&siteID=0625?format=json"
    Dim json As String
    json = request.Response
End Sub

我习惯使用普通模块,但之前不必使用类模块。因此,我正在努力理解这一点,并将其翻译成符合我的要求,但请原谅我,如果我把它变成一个比预期更重要的工作。

我正在尝试从返回JSON的本地Web服务查询数据。 web服务基于ID(例如0656)以以下形式返回数据:

{"total":"1","results":[{"brandName":"ABC","brandingName":"Cat","siteCategory":"Production","siteName":"Scrubbed","streetAddress":"ABC RD, SUBURB, VIC 3000","siteState":"VIC","phoneNumber":"03 0909 0909","mobileNumber":"0409 090 909","faxNumber":"03 9090 9090","applicationServer":"CAT0656ABC001","dateOpened":"1979-08-21","siteNotice":"","lastContact":"2014-05-19 04:36:31",}]}

我的目标是将其放入电子表格中,用户可以在其中输入网站ID(例如0656),然后电子表格就可以提取并回复上述示例中的数据。

编辑29-5-14 @ 8.42AM -

更新模块测试 - 我的目的是测试链接问题中的解析器示例,但是当我现在运行宏时,我收到错误。我的理解是,这可能是我想要实现的缺失部分。我现在已经注释了添加的项目以阻止错误发生。

Sub Test()
    Dim request As New syncWebRequest
    request.AjaxGet "http://crimson/php/SiteEnquiryAjax.php?action=aisFeed&brandCode=s&siteID=0625"
    Dim json As String
    json = request.Response

    'Set clients = parser.Parse(request.Response)
    'For Each client In clients
    'Name = client("Name")
    'State = client("siteState")
    'street = client("Address")("Street")
    'suburb = client("Address")("Suburb")
    'city = client("Address")("City")
    'Next
End Sub

编辑29-5-14 @ 9.40AM -

换句话说......我想转此: HTTP结果“url / SiteEnquiryAjax.php?action = aisFeed& brandCode = s& siteID = 0656”

{"total":"1","results":[{"brandName":"ABC","brandingName":"Cat","siteCategory":"Production","siteName":"Scrubbed","streetAddress":"ABC RD, SUBURB, VIC 3000","siteState":"VIC","phoneNumber":"03 0909 0909","mobileNumber":"0409 090 909","faxNumber":"03 9090 9090","applicationServer":"CAT0656ABC001","dateOpened":"1979-08-21","siteNotice":"","lastContact":"2014-05-19 04:36:31",}]}

进入: Excel输出

Excel Output

1 个答案:

答案 0 :(得分:0)

在不知道您收到的错误的具体细节的情况下,我之前使用过vba-json,并且我认为您在使用之前没有实例化解析器:

Dim parser As New JSONLib
Set clients = parser.parse(Request.Response)

就项目的设计而言,我认为你已经把Request作为一个类分开,然后从一个模块发出请求。我遇到了许多类似的问题,从Excel访问Salesforce并创建了一个我认为可能有帮助的库:Excel-REST。它遵循类似的想法,但将其分为三类:客户端,请求和响应。这是一个例子:

Sub RetrieveJSONSimple
  Dim Client As New RestClient
  Dim Response As RestResponse
  Set Response = Client.GetJSON("url...")

  If Response.Status = Ok Then
    ' Response.Data is parsed json
    For Each Client in Response.Data
      Name = Client("Name")
      State = Client("siteState")
      Street = Client("Address")("Street")
      Suburb = Client("Address")("Suburb")
      City = Client("Address")("City")
    Next Client
  End If
End Sub

Sub RetrieveJSONAdvanced
  Dim Client As New RestClient
  Client.BaseUrl = "http://crimson/php/"
  ' Can also setup authentication with HTTP Basic, OAuth, and others

  Dim Request As New RestRequest
  Request.Resource = "SiteEnquiryAjax.php?action=aisFeed&brandCode=s&siteID={SiteId}"
  Request.AddUrlSegment "SiteId", "0625"

  ' GET and json are default, but can be set
  Request.Method = httpGET
  Request.Format = json

  Dim Response As RestResponse
  Set Response = Client.Execute(Request)

  ' Process as before
End Sub