Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

vbs script throwing errors when trying to create a file

Posted on 2011-03-22
5
Medium Priority
?
456 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
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 …
Introduction to Processes

618 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