Excel宏相同IE Navigate代码在不同工作簿执行结果不一致
两个Excel工作簿的宏中,均使用Internet Explorer的Navigate方法传入字符串变量作为URL,代码逻辑一致,仅URL的拼接方式不同:
- 工作簿A的URL通过多次调用与响应拼接生成
- 工作簿B的URL通过单元格预存的内容拼接生成,最终字符串经监视、复制对比确认与工作簿A完全一致
异常情况:工作簿B执行IE.Navigate dataurl时,无法完整传递URL,会丢失末尾的Query部分(如?type=ST),但dataurl变量本身确实包含完整字符串。
特殊的是,如果直接写死完整URL(示例:IE.Navigate "https://www.marketwatch.com/investing/stock/aapl?Mod=search"),则可正常导航到完整地址。
使用的VB代码如下:
Function calling(dataurl As String) As String Dim IE As InternetExplorer Dim htmldoc As New HTMLDocument Set IE = CreateObject("InternetExplorer.Application") Application.StatusBar = "Setting up..." IE.Visible = False IE.navigate dataurl Application.StatusBar = "Calling server..." Application.Wait (Now + TimeValue("00:00:03")) Do Until IE.readyState = 4 IE.Refresh2 (3) Application.Wait (Now + TimeValue("00:00:05")) Loop calling = IE.document.body.innerHTML IE.Quit Set IE = Nothing Application.StatusBar = "" End Function
已尝试过其他浏览器,最终仍选择使用Internet Explorer。
内容的提问来源于stack exchange,提问作者Javamaral
相关产品推荐
相关产品推荐

