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
相关推荐
SteveJrong5 天前
Excel 笔记
excel·脚本·vba·数据
感谢地心引力1 个月前
【excel】VBA简介(Visual Basic for Applications)
excel·vba·office
Access开发易登软件1 个月前
【vba源码】自动获取汇率
excel·接口·api·vba·access·汇率
东京老树根1 个月前
Excel 技巧02 - 如何批量输入百分号 (★),如何输入百分号并指定小数位数,如何批量删除百分号,如何批量删除小数最后的0?
笔记·学习·excel·vba
reasonsummer1 个月前
【办公类-88-02】20250106批量读后感
python·vba
yivifu2 个月前
在VBA中结合正则表达式和查找功能给文档添加交叉连接
正则表达式·word·vba·交叉链接
斐夷所非2 个月前
VBA API 概述 / 声明 / 宏编程
vba
lc寒曦3 个月前
【VBA实战】使用Word制作简易的考试及阅卷系统
word·vba·考试系统
Access开发易登软件3 个月前
【vba源码】导入excel批注信息
数据库·excel·vba·access
ruleslol3 个月前
VBA10-处理Excel的动态数据区域
excel·vba