简体   繁体   中英

I'm getting error 800A03EA with “Else”

The problem is on line fourteen. Else is somehow a syntax error, (800A03EA) I'm very new to 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

Two changes required here.

  1. else statement started after closing if
  2. f.FileExists ← No object defined for f .

Modified code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM