I'm trying to read a folder full of XML files and change the reference number into a specific format which is Today's DATE(yymmdd), Initials, 8 digit reference starting at 00000001
e.g 120815AB00000001 then 120815AB00000002 etc. Each file has ONE reference number. It is enclosed in the < CPAReferenceNumber> tag.
I'm using Excel and VBA to read the files and change the relevant field. The reference is set to a default value (in the code below it changes 'This' into 'That' for now)
This code works on one individual file and makes the correct change. The files have random names and there is no naming convention in place. I'm unable to expand this out to all XML files in the folder. Any help would be greatly appreciated.
Sub ReplaceStringInFile()
Dim sBuf As String
Dim sTemp As String
Dim iFileNum As Integer
Dim sFileName As String
sFileName = "c:\Search and Replace Files\blah.XML"
iFileNum = FreeFile
Open sFileName For Input As iFileNum
Do Until EOF(iFileNum)
Line Input #iFileNum, sBuf
sTemp = sTemp & sBuf & vbCrLf
Loop
Close iFileNum
sTemp = Replace(sTemp, "THIS", "THAT")
iFileNum = FreeFile
Open sFileName For Output As iFileNum
Print #iFileNum, sTemp
Close iFileNum
End Sub
After Help I have modified it to the below code, this is however causing errors: sFileName is out of context. The text change is not applied to the xml files.
Sub ReplaceStringInFile()
Dim sBuf As String
Dim sTemp As String
Dim iFileNum As Integer
Dim sFileName As String
Const sSearchString As String = "c:\blah\*.xml"
Const directoryString As String = "c:\blah\"
iFileNum = FreeFile
sFileName = Dir(sSearchString)
Do While sFileName <> ""
Open directoryString & sFileName For Input As iFileNum
Do Until EOF(iFileNum)
Line Input #iFileNum, sBuf
sTemp = sTemp & sBuf & vbCrLf
Loop
Close iFileNum
sTemp = Replace(sTemp, "IDNUMBER", "******SUCCESS!!!!!!******")
iFileNum = FreeFile
Open sFileName For Output As iFileNum
Print #iFileNum, sTemp
Close iFileNum
Debug.Print "Do something with file named " & sFileName
sFileName = Dir()
Loop
End Sub