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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing a RTF to an Array with issues

Status
Not open for further replies.

rnpIII

Technical User
May 2, 2001
61
US
Hello All,

I am trying to read a .RTF file and make changes to three lines.
Originally tried just writing to a variable with the changed lines, but the file is so large the computer will hang on this file for quite awhile. When it is reading the rest of the file, and that is before it has actually written the new file.

Decided to write it to an array and use replace to break out the new stuff.
Seemed like a good idea, I am wondering if I am running out of space in the array or my memory.
I am getting "Microsoft VBScript runtime error: Subscript out of range: 'i'", but first I get "The storage control block address is invalid".
Please find code below and many thanks to any assistance to this issue.

Code:
Sub GetSignatureFile
	
	UName = "Test"
	UPhone = "940-321-1234"
	UTitle = "Someone who works here"
		
	s = sline & vbCrLf
	Set Folder = ofso.GetFolder(SourceDir)
	For Each file In Folder.Files
			If SignatureName &".rtf" = File.Name Then
				Set ots = ofso.OpenTextFile (File, ForReading)
				Do While Not ots.AtEndOfStream
					sline = ots.readall
				Loop
				ots.close

				RTFAr = Split (sline, vbCrLf)
				For i = 0 to Ubound(RTFAr)	
					'WScript.Echo RTFAr(i)			
				    If RTFAr(i) = RTFName Then				    				    	
				    	RTFAr(i) = Replace (RTFAr(i), "Name", UName)
				    ElseIf RTFAr(i) = RTFTitle Then
				    	RTFAr(i) = Replace (RTFAr(i), "Job Title", UTitle)
				    ElseIf RTFAr(i) = RTFPhone Then
				    	RTFAr(i) = Replace (RTFAr(i), "940-xxx-xxx", UPhone)
				    	Exit For
				    End If
				    WScript.Echo RTFAr(i)
				    ReDim Preserve RTFAr(i)
				Next				
			End If
	Next
End Sub

rnpIII
CoServ IT
 
[1] It seems you have not much reason to enumerate file. Just try to get it with on error resume next. If it error out, the file does not exist.
[tt]
on error resume next
Set ots = ofso_OpenTextFile(SourceDir & "\" & SignatureName &".rtf", ForReading)
if err.number<>0 then
'you don't have that file there
'what ever you need to do in this case here
err.clear
else
'you main work here.
end if
on error goto 0
[/tt]
[2] In the main working part, you should not use the loop .atendofstream but within which you use readall. Simply readall and that's it.
[tt]
[red]'[/red]Do While Not ots.AtEndOfStream
sline = ots.readall
[red]'[/red]Loop
ots.close
[/tt]
[3] To find a semantic line from .rtf, you do not split vbcrlf. The line break seems to be [tt]"\par[highlight] [/highlight]"[/tt]. Hence, at first instance, I would start looking into this.
[tt] RTFAr = Split (sline, [red]"\par "[/red])[/tt]
In any case, you should have your rtf format spec stand-ready for editing and replacing.

[4] Make sure different cases would not happen on a single "line", otherwise, the logic seems insufficient. But, I have not time to read into it. Do take a careful look.

Just some notes for a casual look sofar at your script.
 
Hey Tsuji,

Thank you for your input.
I will ammend the script accordingly for some of it.
The "split" piece is tricky because each line I look at has a different ending character. Sometimes it's "}" or ";}" or something else totally.
I am looking at the rtf via notepad so I can see what the script would be seeing.
I would like your thoughts on an idea I just had.
The rtf file is mainly just a four liner with a JPG file.
The file size without the JPG is about 5kb, pretty managable, when we add the JPG image it jumps up to 3295kb or something like that.

What if I take the original part of the file without the JPG code in it and just search off of it then add the JPG code after the ammendments to the file have been done?
I have been working on this one for several days and have tried so many different things now.
Does that seem feasible to you?

Thanks once again for your response.
Oh, I changed up the script a little for readability, find it below:

Code:
Sub GetSignatureFile
	
	UName = "Test"
	UPhone = "940-321-1234"
	UTitle = "Someone who works here"
		
	Set Folder = ofso.GetFolder(SourceDir)
	For Each file In Folder.Files
	     If SignatureName &".rtf" = File.Name Then
	          Set ots = ofso.OpenTextFile (File, ForReading)
	          sline = ots.readall
	          ots.Close
                  ArrayIT sline
	     End If
	Next
End Sub

'----------------------------------------------------------------------------------------------------------------------
'----------------------------------------------------------------------------------------------------------------------
Sub ArrayIT(s)
	RTFAr = Array()
	RTFAr = Split (s, vbCrLf)
	For i = 0 to UBound (RTFAr)
	    If RTFAr(i) = RTFName Then			
	    	RTFAr(i) = Replace (RTFAr(i), "Name", UName)
	    ElseIf RTFAr(i) = RTFTitle Then
	    	RTFAr(i) = Replace (RTFAr(i), "Job Title", UTitle)
	    ElseIf RTFAr(i) = RTFPhone Then
	    	RTFAr(i) = Replace (RTFAr(i), "940-xxx-xxx", UPhone)
	    	Exit For
	    End If
	    WScript.Echo RTFAr(i)
	    ReDim Preserve RTFAr(i)
	Next				
End Sub

rnpIII
CoServ IT
 
Hey Tsuji,
I figured it out.
Not the most elegant method I'm sure, but it does the trick and works really fast.

in summary, after breaking out the GIF from the original file and putting that code in another file, I conducted my changes in the first array then broke out the GIF file in the second array with a "replace" and then "joined" it all together in the final stage.

Thanks for your suggestions, I really appreciate your work on this site.

Code:
Sub GetSignatureFile
WScript.Echo "Entering first sub"
	
	UName = "Test"
	UPhone = "940-321-1234"
	UTitle = "Someone who works here"
		
	Set Folder = ofso.GetFolder(SourceDir)
	For Each file In Folder.Files
			If SignatureName &".rtf" = File.Name Then
				Set ots = ofso.OpenTextFile (File, ForReading)
				ArrayIT ots, UName, UPhone, UTitle, file
			End If
	Next
End Sub

'----------------------------------------------------------------------------------------------------------------------
'----------------------------------------------------------------------------------------------------------------------
Sub ArrayIT(f, n, p, t, fn)
	i = 0
	RTFar = Array()
	RTFar = Split(f.ReadAll, vbNewLine)
	f.Close
	For Each Line In RTFar
		WScript.Echo Line
	    If RTFar(i) = RTFName Then				    				    	
	    	RTFar(i) = Replace (RTFar(i), "Name", n)
	    ElseIf RTFar(i) = RTFTitle Then
	    	RTFar(i) = Replace (RTFar(i), "Job Title", t)
	    ElseIf RTFar(i) = RTFPhone Then
	    	RTFar(i) = Replace (RTFar(i), "940-xxx-xxx", p)
	    	'Exit For
	    ElseIf RTFar(i) = "\par }}" Then
			Set fGIF = ofso.OpenTextFile(SourceDir &"\GIF info for RTF.txt", ForReading, False)
			GIFar = Split(fGIF.ReadAll, vbNewLine)	    
	    	RTFar(i) = Replace (RTFar(i), "\par }}", "\par "& Join(GIFar, vbCrLf) &"}}")
	    	Exit For
	    End If
	    i = i + 1
	Next
	Set File = ofso.OpenTextFile(fn, ForWriting, true)
	File.Write join(RTFAr, vbcrlf)
	file.Close
End Sub

rnpIII
CoServ IT
 
Glad you have your problem behind you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top