Solved

vbs script throwing errors when trying to create a file

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

839 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