4

I have an HTML file C:\Program Files\my_html_files\file.html. How do I open this file using VBScript? (By "open" I mean open it with the default application, as if it was double-clicked in Explorer.)

4 Answers 4

8

The following VBScript code does the equivalent of double clicking on the file.html and having the default open command occur for that file:

Dim wshShell
Set wshShell = CreateObject("WScript.Shell")
wshShell.Run """C:\Program Files\my_html_files\file.html"""

If you want to get really tricksy you can omit the variable declaration and write it in one line of code:

CreateObject("WScript.Shell").Run """C:\Program Files\my_html_files\file.html"""

N.B. Strings may require to be enquoted to handle paths containing spaces. This can be done using the Chr(34) suggestion by @aland or as I've done in my code examples.

Sign up to request clarification or add additional context in comments.

1 Comment

You'll need to wrap the path with Chr(34) if there's spaces, e.g. wshShell.Run Chr(34) & "C:\Program Files\my_html_files\file.html" & Chr(34)
4

Do you mean open the file in Internet Explorer?

Dim objIE
'' Create an IE object
Set objIE = CreateObject("InternetExplorer.Application")
'' Open file
objIE.Navigate "C:\Program Files\my_html_files\file.html"

Comments

1

You can use this

CreateObject("WScript.Shell").Run Chr(34) & "C:\Program Files\my_html_files\file.html" & Chr(34)

1 Comment

There is a & missing. Both upvoter should be ashamed.
0

You can use the File System Object like this:

Set FSO=CreateObject("Scripting.FileSystemObject")
Set iFile = FSO.OpenTextFile ("C:\Program Files\my_html_files\file.html")
Data = iFile.ReadAll
MsgBox(Data)
iFile.Close

1 Comment

It not good I need the action as double click on the file.html Your script not display the real file.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.