我收到错误800A03EA与“Else”

时间:2016-04-24 00:41:33

标签: vbscript

问题在于十四号。否则就是语法错误,(800A03EA)我对VBScript很新。

Dim StrThing, fso, f
Const ForReading = 1, ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
If f.FileExists("Desktop\testfile") then
    StrThing=InputBox("Type your name in")
    Set f = fso.OpenTextFile("Desktop\testfile.txt", ForWriting, True)
    f.WriteLine "Hello world!" 
    f.WriteLine "Hello, " & StrThing
    Set f = fso.OpenTextFile("Desktop\testfile.txt", ForReading)
    WriteLineToFile = f.ReadAll
    StrThing=MsgBox("Hello, " & StrThing)
    StrThing=MsgBox("Goodbye, " & StrThing)
    End if
Else
    x=MsgBox("Hello")
    f.createTextFile
    StrThing=InputBox("Type your name in")
    Set f = fso.OpenTextFile("Desktop\testfile.txt", ForWriting, True)
    f.WriteLine "Hello world!" 
    f.WriteLine "Hello, " & StrThing
    Set f = fso.OpenTextFile("Desktop\testfile.txt", ForReading)
    WriteLineToFile = f.ReadAll
    StrThing=MsgBox("Hello, " & StrThing)
    StrThing=MsgBox("Goodbye, " & StrThing)
    End if

1 个答案:

答案 0 :(得分:0)

此处需要进行两项更改。

    关闭else
  1. if语句开始
  2. f.FileExists←没有为f定义对象。
  3. 修改后的代码:

    Dim StrThing, fso, f
    Const ForReading = 1, ForWriting = 2
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = CreateObject("Scripting.FileSystemObject")
    If f.FileExists("Desktop\testfile.txt") then
        StrThing=InputBox("Type your name in")
        Set f = fso.OpenTextFile("Desktop\testfile.txt", ForWriting, True)
        f.WriteLine "Hello world!" 
        f.WriteLine "Hello, " & StrThing
        Set f = fso.OpenTextFile("Desktop\testfile.txt", ForReading)
        WriteLineToFile = f.ReadAll
        StrThing=MsgBox("Hello, " & StrThing)
        StrThing=MsgBox("Goodbye, " & StrThing)
    Else
        x=MsgBox("Hello")
        f.createTextFile
        StrThing=InputBox("Type your name in")
        Set f = fso.OpenTextFile("Desktop\testfile.txt", ForWriting, True)
        f.WriteLine "Hello world!" 
        f.WriteLine "Hello, " & StrThing
        Set f = fso.OpenTextFile("Desktop\testfile.txt", ForReading)
        WriteLineToFile = f.ReadAll
        StrThing=MsgBox("Hello, " & StrThing)
        StrThing=MsgBox("Goodbye, " & StrThing)
    End if