我找到了这个脚本,似乎无法弄清楚为什么在尝试创建文件C:\IPSecWeights.xls
时会抛出错误。
到目前为止,我遇到的问题是:
Set objWorkbook = objExcel.Workbooks.Open(FileLoc)
我收到一条错误,指出无法找到该文件。如何重写我的代码来解决这个问题?
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Dim objFSO,objFile
Dim arrLines
Dim strLine
Dim objExcel,objWorkbook
Dim FileLoc
Dim intRow
Dim objDictionary
FileLoc = "C:\IPSecWeights.xls"
Sub ExcelHeaders()
Set objRange = objExcel.Range("A1","G1")
objRange.Font.Size = 12
objRange.Interior.ColorIndex=15
objexcel.cells(1,1)="Filter Name"
objexcel.cells(1,2)="Source"
objexcel.cells(1,3)="Destination"
objexcel.cells(1,4)="Source Port"
objexcel.cells(1,5)="Destination Port"
objexcel.cells(1,6)="Protocol"
objexcel.cells(1,7)="Direction"
End Sub
Function RegExFind(strText,strPattern)
Dim regEx
Dim match, Matches
Dim arrMatches
Dim i : i = 0
Set regEx = New RegExp
regEx.IgnoreCase = True
regEx.Global = True
regEx.Pattern = strPattern
Set matches = regEx.Execute(strText)
ReDim arrMatches(Matches.Count)
For Each match In Matches
For Each SubMatch In match.Submatches
arrMatches(i) = Submatch
i = i + 1
Next
Next
RegExFind = arrMatches
End Function
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(WScript.Arguments(0),ForReading)
Set objExcel = CreateObject("excel.application")
Set objWorkbook = objExcel.Workbooks.Open(FileLoc)
objExcel.Visible = True
ExcelHeaders ' Create Excel Headers
rePolicy = "Policy Name\s+:\s(.+)"
reSRCAddr = "Source Address\s+:\s(.+)"
reDSTAddr = "Destination Address\s+:\s(.+)"
reProtocol = "Protocol\s+:\s(.+)"
reSRCPort = "Source Port\s+:\s(.+)"
reDSTPort = "Destination Port\s+:\s(.+)"
reDirection = "Direction\s+:\s(.+)"
strText = objFile.ReadAll
objFile.Close
Dim arrPolicy, arrSRCAddr, arrDSTAddr, arrProtocol, arrSRCPort, arrDSTPort, arrDirection
arrPolicy = RegExFind(strText, rePolicy)
arrSRCAddr = RegExFind(strText, reSRCAddr)
arrDSTAddr = RegExFind(strText, reDSTAddr)
arrProtocol = RegExFind(strText, reProtocol)
arrSRCPort = RegExFind(strText, reSRCPort)
arrDSTPort = RegExFind(strText, reDSTPort)
arrDirection = RegExFind(strText, reDirection)
intRow = 2
For i = 0 To UBound(arrPolicy)
objExcel.Cells(introw,1) = arrPolicy(i)
objExcel.Cells(introw,2) = arrSRCAddr(i)
objExcel.Cells(introw,3) = arrDSTAddr(i)
objExcel.Cells(introw,4) = arrSRCPort(i)
objExcel.Cells(introw,5) = arrDSTPort(i)
objExcel.Cells(introw,6) = arrProtocol(i)
objExcel.Cells(introw,7) = arrDirection(i)
intRow = intRow + 1
Next
objFile.Close
objWorkbook.save
'objExcel.Quit
答案 0 :(得分:3)
我怀疑“找不到文件”是一个有点误导性的错误消息。也就是说,您的实际问题是文件系统权限,但VBScript顽固地报告这是“未找到文件”而不是更明智的错误。
据推测,该脚本在早期版本的Windows中运行良好,但您现在正在安装Vista或更高版本的计算机上进行尝试,这会引入一系列额外的安全功能和写保护。 User Account Control (UAC)不允许应用程序或脚本写入硬盘的根级别。
这当然不应该是一个问题,因为合法的应用程序不需要篡改硬盘根级别或共享系统文件夹(如C:\Windows
)中的文件,但偶尔会出现问题在测试期间。
将脚本中的文件路径更改为您可以保证具有读/写权限的文件路径,例如“我的文档”文件夹。对于部署,您不应该对文件系统路径进行硬编码。请使用FileSystemObject
的{{3}}来检索路径。