vbs script throwing errors when trying to create a file

Posted on 2011-03-22
Last Modified: 2012-08-14
i have found this script and i am trying to run it but i am getting an error, the name of the vbs is parse,vbs and i am running it by pasrs.vbs file.txt, why is it having issues creating the file? ipsec.xls
line 54
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:\parse\ipsec.xls"

Sub ExcelHeaders()
	Set objRange = objExcel.Range("A1","G1")
	objRange.Font.Size = 12
	objexcel.cells(1,1)="Filter Name"
	objexcel.cells(1,4)="Source Port"
	objexcel.cells(1,5)="Destination Port"
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
	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

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


Open in new window

Question by:adamshields
  • 4

Accepted Solution

holthd earned 500 total points
ID: 35194900
Here's a few things I can think of:
A) You managed to get past line 54 in the past. Now another process of excel.exe is holding the file open.
B) 'C:\Parse\ipsec.xls' does not exist
C) The user account the script runs as does not have Read permissions on the file 'C:\Parse\ipsec.xls'

You might want to replace lines 53-54 with the below for a little more reliabilty.

On Error Resume Next
Set objExcel = CreateObject("excel.application")
If (Err.Number <> 0) Then
   Wscript.Echo "Unable to create object Application.Excel"
End If
On Error GoTo 0
Dim strFileFolder : strFileFolder = strReverse(FileLoc)
strFileFolder = Left(strReverse(strFileFolder), Len(strFileFolder)-InStr(strFileFolder, "\"))
If objFSO.FolderExists(strFileFolder) = TRUE Then
   If objFSO.FileExists(FileLoc) = TRUE Then
      Set objWorkbook = objExcel.Workbooks.Open(FileLoc)
      Wscript.Echo FileLoc & " not found"
   End If
   Wscript.Echo strFileFolder & " not found"
End If

Open in new window


Author Comment

ID: 35194944
ok well your part of code worked by getting me past the line 54, but i am still getting c:\parse\ipsec.xls does not exist, i manually created the file and still get the same thing, this should be creating the excel file right? i am sure i have access to the specific folder etc...

Author Comment

ID: 35194954
excel is not running under task manager either, thanks for your help

Author Comment

ID: 35195138
well, i have been trying some things and with the first script that i posted, if i change the FileLoc file name to ipsec.txt the script will run, it opens excel and everything, but i get an out of range error, why is it having issues with just the .xls file extension? any ideas?

Author Closing Comment

ID: 35195207
for some reason i had to open excel and create/save the file, i was just right clicking in the folder and creating a new excel file

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now