Solved

vbs script throwing errors when trying to create a file

Posted on 2011-03-22
5
452 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
add projects t working set in maven 2 44
C qsort compare function issue 6 45
Programming Language for Wordpress 7 67
Merging text files strings with filename 18 46
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This is about my first experience with programming Arduino.
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

726 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