how do we open a word file using vb script

36,578

Solution 1

LittleBobbyTables explained in his comment why your first example doesn't work.

As for your second example, it doesn't work because you don't insert any spaces between the winword.exe path and the file path, so your command line looks like this:

"C:\Program Files\Microsoft Office\Office\winword.exe""C:\UserGuide.doc"


Anyway, hard-coding the winword.exe path like this is unreliable, as this path is different in 64-bit and some localized Windows versions as well as for some MS Office versions. I suggest that you use Word automation objects instead:

Set oWord = CreateObject("Word.Application")
oWord.Visible = True
oWord.Documents.Open "C:\UserGuide.doc"

Solution 2

OPTION EXPLICIT
dim fso, ws, file_to_open, OFFICE_PATH
Set ws = WScript.CreateObject("WScript.Shell")
OFFICE_PATH = "C:\Program Files\Microsoft Office\Office"
file_to_open = CHR(34) & "C:\UserGuide.doc" & CHR(34)
ws.Run CHR(34) & OFFICE_PATH & "\winword.exe " & CHR(34) & file_to_open, 0, "FALSE"

try this revised code, check the modifications in last line :)

Share:
36,578
Bijoy K Jose
Author by

Bijoy K Jose

Someone who assures finest customer experience and best in class performance in digital transformation initiatives with his tools, techniques and expertise. Digital Performance Assurance Digital Value Chain Assurance Digital Customer Experience Assurance Digital Agility Assurance https://dzone.com/articles/embrace-digital-assurance-practices-in-devops-cycl

Updated on July 09, 2022

Comments

  • Bijoy K Jose
    Bijoy K Jose almost 2 years

    could anyone plz tell me how to open word files using vbs windows scripting.

    I tried out these two set of vbs, but windows script Host error ("The system cannot find the file specified", errorcode: 80070002) is getting displayed eventhough the file exists at the specified location.

    the first vbs i tried out:

    Dim sAppPath
    Dim sPrgFolder
    sPrgFolder=CreateObject("WScript.Shell").ExpandEnvironmentStrings("%ProgramFiles%") 
    sAppPath =sPrgFolder + "c:\UserGuide.doc"
    WScript.CreateObject("WScript.Shell").Run sAppPath)
    

    second vbs i tried:

    OPTION EXPLICIT
    dim fso, ws, file_to_open, OFFICE_PATH
    Set ws = WScript.CreateObject("WScript.Shell")
    OFFICE_PATH = "C:\Program Files\Microsoft Office\Office"
    file_to_open = CHR(34) & "C:\UserGuide.doc" & CHR(34)
    ws.Run CHR(34)& OFFICE_PATH & "\winword.exe" & CHR(34) & file_to_open, 0, "FALSE"