1 vbscript
当我运行我的代码时,我在第15行收到错误"对象变量未设置".我不是每次都得到它.这似乎是随机的.我已经搜索过,但找不到任何理由.我遇到的问题是变量没有一直被清除,所以我刚添加第42行设置strText = Nothing.我没有正确设置变量的问题消失了,但现在我有了这个.任何帮助将不胜感激.代码如下.
Option Explicit
Dim i, objFSO, objFile, strText, Fields, TimeStamp, Location, MachNum, Amount, Theme, objSMFile, SMLine, p, CSVLine, objReportFile, FileName
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Do while i<>1
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("C:\Print\output.txt") Then
'*** Clean up CR/LF and Make CSV Variable ***
Set objFile = objFSO.OpenTextFile("C:\Print\output.txt", ForReading)
If Not objFile.AtEndOfStream Then strText = objFile.ReadAll
objFile.Close
' WScript.Sleep 1000
objFSO.CopyFile "C:\Print\output.txt", "C:\Print\outputCopy.txt"
objFSO.DeleteFile("C:\Print\output.txt")
' WScript.Sleep 3000
strText = Replace(strText, vbCrlf, "")
strText = Replace(strText, " ", ";")
' Split by semi-colon into an array...
Fields = Split(strText, ";")
TimeStamp = Fields(0)
Location = Fields(1)
MachNum = Fields(3)
Amount = Fields(4)
'*** Find Machine Number in Slot Master ***
Set objSMFile = objFSO.OpenTextFile("C:\Scripts\SlotMaster.csv", ForReading)
do while not objSMFile.AtEndOfStream
SMLine=objSMFile.readline
p=instr(SMLine, MachNum)
if p>0 then CSVLine = SMLine
Loop
' Split by comma into an array...
Fields = Split(CSVLine, ",")
Theme = Fields(7)
'*** Create Tab Delimited Text File ***
FileName = Year(Now) & "-" & Month(Now) & "-" & Day(Now) & " Jackpots.txt"
If Not objFSO.FileExists("C:\Print\" & FileName) Then
Set objReportFile = objFSO.CreateTextFile("C:\Print\" &FileName)
objReportFile.Close
End If
Set objReportFile = objFSO.OpenTextFile("C:\Print\" & FileName, ForAppending)
objReportFile.Write TimeStamp & vbTab & Location & vbTab & Amount & vbTab & Theme & vbCrLf
objReportFile.Close
Set strText = Nothing
End If
Loop
Run Code Online (Sandbox Code Playgroud)
简短的回答
strText不是一个对象.它在那里处理一个字符串.
因此,您应该重新启动它:
strText= ""
Run Code Online (Sandbox Code Playgroud)
长期回答
当你这样做
Set strText = Nothing
Run Code Online (Sandbox Code Playgroud)
strText为对象,因此它不再是字符串.""因此,不允许尝试为其分配值(空白字符串).Replace()或.ReadAll将要执行它,它会失败,因为它不再存在.即使它存在,它也会失败,因为它不再是一个字符串,如前所述.