Hello All,
I could really use some help with this problem its driving me nuts. I have a small vb app, the goal of the app is to read an html doc into a variable then go through that variable and find and replace some tags. I have 3 functions. 1 to open the doc, the 2nd to find and replace the tags the 3rd to save the info. the code is pasted below :
now here is the problem. When i hit the messagebox statements the variable is displayed in the way i want it to be. yet when i step through the logic before the call to ReplaceText when i check the value of sFrom1 and sTo1 there are extra quotes and things that arent allowing the string to be found and thus not replaced. I would like to know how to pass the variable into the function in the way its being displayed as opposed to this other way with all the additional quotes.
all help is appreciated im stuck here. I would like someone to help me but also explain why im getting this problem in the solution. I dont just want an answer i would like to learn as well.
thanks in advance,
john
I could really use some help with this problem its driving me nuts. I have a small vb app, the goal of the app is to read an html doc into a variable then go through that variable and find and replace some tags. I have 3 functions. 1 to open the doc, the 2nd to find and replace the tags the 3rd to save the info. the code is pasted below :
Code:
Public Function ReadFileContents(FileFullPath As String) As _ String On Error GoTo ErrorHandler Dim iFileNumber As Integer Dim sAns As String If Dir(FileFullPath) = "" Then Exit Function iFileNumber = FreeFile Open FileFullPath For Input As #iFileNumber sAns = Input(LOF(iFileNumber), #iFileNumber) sAns = UCase(sAns) ReadFileContents = sAns 'MsgBox sAns ErrorHandler: Close #iFileNumber End Function
Code:
Public Function ReplaceText(ByVal txt As String, ByVal _
from_str As String, ByVal to_str As String) As String
Dim result As String
Dim from_len As Integer
Dim pos As Integer
from_len = Len(from_str)
Do While Len(txt) > 0
' Find level 1.
pos = InStr(txt, from_str)
If pos = 0 Then
' No more occurrences.
result = result & txt
txt = ""
Else
' Make the replacement.
result = result & Left$(txt, pos - 1) & to_str
txt = Mid$(txt, pos + from_len)
End If
Loop
ReplaceText = result
End Function
Code:
Public Function SaveTextToFile(FileFullPath As String, _
sText As String, Optional Overwrite As Boolean = True) As _
Boolean
On Error GoTo ErrorHandler
Dim iFileNumber As Integer
iFileNumber = FreeFile
If Overwrite Then
Open FileFullPath For Output As #iFileNumber
Else
Open FileFullPath For Append As #iFileNumber
End If
Print #iFileNumber, sText
SaveTextToFile = True
ErrorHandler:
Close #iFileNumber
End Function
Code:
Private Sub Form_Load()
'****************************************************************
'Define variables for strings
Dim sFileText As String
Dim sFrom1 As String, sTo1 As String
'****************************************************************
'Open the HTML file as text
sFileText = ReadFileContents("c:\toc\toc.htm")
'****************************************************************
'In this section you should setup the strings you are searching
'The document for. You should also setup the replacement string
'If the above named strings are found
'****************************************************************
sFrom1 = "<img src=" + Chr(34) + "space.gif" + Chr(34)
MsgBox sFrom1
sTo1 = "<div class=" + Chr(34) + "toc1" + Chr(34) + "><img src=" + Chr(34) + "space.gif" + Chr(34)
MsgBox sTo1
Call ReplaceText(sFileText, sFrom1, sTo1)
Call SaveTextToFile("c:\toc\toc.htm", sFileText, True)
End Sub
all help is appreciated im stuck here. I would like someone to help me but also explain why im getting this problem in the solution. I dont just want an answer i would like to learn as well.
thanks in advance,
john
Comment