简体   繁体   English

创建函数-Excel VBA

[英]Creating a Function - Excel VBA

I have some code that makes a HTTP request and fetches an XML from an API before parsing. 我有一些代码可以发出HTTP请求,并在解析之前从API提取XML。

The HTTP request part of my code is always the same and the only parameter that changes is the URL I send the request to. 我的代码的HTTP请求部分始终是相同的,唯一更改的参数是我将请求发送到的URL。 I'm wondering if I can therefore convert the request to a Function? 我想知道是否可以将请求转换为功能吗?

Set xmldoc = CreateObject("Msxml.DOMDocument")
Set httpReq = CreateObject("WinHttp.WinHttprequest.5.1")
xmldoc.async = False

httpReq.Open "GET", myUrl, False

httpReq.setRequestHeader "Content-Type", "text/xml"
    If Sheet2.proxyStatus = "ON" Then
    httpReq.setProxy 2, Sheet2.proxyServer, ""
    ElseIf Sheet2.proxyStatus = "OFF" Then
    httpReq.setProxy 0, "", ""
    End If
httpReq.setTimeouts -1, -1, -1, -1
httpReq.send request

xmldoc.LoadXML httpReq.responseText

Set xmlElement = xmldoc.DocumentElement

After making the request I use some code like this to parse data from the XML: 发出请求后,我使用类似以下的代码来解析XML中的数据:

TotalSessions = xmlElement.SelectSingleNode("//Row[@rowKey='Sessions']/Value[@columnId='SESSIONS']").Text

The code that parses the information can't be included in the function because the nodes that it is looking for and the variables it is assigning are unique and numerous but it needs to be able to read the XML downloaded by the Function obviously. 解析信息的代码不能包含在函数中,因为它正在寻找的节点和它所分配的变量是唯一且众多的,但显然它必须能够读取Function下载的XML。

I have tried creating the Function but I am unsure what type to define it as, so I went with Object: 我曾尝试创建函数,但是不确定将其定义为哪种类型,所以我选择了对象:

Function fetchXML(url As String) As Object

Set xmldoc = CreateObject("Msxml.DOMDocument")
Set httpReq = CreateObject("WinHttp.WinHttprequest.5.1")
xmldoc.async = False

httpReq.Open "GET", url, False

httpReq.setRequestHeader "Content-Type", "text/xml"
    If Sheet2.proxyStatus = "ON" Then
    httpReq.setProxy 2, Sheet2.proxyServer, ""
    ElseIf Sheet2.proxyStatus = "OFF" Then
    httpReq.setProxy 0, "", ""
    End If
httpReq.setTimeouts -1, -1, -1, -1
httpReq.send request

xmldoc.LoadXML httpReq.responseText

Set xmlElement = xmldoc.DocumentElement

End Function

I have then tried to call the Function and Parse some information: 然后,我尝试调用函数并解析一些信息:

fetchXML (coukChannels30DayUrl)

coukPPCSessionsSS = xmlElement.SelectSingleNode("//Row[@rowKey='1#11588418521354:1158842490346']/Value[@columnId='SESSIONS']").Text

But unfortunately it didn't work, am I calling the function wrong? 但是不幸的是,它没有用,我把这个函数叫错了吗? Is it the wrong type? 是错误的类型吗? Can I even do this? 我什至可以这样做吗? lol 大声笑

Cheers 干杯

Okay I got it to work, I just needed to change this line in the function: 好的,我可以使用它,我只需要在函数中更改此行:

Set xmlElement = xmldoc.DocumentElement

To this: 对此:

Set fetchXML = xmldoc.DocumentElement

And then change this line in the sub: 然后在子目录中更改此行:

fetchXML (coukChannels30DayUrl)

To this: 对此:

Set xmlElement = fetchXML(coukChannels30DayUrl)

And it's working very nicely, yay for functions. 而且它的功能非常好,是的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM