How to Write a VBS Script to Open a File

Visual Basic Scripting (VBS) is a language based on the Visual Basic compiler. VBS files are written in plain text, and when you double-click the file, Windows automatically knows to execute the file. You can write VBS script files for several computer maintenance and automation processes. One of these processes is the ability to automatically open a file. The file opens and uses the default software associated with the file extension.

Instructions

    • 1

      Click the Windows "Start" button and select "All Programs." Choose "Accessories" and then click the "Notepad" icon. Since VBS files are plain text, you can use Notepad to create and edit them.

    • 2

      Create a variable to hold the file you want to open. The following code creates a variable and assigns it a Microsoft Word document to open.
      Dim my_file
      my_file = "my_document.doc"

    • 3

      Open the file. The following code opens the file indicated in the my_file variable:
      ShellExecute 0, vbNullString, my_file, vbNullString, vbNullString, vbNormalFocus.
      The "vbNullString" options are not used, so this variable is used to pass a null value. The "vbNormalFocus" is a variable used to set focus to the document.

    • 4

      Save the file. Ensure you save it with a ".vbs" file extension. Double-click the file to see it run and test it. After it runs, the document indicated opens on your desktop.

Related Searches:

References

Comments

You May Also Like

Related Ads

Featured