vba 使用默认浏览器打开浏览器/URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42137991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:05:28  来源:igfitidea点击:

Open Browser/URL with default browser

vbams-access

提问by rel0aded0ne

I want to open an URL with my default browser.

我想用我的默认浏览器打开一个 URL。

I tried to use "Call Shell("C:\Program Files (x86)\Mozilla Firefox\firefox.exe -url" & strUrl, 1)" but firefox wont open the URL. Instead firefox is starting with my default page.

我尝试使用“ Call Shell("C:\Program Files (x86)\Mozilla Firefox\firefox.exe -url" & strUrl, 1)”,但 Firefox 不会打开 URL。相反,firefox 从我的默认页面开始。

When i use "Call Shell(strURL,1)" im getting a "File not found" Error.

当我使用“ Call Shell(strURL,1)”时,出现“找不到文件”错误。

Private Sub openurl_Click()
    Dim urlopen As String
    Dim User As String
    Dim pass As String

        urlopen = URL.Value
        User = Username.Value
        'pass = Passwort.Value
        pass = InputBox("Passwort eingeben")
        strUrl = "https://" & User & ":" & pass & "@" & urlopen

        'MsgBox strURL <- TEST OUTPUT

        'Call Shell("C:\Program Files (x86)\Mozilla Firefox\firefox.exe -t" & strUrl, 1)
        'Call Shell(strURL, 1)

    End Sub

回答by rel0aded0ne

I got a solution. Maybe its interesting for someone here.

我得到了解决方案。也许对这里的人来说很有趣。

    Private Sub openurl_Click()
    Dim urlopen As String
    Dim User As String
    Dim pass As String

        urlopen = URL.Value
        User = Username.Value
        'pass = Passwort.Value
        pass = InputBox("Passwort eingeben")
        strURL = "cmd /c start https://" & User & ":" & pass & "@" & urlopen

        'MsgBox strURL <- Test Output

        Call Shell(strURL, 1)

End Sub

i added cmd /c startto the strURLString

我添加cmd /c startstrURL字符串

strURL = "cmd /c start https://" & User & ":" & pass & "@" & urlopen

it seems to be a "dirty" solution but it works :D

这似乎是一个“肮脏”的解决方案,但它有效:D

回答by Sergey S.

Try Application.FollowHyperlinkinstead of Call Shell

尝试Application.FollowHyperlink代替Call Shell

Application.FollowHyperlink strURL

回答by Thomas G

Here's the way I handle this in my Access apps, with 100% success so far. It directly calls the windows API shell32.dll

这是我在 Access 应用程序中处理此问题的方式,到目前为止已 100% 成功。它直接调用windows API shell32.dll

1. Create a module and add this :

1. 创建一个模块并添加:

'------------------------------------------------------------------------
' Open an external application - Advanced way
'------------------------------------------------------------------------
Public 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


'------------------------------------------------------------------------
' Open Webpage in default browser
'------------------------------------------------------------------------
Public Sub OpenUrl(strURL)
    Dim lSuccess As Long
    lSuccess = ShellExecute(0, "Open", strURL)
End Sub

2. From your code, open your URLs like this:

2. 从您的代码中,像这样打开您的 URL:

OpenUrl "http://anything.com"


You can further expand the ShellExecutefunction to open anything else and not only URLs

您可以进一步扩展该ShellExecute功能以打开任何其他内容而不仅仅是 URL