Windows脚本宿主错误800A0046

时间:2013-07-19 19:20:56

标签: vbscript

运行程序时收到以下错误:

脚本:C:我的文件夹\跟踪Macro.vbs
行:70
Char:1
错误:权限被拒绝 代码:800A0046
来源:Microsoft VBScript运行时错误

这是代码。

' Set constants for reading, writing, and appending files
Const ForReading = 1, ForWriting = 2, ForAppending = 8

 ' Sets up the object variables.
 Dim objExcel, objFSO, objTextFile, objCSVFile

' Sets up the string variables.
Dim strTextFile, strHeadLine, strTextLine, strCSVFile

' Sets up the all the string variables for the program.
Dim Desktop, todaysDate, usageDate, myDay, myMonth, myYear

'This creates the required Objects
Set objExcel = CreateObject("Excel.application")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
Desktop = WshShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\" & "Desktop"

' Set date for date stamp in file name and sheet name
todaysDate = Date()

myMonth = Month(todaysDate)
If Len(myMonth)=1 Then myMonth="0" & myMonth

myDay = Day(todaysDate)
If Len(myDay)=1 Then myDay="0" & myDay

myYear = Right(Year(todaysDate), 2)

usageDate = myMonth & myDay & myYear

' Set up the origin and destination files
strTextFile = (Desktop & "\MacroTracker.txt")
strCSVFile = "C: My Folder\TrackingTesting" & usageDate & ".csv"
strHeadLine = "Macro Name,User ID,Ran At,Contracted Rate,BHVN,Set Number,Provider TIN,Billed Charge,Service Code"

Set objTextFile = objFSO.OpenTextFile(strTextFile)

' Read the entire origin file
Do Until objTextFile.AtEndOfStream
strTextLine = objTextFile.ReadLine
Loop

If (objFSO.FileExists(strCSVFile)) Then
' Create object for appending current TXT file to CSV file
Set objCSVFile = objFSO.OpenTextFile(strCSVFile, ForAppending, True)
' Write an append line of data to the CSV file
objCSVFile.WriteLine strTextLine
Else
' Create CSV file to write to with today's date
Set objCSVFile = objFSO.CreateTextFile(strCSVFile, True)
' Create object for appending current TXT file to CSV file
Set objCSVFile = objFSO.OpenTextFile(strCSVFile, ForAppending, True)
' Write initial header for the CSV file
objCSVFile.WriteLine strHeadLine
' Write an append line of data to the CSV file
objCSVFile.WriteLine strTextLine
End If

' Wait for file to be written to
Wscript.Sleep 600

' Delete origin file to prevent user tampering
objFSO.DeleteFile(strTextFile)

第70行是我删除文本文件的最后一行。根据我见过的每个帮助网站,这是如何打字的。我检查了文件的权限...我完全控制,所以我应该能够删除它。它只是一个临时文件,而不是长时间存储信息的东西。

我已经检查了Microsoft和所有其他帮助网站的错误代码,但没有找到任何可以帮助我的解决方案。我希望有人可能遇到类似的情况并找到解决方案。

1 个答案:

答案 0 :(得分:4)

您的文件仍处于打开状态。你需要添加这个:

objTextFile.Close
在您尝试删除它之前的某个地方。在你使用完文件后我会把它放好。