Solved

vbs script throwing errors when trying to create a file

Posted on 2011-03-22
5
446 Views
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
	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

Open in new window

0
Comment
Question by:adamshields
  • 4
5 Comments
 
LVL 7

Accepted Solution

by:
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"
   Wscript.Quit(-1)
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)
   Else
      Wscript.Echo FileLoc & " not found"
      Wscript.Quit(-1)
   End If
Else
   Wscript.Echo strFileFolder & " not found"
   Wscript.Quit(-1)
End If

Open in new window

0
 
LVL 3

Author Comment

by:adamshields
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...
0
 
LVL 3

Author Comment

by:adamshields
ID: 35194954
excel is not running under task manager either, thanks for your help
0
 
LVL 3

Author Comment

by:adamshields
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?
0
 
LVL 3

Author Closing Comment

by:adamshields
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
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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 …

746 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

13 Experts available now in Live!

Get 1:1 Help Now