Link to home
Start Free TrialLog in
Avatar of CodeJunky
CodeJunkyFlag for United States of America

asked on

Problem with OpenTextFile Procedure in a VBScript

Hi All, I having a vbscript that looks in a particular directory(s), for a file that will change text within that/those files.  When I get to the code line below, I get an error:
Invalid procedure call or argument code: 80A0005 LINE:22 CHAR:3

Set objFile = objFSO.OpenTextFile(strFileName1, ForReading)

I used this very code by itself, simpliar code and it worked.  Any ideas would be appreciated.

thanks,
John.

'----------------------------------------------------------------------------------------
Const USER_PROFILE = &H28&

'START AT THE USER'S PROFILE
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(USER_PROFILE)
Set objFolderItem = objFolder.Self 

'THEN BEGIN AT THE USER'S APPDATA AND PMM ROOT DIRECTORY
Set objShell2 = CreateObject("Shell.Application")
Set objFolder2 = objShell.Namespace(objFolderItem.Path & "\appdata\local\Atlantic_Realty_Developme")
Set objFolderItem2 = objFolder2.Self 

'CHANGE PMM CONFIG FILE IN EACH DIRECTORY
Set colItems = objFolder2.Items
For Each objItem in colItems
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	strFileName1 = objFolderItem.Path & "\AppData\Local\PROGNAME\" & objItem.Name & "\3.0.0.0\user.config"
	strFileName2 = objFolderItem.Path & "\AppData\Local\PROGNAME\" & objItem.Name & "\1.0.0.0\user.config"
	
	IF objFSO.FileExists(strFileName1) Then
		'CHANGE TEXT IN CONFIG FILES		
		Set objFile = objFSO.OpenTextFile(strFileName1, ForReading)
		strText = objFile.ReadAll
		objFile.Close
		strNewText = Replace(strText, strOldText, strNewText)

		Set objFile = objFSO.OpenTextFile(strFileName1, ForWriting)
		objFile.WriteLine strNewText
		objFile.Close
	END IF
	
	IF objFSO.FileExists(strFileName2) Then
		'CHANGE TEXT IN CONFIG FILES
		Set objFile = objFSO.OpenTextFile(strFileName2, ForReading)
		strText = objFile.ReadAll
		objFile.Close
		strNewText = Replace(strText, strOldText, strNewText)

		Set objFile = objFSO.OpenTextFile(strFileName2, ForWriting)
		objFile.WriteLine strNewText
		objFile.Close
	END IF
Next

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CodeJunky

ASKER

That worked perfectly. thank you very much.
If you could, can you explain how those constants work?
Avatar of Bill Prew
Bill Prew

Think of them just like variables, the difference is a constant can't have it's value changed after it is set.

It has the same effect as doing this:

ForReading = 1
ForWriting = 2
ForAppending = 8

Then in the code that follows you can use the constant name and it makes the code much more readable and understandable.  There are a number of constants like this that can be passed to methods of the filesystemobject.

Here's a couple of links for further info:

http://support.microsoft.com/kb/163009
http://msdn.microsoft.com/en-us/library/314cz14s%28v=vs.84%29.aspx

~bp
Thanks very much for the explaination