VBA中如何使用Edge内核Browser?

问题:

在微软宣布停止对IE的支持后,许多使用VBA中Web Browser的用户遇到了困境,因为Web Browser使用的IE内核。然而微软并没有给出支持Edge的新版Web Browser,网上许多第三方的库也不够完善。

解决方案:

经过调研,libEdge可以较好的和VBA集成,实现在Excel中打开Browser,甚至能够通过执行js代码,获取web页面的信息。

https://github.com/bubdm/libEdge/tree/main/VBA_libEdge

注意:

项目需要WebView2Loader.dll和LibEdge.dll两个库,区分x86和x64版本。

示例代码:

vba 复制代码
Public Function GetEncodedCookieFromWebBrowser(serverIP As String) As String
    Dim url As String
    Dim encodedCookie As String
    Dim isValidIPFormat As Boolean
    Dim jsResult As String
    Dim path As String

    encodedCookie = ""
    url = "https://" + serverIP + "/proj/demo/" + Guid()
    isValidIPFormat = IsValidIP(serverIP)
    path = Application.ActiveWorkbook.path & "\"
    ChDir path

    If isValidIPFormat = False Then
        If VBA_StartEdge() <> 0 Then Exit Function

        VBA_Navigate url

        ' Wait for the page to fully load
        Do While Not PageLoaded
            Sleep 500 ' Sleep for 500 milliseconds to allow user interaction
            DoEvents
        Loop

        ' Wait until the cookie contains 'token%22'
        Do Until InStr(jsResult, "token%22") > 0
            If VBA_RunJavascript("document.cookie", jsResult) <> 0 Then
                VBA_StopEdge True
                Exit Function
            End If
            Sleep 500 ' Sleep for 500 milliseconds to allow user interaction
            DoEvents
        Loop
        encodedCookie = jsResult
        VBA_StopEdge True
    End If
    GetEncodedCookieFromWebBrowser = encodedCookie
End Function
相关推荐
Learn-Share_HY10 天前
[Excel VBA]如何製作買三送一優惠條件的POS結帳介面?
excel·vba·office·自動化·自動化辦公
shandianchengzi12 天前
【工具】Quicker/VBA|PPT 在指定位置添加有颜色的参考线
powerpoint·vba·ppt·quicker
Access开发易登软件17 天前
Access链接Azure SQL
数据库·后端·sql·flask·vba·azure·access
专注VB编程开发20年17 天前
在 VB6 中强制设置 Word 文档的纸张尺寸
ui·c#·word·vba·vb6
通义灵码18 天前
在 Excel 中使用通义灵码辅助开发 VBA 程序
人工智能·阿里云·excel·vba·通义灵码
課代表20 天前
Office 中 VBE 的共同特点与区别
word·excel·vba·office·vbe
莫负初1 个月前
Excel使用VBA批量计算指定列的中位数和标准差并筛选指定列数据
数据分析·自动化·excel·vba·方差·标准差
課代表1 个月前
Excel VBA 词频统计宏
ui·excel··vba·模块·字典
专注VB编程开发20年1 个月前
Power Query 是 Excel 和 Power BI 中强大的数据获取、转换和加载工具
数据库·excel·vba·csv·导入数据
課代表1 个月前
Excel VBA 自定义函数
excel·vba·自定义函数