Visual Basic Open URL with Default Browser

2019-04-19 13:10发布

Edit

For VB 6

End Edit

Hey this seems like it should be an easy fix and I don't particularly like the Visual Basic language, but how would I open a URL in the default web browser, using code?

Edit
Why do I keep getting this error?

A call to PInvoke function 'CrackleMail!WindowsApplication1.FormFinal::ShellExecute' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

标签: url vb6 browser
4条回答
劫难
2楼-- · 2019-04-19 13:16
Option Explicit

'Link the kernel method that allows a process to be open/spawn

Private Declare Function ShellExecute _
                            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

Private Sub mnuAbrirNavegador_Click(Index As Integer)
    OpenUrl("http://www.microsoft.com")
End Sub

Private Sub OpenUrl(ByVal url As String)
    r = ShellExecute(0, "open", url, 0, 0, 1)
End Sub
查看更多
手持菜刀,她持情操
3楼-- · 2019-04-19 13:26

The code in the accepted answer gave a compile error for me I got the code below from MSDN Use ShellExecute to launch the default Web browser

Private Declare Function ShellExecute _
                            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

Private Sub Command1_Click()
   Dim r As Long
   r = ShellExecute(0, "open", "http://www.microsoft.com", 0, 0, 1)
End Sub
查看更多
甜甜的少女心
4楼-- · 2019-04-19 13:35

It is simple! Just use the Wscript createobject Method

CreateObject("Wscript.Shell").Run "www.example.com"
查看更多
一夜七次
5楼-- · 2019-04-19 13:37

VB.NET:

System.Diagnostics.Process.Start("http://example.com")

VB 6 (not sure):

Declare Function ShellExecuteA Lib "shell32.dll" ( _
    ByVal hWnd As IntPtr, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Integer) As IntPtr

ShellExecuteA(Me.Handle, "open", "http://example.com", "", "", 4)
查看更多
登录 后发表回答