Hi guys, how can I attach or link a file (an "HTML document") - a saved Web page...
that when I click a command button, the "HTML document" will be executed, just like double-clicking it in the explorer window.

Just view the attached image file for better understanding about my question, it's a screen shot..

I've been trying it out for 2 days, but i can't figure it out...

Sorry, for I'm just a newbie in VB 6...:(

Dani AI

Generated

This thread shows the common VB6 confusion between launching an executable and asking Windows to open a document with its registered program. The Run-time error '5' that saw comes from calling VB6's Shell with an .html filename: Shell is meant to start executables, not file types. 's ShellExecute approach is the robust fix because it asks Windows to use the file association; 's suggestion of launching the browser executable also works but requires the exact executable path and correct quoting.

Checklist and troubleshooting:

  • Confirm the exact path string (for example with Debug.Print App.Path & "\QuizThing.html").
  • Verify the file actually exists (use Dir(...) or FSO FileExists) before attempting to open it.
  • Note App.Path can differ between the IDE and a compiled EXE; ensure the HTML is placed where the running program expects it.
  • If folder names contain spaces, wrap the path in quotes when passing it to external commands.

A simple alternative that avoids API declares is to use the Shell COM object (lets Windows pick the associated application):

Dim sh As Object
Set sh = CreateObject("Shell.Application")
sh.ShellExecute filePath, "", "", "open", 1

Other options: embed the WebBrowser control to render the page inside the app, or launch a specific browser exe when control over the host is required. Always include a graceful error path (file-not-found or fallback dialog) so the app behaves predictably across different machines.

Recommended Answers

All 6 Replies

Hi guys, how can I attach or link a file (an "HTML document") - a saved Web page...
that when I click a command button, the "HTML document" will be executed, just like double-clicking it in the explorer window.

Just view the attached image file for better understanding about my question, it's a screen shot..

I've been trying it out for 2 days, but i can't figure it out...

Sorry, for I'm just a newbie in VB 6...:(

Use your shell command i.e.

Shell (c:/PoisenedHeart.html), vbNormalFocus

Make sure to have the path name correct and specify the window focus after comma.

if you want to shell and view in ie u must use shell with iexplorer address (C:\Program Files\Internet Explorer\iexplore.exe) and youe html file parameter (see shell on msdn) but if c:\ not default windows path u have problem to open ur file.
with windows api you can open all of filetype with origininal application like mydoc.doc window open with word mypdf.pdf open with acrobat,... every thing open in winodws you can open it.

another way u can use iframe control and develope new viewer for you self

Hi guys, how can I attach or link a file (an "HTML document") - a saved Web page...
that when I click a command button, the "HTML document" will be executed, just like double-clicking it in the explorer window.

Just view the attached image file for better understanding about my question, it's a screen shot..

I've been trying it out for 2 days, but i can't figure it out...

Sorry, for I'm just a newbie in VB 6...:(

Use your shell command i.e.

Shell (c:/PoisenedHeart.html), vbNormalFocus

Make sure to have the path name correct and specify the window focus after comma.

I have tried it, but I got this error:

Run-time error '5'
Invalid procedure call or argument

My code is this:

Private Sub Command1_Click()
    Shell App.Path & "\QuizThing.html", vbNormalFocus
End Sub

You can view the screenshot....

Here's the full code. This works on my side. Copy and paste into your form module.

Option Explicit

Private Declare Function GetDesktopWindow Lib "user32" () As Long

Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWDEFAULT As Long = 10

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 sTopic As String
   Dim sFile As String
   Dim sParams As String
   Dim sDirectory As String

  'set default setting to be used
  'if not specifically required
   sTopic = "Open"
   sFile = vbNullString
   sParams = vbNullString
   sDirectory = vbNullString
              
sFile = App.Path & "\QuizThing.html"

   
Call RunShellExecute(sTopic, sFile, sParams, sDirectory, SW_SHOWDEFAULT)
   
End Sub

Private Sub RunShellExecute(sTopic As String, sFile As Variant, sParams As Variant, sDirectory As Variant, nShowCmd As Long)

   Dim success As Long
Dim hWndDesk As Long
  
  'the desktop will be the
  'default for error messages
   hWndDesk = GetDesktopWindow()
  
  'execute the passed operation
   success = ShellExecute(hWndDesk, sTopic, sFile, sParams, sDirectory, nShowCmd)

  'This is optional. Uncomment the three lines
  'below to have the "Open With.." dialog appear
  'when the ShellExecute API call fails
  If success < 32 Then
     Call Shell("rundll32.exe shell32.dll,OpenAs_RunDLL " & sFile, vbNormalFocus)
  End If
   
End Sub

You'll see at the end there is an error trap. This will open the 'Open With' dialog box if your html document is not found or any other error occurs.

Here's the full code. This works on my side. Copy and paste into your form module.

Option Explicit

Private Declare Function GetDesktopWindow Lib "user32" () As Long

Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWDEFAULT As Long = 10

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 sTopic As String
   Dim sFile As String
   Dim sParams As String
   Dim sDirectory As String

  'set default setting to be used
  'if not specifically required
   sTopic = "Open"
   sFile = vbNullString
   sParams = vbNullString
   sDirectory = vbNullString
              
sFile = App.Path & "\QuizThing.html"

   
Call RunShellExecute(sTopic, sFile, sParams, sDirectory, SW_SHOWDEFAULT)
   
End Sub

Private Sub RunShellExecute(sTopic As String, sFile As Variant, sParams As Variant, sDirectory As Variant, nShowCmd As Long)

   Dim success As Long
Dim hWndDesk As Long
  
  'the desktop will be the
  'default for error messages
   hWndDesk = GetDesktopWindow()
  
  'execute the passed operation
   success = ShellExecute(hWndDesk, sTopic, sFile, sParams, sDirectory, nShowCmd)

  'This is optional. Uncomment the three lines
  'below to have the "Open With.." dialog appear
  'when the ShellExecute API call fails
  If success < 32 Then
     Call Shell("rundll32.exe shell32.dll,OpenAs_RunDLL " & sFile, vbNormalFocus)
  End If
   
End Sub

You'll see at the end there is an error trap. This will open the 'Open With' dialog box if your html document is not found or any other error occurs.

Wow!!,,..What a code....
Thank you for your time for doing these codes...

Hope this will solve my problem....

A BIG Thanks to you!
:icon_smile:

Only a pleasure. Please mark as answered if it works for you. You are also welcome to add to my reputation should you feel so.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.