【问题标题】:Open an html page in default browser with VBA?使用 VBA 在默认浏览器中打开一个 html 页面?
【发布时间】:2011-03-11 02:59:19
【问题描述】:

如何使用 VBA 在默认浏览器中打开 HTML 页面?我知道是这样的:

Shell "http://myHtmlPage.com"

但我想我必须参考打开页面的程序。

【问题讨论】:

    标签: vba browser navigation


    【解决方案1】:

    您可以使用 Windows API 函数 ShellExecute 来执行此操作:

    Option Explicit
    
    Private Declare Function ShellExecute _
      Lib "shell32.dll" Alias "ShellExecuteA" ( _
      ByVal hWnd As Long, _
      ByVal Operation As String, _
      ByVal Filename As String, _
      Optional ByVal Parameters As String, _
      Optional ByVal Directory As String, _
      Optional ByVal WindowStyle As Long = vbMinimizedFocus _
      ) As Long
    
    Public Sub OpenUrl()
    
        Dim lSuccess As Long
        lSuccess = ShellExecute(0, "Open", "www.google.com")
    
    End Sub
    

    如评论中所述,要使其在 64 位中工作,您需要在 Private Declare Line 中添加 PtrSafe,如下所示:

    Private Declare PtrSafe Function ShellExecute _
    

    关于安全性的简短说明:如果 URL 来自用户输入,请确保严格验证输入,因为 ShellExecute 将执行具有用户权限的任何命令,如果用户是管理员。

    【讨论】:

    • 请注意,给将来可能会使用它的任何人:您必须将 ShellExecute 函数放在页面顶部的声明部分中。
    • 有些可能需要在声明语句中添加“PtrSafe”:“Private Declare PtrSafe Function ShellExecute...”,使其在64位下工作。
    • @ashleedawg:请注意ThisWorkbook 仅适用于 Excel
    • @Dirk - 正确。例如,您可以在 Access 或 Project 中发送 application.followhyperlink,在 Word 中发送 activedocument.followhyperlink,在 PowerPoint 中发送 activepresentation.folllowhyperlink 等等……不要误会,您的方法没有问题;对于不同的情况,两者都是不错的选择。我更有可能将FollowHyperlink 用于一次性或需要更多控制post/get/etc 的情况,ShellExecute 用于同时打开多组页面。
    • 我在 Mac 中尝试了上述方法,但没有成功。我收到“找不到文件”错误。我猜它是针对 shell32.dll 的。知道如何让它在 Mac 和 Windows 上工作吗?谢谢。
    【解决方案2】:

    你甚至可以说:

    FollowHyperlink "www.google.com"
    

    如果您遇到自动化错误,请使用 http://:

    ThisWorkbook.FollowHyperlink("http://www.google.com")
    

    【讨论】:

    • 如果在 Excel 中,您需要一个工作簿对象,例如 ThisWorkbook.FollowHyperlink "www.google.com"
    • 我收到自动化错误。所以我需要使用http://。然后,完整的命令是:ThisWorkbook.FollowHyperlink "http://www.google.com.br"
    • 如果您正在访问受信任的网页,这是最简单的方法,但是如果您想在网络浏览器中打开 powerpoint 或 pdf 文档,则必须使用 Dirk Vollmar 的 ShellExecute 以避免每次出现错误消息运行它。
    • FollowHyperlink 扰乱了 IE/Edge 安全性,据我体验,使用 ShellExecute 的解决方案更可靠
    • FollowHyperlink 的一个问题是它试图解释 url。如果 Url 返回 404 VBA 代码失败。
    【解决方案3】:

    如果您想要一个更强大的 ShellExecute 解决方案,该解决方案将使用默认的操作系统相关程序打开任何文件、文件夹或 URL,这里是一个取自 http://access.mvps.org/access/api/api0018.htm 的函数:

    '************ Code Start **********
    ' This code was originally written by Dev Ashish.
    ' It is not to be altered or distributed,
    ' except as part of an application.
    ' You are free to use it in any application,
    ' provided the copyright notice is left unchanged.
    '
    ' Code Courtesy of
    ' Dev Ashish
    '
    Private Declare Function apiShellExecute Lib "shell32.dll" _
        Alias "ShellExecuteA" _
        (ByVal hwnd As Long, _
        ByVal lpOperation As String, _
        ByVal lpFile As String, _
        ByVal lpParameters As String, _
        ByVal lpDirectory As String, _
        ByVal nShowCmd As Long) _
        As Long
    
    '***App Window Constants***
    Public Const WIN_NORMAL = 1         'Open Normal
    Public Const WIN_MAX = 3            'Open Maximized
    Public Const WIN_MIN = 2            'Open Minimized
    
    '***Error Codes***
    Private Const ERROR_SUCCESS = 32&
    Private Const ERROR_NO_ASSOC = 31&
    Private Const ERROR_OUT_OF_MEM = 0&
    Private Const ERROR_FILE_NOT_FOUND = 2&
    Private Const ERROR_PATH_NOT_FOUND = 3&
    Private Const ERROR_BAD_FORMAT = 11&
    
    '***************Usage Examples***********************
    'Open a folder:     ?fHandleFile("C:\TEMP\",WIN_NORMAL)
    'Call Email app:    ?fHandleFile("mailto:dash10@hotmail.com",WIN_NORMAL)
    'Open URL:          ?fHandleFile("http://home.att.net/~dashish", WIN_NORMAL)
    'Handle Unknown extensions (call Open With Dialog):
    '                   ?fHandleFile("C:\TEMP\TestThis",Win_Normal)
    'Start Access instance:
    '                   ?fHandleFile("I:\mdbs\CodeNStuff.mdb", Win_NORMAL)
    '****************************************************
    
    Function fHandleFile(stFile As String, lShowHow As Long)
    Dim lRet As Long, varTaskID As Variant
    Dim stRet As String
        'First try ShellExecute
        lRet = apiShellExecute(hWndAccessApp, vbNullString, _
                stFile, vbNullString, vbNullString, lShowHow)
    
        If lRet > ERROR_SUCCESS Then
            stRet = vbNullString
            lRet = -1
        Else
            Select Case lRet
                Case ERROR_NO_ASSOC:
                    'Try the OpenWith dialog
                    varTaskID = Shell("rundll32.exe shell32.dll,OpenAs_RunDLL " _
                            & stFile, WIN_NORMAL)
                    lRet = (varTaskID <> 0)
                Case ERROR_OUT_OF_MEM:
                    stRet = "Error: Out of Memory/Resources. Couldn't Execute!"
                Case ERROR_FILE_NOT_FOUND:
                    stRet = "Error: File not found.  Couldn't Execute!"
                Case ERROR_PATH_NOT_FOUND:
                    stRet = "Error: Path not found. Couldn't Execute!"
                Case ERROR_BAD_FORMAT:
                    stRet = "Error:  Bad File Format. Couldn't Execute!"
                Case Else:
            End Select
        End If
        fHandleFile = lRet & _
                    IIf(stRet = "", vbNullString, ", " & stRet)
    End Function
    '************ Code End **********
    

    只需将其放入单独的模块中,并使用正确的参数调用 fHandleFile()。

    【讨论】:

    • 访问 97 中的 URLsi 被转换为较低的。
    【解决方案4】:

    我觉得最简单的是

    shell "explorer.exe URL"
    

    这也适用于打开本地文件夹。

    【讨论】:

      【解决方案5】:

      【讨论】:

      • 链接已失效...仅供参考
      猜你喜欢
      • 1970-01-01
      • 2013-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-19
      • 2014-12-06
      • 2021-09-16
      • 2010-09-14
      相关资源
      最近更新 更多