创建后打开文本文件

创建后打开文本文件

请帮忙看看文件创建成功后无法打开查看,到底是哪里出了问题。

dim fname
fname=InputBox("Enter File Name:")
MsgBox("The File name is " & fname)

' Const ForReading = 1, ForWriting = 2, ForAppending = 8

Const ForReading = 1
Const OpenAsASCII = 0
Const OverwriteIfExist = -1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set fResultFile = objFSO.CreateTextFile(fname & ".txt", _
OverwriteIfExist, OpenAsASCII)

Set objInputFile = objFSO.OpenTextFile("iplist.txt", ForReading)
arrIpList = Split(objInputFile.ReadAll, vbCrLf)
objInputFile.Close

For l = 0 To UBound(arrIpList)
strIP = Trim(arrIpList(l))
If strIP <> "" Then
If Not IsConnectible(strIP, "", "") Then
fResultFile.WriteLine strIP & " is down"
End If
End If
Next


' Open Result file
Set inCsvSys = CreateObject("Scripting.FileSystemObject")
Set inCsv    = inCsvSys.OpenTextFile(fname & ".txt")
WScript.Echo "FIle found and opened successfully"





Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
' Works an "all" WSH versions
' sHost is a hostname or IP
' iPings is number of ping attempts
' iTO is timeout in milliseconds
' if values are set to "", then defaults below used

Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1
Dim oShell, oFSO, sTempFile, fFile

If iPings = "" Then iPings = 2
If iTO = "" Then iTO = 750

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")

sTempFile = oFSO.GetSpecialFolder(2).ShortPath & "\" & oFSO.GetTempName

oShell.Run "%comspec% /c ping.exe -n " & iPings & " -w " & iTO _
& " " & sHost & ">" & sTempFile, 0 , True

Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
FailIfNotExist, OpenAsASCII)

Select Case InStr(fFile.ReadAll, "TTL=")
Case 0 IsConnectible = False
Case Else IsConnectible = True
End Select

fFile.Close
oFSO.DeleteFile(sTempFile)

End Function

答案1

根据文档OpenTextFile方法

“打开指定的文件并返回可用于读取、写入或附加到该文件的 TextStream 对象。”

这不是您想要做的。您需要运行一个程序并将文件名作为命令行参数传递给它。

尝试:

With CreateObject("WScript.Shell")
    .Run "notepad.exe " & fname & ".txt"
End With

.Run fname & ".txt"假设您的系统上为 txt 文件设置了默认应用程序,那么(即不指定应用程序)可能会起作用。

相关内容