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
相关产品推荐
相关产品推荐

