Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating CSV file with VBScript 1

Status
Not open for further replies.

Yaik

Programmer
Oct 1, 2010
36
US
This questions is more of a "why doesn't this script work".
It used to work, but now I have no clue why. It is supposed to run on a specific computer, but it doesn't completely work. Then I took the script home to work on it, and it worked perfectly fine on my computer, so I am kind of lost as to why it wouldn't work on a specific computer.

Here is the line of code that is giving me trouble.
Code:
line=0
Set txtFile1 = objfso.OpenTextFile(objFileNameOld, ForReading)
Set strLine1 = CreateObject("VBScript.RegExp")

Do Until txtFile1.AtEndOfStream
line=line+1
'Set f = objfso.OpenTextFile(objFileNameOld, ForWriting)
strMatch = False
    strLine1 = txtFile1.Readline
    tempstrLine = strLine1
    MyArray = Split(strLine1, ",", -1, 1)
    strLine1 = MyArray(1)
Set txtFile2 = objfso.OpenTextFile(objFileName, ForReading)
        Do Until txtFile2.AtEndOfStream
            strLine2 = txtFile2.Readline
	    MyArray2 = Split(strLine2, ",", -1, 1)
	    strLine2 = MyArray2(1)
		If Trim(UCase(strLine2)) = Trim(UCase(strLine1)) Then
                    strMatch = True
                Else 
                End If 

        Loop
	txtFile2.Close

        If strMatch <> True then
	'f.close
	Set objExcel = CreateObject("Excel.Application") 
	objExcel.Visible = False
	objExcel.DisplayAlerts = False
	objExcel.Workbooks.Add
	Set objWorkbook = objExcel.Workbooks.Open (objFileNameOld)

	objExcel.Cells(line, 1).Value = GetCustomDate
	objExcel.Cells(line, 5).Value = "REMOVED"
	objExcel.ActiveWorkBook.SaveAs objFileNameOld
	objExcel.ActiveWorkbook.Close

	objExcel.Application.Quit

        End If
Loop

txtFile1.Close
txtFile2.Close
f.close

Then I have this very similar lines of codes that do work.
Code:
Dim txtFile, txtFile2, strLine2

Set txtFile1 = objfso.OpenTextFile(objFileName, ForReading)
Set strLine1 = CreateObject("VBScript.RegExp")

Do Until txtFile1.AtEndOfStream
Set f = objfso.OpenTextFile(objFileNameOld, ForAppending)
strMatch = False
    strLine1 = txtFile1.Readline
    tempstrLine = strLine1
    MyArray = Split(strLine1, ",", -1, 1)
    strLine1 = MyArray(1)
Set txtFile2 = objfso.OpenTextFile(objFileNameOld, ForReading)
        Do Until txtFile2.AtEndOfStream
            strLine2 = txtFile2.Readline
	    MyArray2 = Split(strLine2, ",", -1, 1)
	    strLine2 = MyArray2(1)
			If Trim(UCase(strLine2)) = Trim(UCase(strLine1)) Then
                    strMatch = True
					strLine1=MyArray(4)
					strLine2=MyArray2(4)
				If strLine2 <> strLine1 Then
				strMatch=False
			
				End If
			
            Else 
            End If 
			strLine1=MyArray(1)
			strLine2=MyArray2(1)
		Loop
	txtFile2.Close

        If strMatch <> True then
             f.writeline tempstrLine
	f.close
	Set objExcel = CreateObject("Excel.Application") 
	objExcel.Visible = False
	objExcel.DisplayAlerts = False
	objExcel.Workbooks.Add
	Set objWorkbook = objExcel.Workbooks.Open (objFileNameOld) 
	objExcel.Cells(intNewRow, 5).Value = "NEW"
	objExcel.ActiveWorkBook.SaveAs objFileNameOld
	objExcel.ActiveWorkbook.Close
	objExcel.Application.Quit
	intNewRow=intNewRow+1
        End If
Loop

txtFile1.Close
txtFile2.Close
f.close

Any help would really be appreciated. I am pulling my hair out at this moment.
 
Some contradictory assignment in your code, eg:
Set strLine1 = CreateObject("VBScript.RegExp")
...
strLine1 = txtFile1.Readline

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
That didn't make it work.

I've pinpointed the problem, which I don't know why it is a problem. The problem is that it doesn't want o save. I verified that it writes the text but doesn't save it. This is because I made excel visible so that I can see if it writes it, and it does, but then it doesn't save right.
 
In your first (non-working version) Excel is trying to save as 'objFileNameOld' which you still have open as textfile1 (the file is locked and cannot be saved). In the version that works you have closed objFileNameOld before Excel does the save as (allowing it to work).
 
OK, nevermind that, got it working, thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top