How to open a text file using vb script

can anyone tell me how to open text files using windows vbs scripts.

I tried these two sets of vbs, but windows script host error ( "The system cannot find the file specified", errorcode: 80070002) is displayed, although the file exists in the specified location.

first vbs i tried:

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"
+3
source share
3 answers

LittleBobbyTables explained in his comment why your first example is not working.

As for your second example, this does not work because you do not insert spaces between the end of winword.exe and the file path, so your command line looks like this:

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


, winword.exe, , , 64- Windows, MS Office. Word:

Set oWord = CreateObject("Word.Application")
oWord.Visible = True
oWord.Documents.Open "C:\UserGuide.doc"
+7
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"

, :)

+1

Thanks, friends ..... I got his job with these vbs.

Dim shell, quote, pgm, fname

set shell = WScript.CreateObject("WScript.Shell")
quote = Chr(34)
pgm = "WINWORD"
fname = "C:\UserGuide.doc"
shell.Run quote & pgm & quote & " " &fname
0
source

Source: https://habr.com/ru/post/1768048/


All Articles