2016-07-28 62 views
1

我在运行代码时收到第15行的“对象变量未设置”错误。我不明白每一次。这似乎是随机的。我已经搜索过,但找不到任何理由。我遇到了一个问题,因为变量一直没有被清除,所以我只是添加了第42行Set strText = Nothing。我的问题与变量设置不正确,但现在我有这个。任何帮助将不胜感激。代码如下。未设置VBScript对象变量

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 
+1

建议阅读:[如何创建一个最小,完整和可验证示例](http://stackoverflow.com/help/mcve) – wogsland

回答

6

SHORT ANSWER

strText不是一个对象。它在那里处理一个字符串。

你应该这样用重新初始化它:

strText= "" 

长的答案

当你

Set strText = Nothing 
  1. 隐含声明strText作为对象,因此它不再是字符串了。因此试图给它分配一个值""(空白字符串)是不允许的。
  2. 你减少它的引用计数器,它使对象不再可寻址并且是垃圾收集器的候选者。因此,下一次执行Replace().ReadAll时,它会失败,因为它不再存在。即使它存在,它也会失败,因为它不再是字符串,如前所述。
+0

现在我得到一个随机的“下标超出范围:'[数字:0]“在第17行。 –