使用 VBA 在默认浏览器中打开 html 页面? [英] Open an html page in default browser with VBA?

查看:32
本文介绍了使用 VBA 在默认浏览器中打开 html 页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I open an HTML page in the default browser with VBA? I know it's something like:

Shell "http://myHtmlPage.com"

但我想我必须引用将打开页面的程序.

But I think I have to reference the program which will open the page.

推荐答案

您可以使用 Windows API 函数 ShellExecute 来做到这一点:

You can use the Windows API function ShellExecute to do so:

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 ,如下所示:

As given in comment, to make it work in 64-bit, you need add PtrSafe in the Private Declare Line as shown below:

Private Declare PtrSafe Function ShellExecute _

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

Just a short remark concerning security: If the URL comes from user input make sure to strictly validate that input as ShellExecute would execute any command with the user's permissions, also a format c: would be executed if the user is an administrator.

这篇关于使用 VBA 在默认浏览器中打开 html 页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆