2016-07-28 61 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行。 –