0

大家下午好

我正在开展一个工作项目,我需要根据注册号和里程获取车辆值,并将这些值输入 Excel 电子表格。

注册号和里程存储在电子表格中,但我不知道从哪里开始。

我上周末创建了一个粗略的 VBA 应用程序,如下所示

注册号和里程存储在电子表格中,但我不知道从哪里开始。

上周末我创建了一个粗略的 VBA 应用程序,如下所示:

子 GetHTMLDocument()

Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim Email As MSHTML.IHTMLElement
Dim Password As MSHTML.IHTMLElement
Dim LoginButton As MSHTML.IHTMLElement
Dim REG As MSHTML.IHTMLElement
Dim Mileage As MSHTML.IHTMLElement
Dim CAPGo As MSHTML.IHTMLElement
Dim objEvent
Dim GetValue As MSHTML.IHTMLElement

'Show IE for testing purposes
IE.Visible = True
'Navigate to web page
IE.Navigate "https://valuationanywhere.cap.co.uk/LoginPage?ReturnUrl=%2f%3f__hstc%3d208265677.8bb2d3e6c872f15cd37070c17648ee29.1549763639794.1549763639794.1549763639794.1%26__hssc%3d208265677.1.1549763639794%26__hsfp%3d959865525&__hstc=208265677.8bb2d3e6c872f15cd37070c17648ee29.1549763639794.1549763639794.1549763639794.1&__hssc=208265677.1.1549763639794&__hsfp=959865525"

'Loop an empty loop until done
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop

Set HTMLDoc = IE.Document

'inputs email address
Set Email = HTMLDoc.getElementById("inputLoginEmail")
Email.Value = "email"
'inputs password
Set Password = HTMLDoc.getElementById("inputLoginPassword")
Password.Value = "password"
'Clicks login button
Set LoginButton = HTMLDoc.getElementById("btnLogin")
LoginButton.Click

'Wait 3 seconds for page to load
Application.Wait (Now + TimeValue("0:00:03"))

Set objEvent = IE.Document.createEvent("HTMLEvents")

'Input REG into text box
Set REG = HTMLDoc.getElementById("vrm")
REG.Value = "reg"
'Input mileage into text box
Set Mileage = HTMLDoc.getElementById("mileage")
Mileage.Value = "181000"

'Fakes data entry as no focus is given to the text box
objEvent.initEvent "change", False, True
REG.dispatchEvent objEvent
Mileage.dispatchEvent objEvent

'Clicks Go button
Set tags = IE.Document.getElementsByTagName("button")
For Each tagx In tags
If tagx.innerText = "Go" Then
    tagx.Click
    Exit For
End If
Next

'Wait 3 seconds for popup to load
Application.Wait (Now + TimeValue("0:00:03"))

Set tags = IE.Document.getElementsByTagName("button")
For Each tagx In tags
If tagx.innerText = "Create NEW Valuation" Then
    tagx.Click
    Exit For
End If
Next

这将导航到页面,让我登录并搜索估值。然而,我们最终将拥有一个包含数百辆汽车的数据库,我们希望获得估值,我们的 CAP 服务在这里有一些插件 - https://soap.cap.co.uk/vrm/capvrm.asmx?op=VRMValuation

有什么办法可以让 VBA 从一张表中选择一个 reg 和里程,然后拉回价值?

我不指望有人会写出我想从中学习的全部内容。但是谁能指出我正确的方向?

最诚挚的问候, 克雷格

4

1 回答 1

1

本质上,您可以从 Excel 中读取 2 列范围,其中包含 A 列 reg 和 B 列里程,进入二维数组,然后将数组的维度 1 从 lbound 循环到 ubound (即行)并通过以下方式访问 reg 和里程索引到数组中。然后,您可以将这些值连接到 POST 请求的正文中。这是可以理解的非常高的水平,如下所示。您可以将响应读入 xml 文档,以便解析出所需的信息。

在检索值方面,我们需要查看相关的 XML。

Option Explicit

Public Sub Test()
    'VBE > Tools > References > Add a reference to Microsoft HTML Object Library
    'other code

    Dim regAndMileage(), xmlDoc As Object
    Dim ws As Worksheet, r As Long, placeholderMileage As String, placeholderVR As String, body As String, response As String, html As HTMLDocument
    Const SUBSCRIBER_ID As Long = 123
    Const PASSWORD As String = "ABC"
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    Set html = New HTMLDocument
    Set xmlDoc = CreateObject("MSXML2.DOMDocument")
    regAndMileage = ws.Range("A2:B4").Value      'Create the array. Reg is in col A and mileage in col B. Check datatypes when passed are as expected (int - though Long should work; and string)

    body = "<?xml version=""1.0"" encoding=""utf-8""?>"
    body = body & Chr$(10) & "<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">"
    body = body & Chr$(10) & "<soap:Body>"
    body = body & Chr$(10) & "<VRMValuation xmlns=""https://soap.cap.co.uk/vrm"">"
    body = body & Chr$(10) & "<SubscriberID>" & SUBSCRIBER_ID & " </SubscriberID>" 'int
    body = body & Chr$(10) & "<Password>" & PASSWORD & "</Password>" 'string
    body = body & Chr$(10) & "<VRM>placeholderVRM</VRM>" 'string
    body = body & Chr$(10) & "<Mileage>placeholderMileage</Mileage>" 'Mileage
    body = body & Chr$(10) & "<StandardEquipmentRequired>boolean</StandardEquipmentRequired>"
    body = body & Chr$(10) & "</VRMValuation>"
    body = body & Chr$(10) & "</soap:Body>"
    body = body & Chr$(10) & "</soap:Envelope>"

    With CreateObject("MSXML2.XMLHTTP")
        For r = LBound(regAndMileage, 1) To UBound(regAndMileage, 1)
            mileage = regAndMileage(r, 1)
            reg = regAndMileage(r, 2)
               'create your body here and concatentate in your mileage and reg variables
            .Open "POST", "protocol&domain/vrm/capvrm.asmx/VRMValuation", False
            .setRequestHeader "SOAPAction", "https://soap.cap.co.uk/vrm/VRMValuation"
            .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
            .send Replace$(Replace$(body, placeholderVRM, reg), placeholderMileage, mileage)
            response = .responseText
            With xmlDoc
                .validateOnParse = True
                .setProperty "SelectionLanguage", "XPath"
                .async = False
                If Not .LoadXML(sResponse) Then
                    Err.Raise .parseError.ErrorCode, , .parseError.reason
                End If
            End With
            'Do something to extract values
        Next
    End With
End Sub

有关范围和数组的更多信息,请参阅此内容。

您可能需要在请求中添加内容长度和其他信息。

SOAP 请求

于 2019-02-14T20:36:51.593 回答