You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VBA解析OAuth HTML响应文本,获取theToken值求助

解决VBA提取HTML中指定input的value值问题

问题根源

你的代码里存在一个关键错误:Set tokn = html.getElementById("theToken").value 中,value是字符串类型,并非对象,不能用Set关键字赋值,直接赋值给字符串变量即可。

修正后的代码

Dim html As New HTMLDocument
Dim XMLHTTP_swlc As Object
Dim strurl As String
Dim ivari2 As Integer
Dim tokn As String ' 明确定义为字符串类型

Set XMLHTTP_swlc = CreateObject("MSXML2.XMLHTTP")

strurl = "https:***remoting/oauth-token?login=true"
With XMLHTTP_swlc
    .Open "POST", strurl, False
    .setRequestHeader "Content-Type", "application/json"
    .setRequestHeader "Accept", "application/json"
    .setRequestHeader "Authorization", "Bearer" & "rlZ....PT0"
    .send
    
    ivari2 = .status
    Debug.Print ivari2
    
    html.Body.innerHTML = XMLHTTP_swlc.responseText
    ' 移除Set关键字,直接赋值字符串
    tokn = html.getElementById("theToken").value
    Debug.Print tokn ' 打印结果验证
End With

额外检查项

  • 确保已引用Microsoft HTML Object Library:在VBA编辑器中点击「工具」→「引用」,勾选该库,可获得更完善的HTML解析支持。
  • 若getElementById返回Nothing,可尝试将响应文本赋值给整个文档元素而非Body:
    html.documentElement.innerHTML = XMLHTTP_swlc.responseText
    

内容的提问来源于stack exchange,提问作者ashwini

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 18:09:25