-
I'm trying to replace a word in .rtf file by multiple values. I wrote a code which calculates how many values needs to be replaced and replaces them.
staffCount = frmProposalLetter.lstSelected.ListCount
For idIndex = 0 To staffCount - 1
strRef = frmProposalLetter.lstSelected.List(idIndex)
rtbCoverLetter = Replace(rtbCoverLetter, "#Reference#", strRef, staffCount, vbTextCompare)
Next
I thought that each time when it goes through the loop a new value is added as many times as the value of staffCount is but instead it saves just the last value.
Can anybody help?
Thank you!
-
you are going trough the loop of listbox
not trough the file in which you have to replace the values
-
do you know how to resolve it?
-
what is this value? rtbCoverLetter
Is it a variable that holds informationt where
"#Reference#" has to replace?
-
rtbCoverLetter is a RichText Box control in my VB app. User selects some values from a listbox1 to a listbox2 and clicks Preview button. It loads a .rtf file (a template with some text) into another form. Now a word "#Reference#" in .rtf file has to be replaced with values from listbox2. The problem is that it loops through all values and replaces it with the last value. Here is a code:
Private Sub Form_Load()
Dim staffIDs()
Dim staffCount
Dim idIndex
Dim strRef As String
ShowCentered Me
'Load the Template
rtbCoverLetter.LoadFile "e:\VB\Jcon\CoverLetter.rtf"
'Replace words in brackets with variables
rtbCoverLetter = Replace(rtbCoverLetter, "#Date#", strProposalDate)
rtbCoverLetter = Replace(rtbCoverLetter, "#Chron_Number#", strCor_Reference_Number)
staffCount = frmProposalLetter.lstSelected.ListCount
For idIndex = 0 To staffCount - 1
rtbCoverLetter = Replace(rtbCoverLetter, "#Reference#", (frmProposalLetter.lstSelected.List(idIndex)), 1, staffCount, vbTextCompare)
Next
rtbCoverLetter = Replace(rtbCoverLetter, "#Reference#", strRef)
rtbCoverLetter = Replace(rtbCoverLetter, "#Subject#", strTitle)
rtbCoverLetter = Replace(rtbCoverLetter, "#ACOTR#", strACOTR)
End Sub
-
Try to Figure this out
you where missing Selected Propetry of the list box
and loop througn richtextbox
staffCount = lstSelected.ListCount
For idIndex = 0 To staffCount - 1
If lstSelected.Selected(idIndex) = True Then
For i = 1 To Len(rtbCoverLetter.Text)
rtbCoverLetter.Text = Replace(rtbCoverLetter.Text, "#Reference#", _
(lstSelected.List(idIndex)), 1, staffCount, vbTextCompare)
Next
End If
Next
-
I don't need Selected Property of the listbox. I need ALL values from the list box.
-
For idIndex = 0 To staffCount - 1
rtbCoverLetter = Replace(rtbCoverLetter.Text, _"#Reference#", (lstSelected.List(idIndex)), 1, 1, vbTextCompare)
Next
you used staffCount and it was count of your listbox items
replace for 1
and will work
-
I'm wondering if you tested it. It doesn't work! It does exactly the same thing as before but slower and with much more code. Aslo it works only if one value in the listbox is selected. What if I need all values in the listbox? With your code it doesn't work!
-
Anna,
Did you get my email this morning (japan time) with what I thought was wrong?
-
Paul,
I got your email and send you an email back. The question is how to replace one #Reference# with multiple values from the listbox?
Thank you,
Anna
-
Of course I tried it and it works fine
'ShowCentered Me
'Load the Template
rtbCoverLetter.LoadFile "C:\Temp.txt"
'Replace words in brackets with variables
'rtbCoverLetter = Replace(rtbCoverLetter, "#Date#", strProposalDate)
'rtbCoverLetter = Replace(rtbCoverLetter, "#Chron_Number#", strCor_Reference_Number)
iPos = 1
staffCount = lstSelected.ListCount
For idIndex = 0 To staffCount - 1
rtbCoverLetter = Replace(rtbCoverLetter.Text, "Helen", _
***This where I changed staffCount for 1***
(lstSelected.List(idIndex)), 1, 1, vbTextCompare)
Next
'rtbCoverLetter = Replace(rtbCoverLetter, "#Reference#", strRef)
'rtbCoverLetter = Replace(rtbCoverLetter, "#Subject#", strTitle)
'rtbCoverLetter = Replace(rtbCoverLetter, "#ACOTR#", strACOTR)
-
Yes, it works fine if you wrote "Helen" 3 times in your .txt file. That's a straight forward situation. My question was how to replace it if you wrote "Helen" once in your ".txt" file but you have multiple values in the listbox. In real life it's a letter with a list of references and you never know how many references user wants to put there. My code worked fine when I was creating a ".txt" file and writing to a file but when I decided to use an ".rtf" template and Replace function it doesn't work.
-
i hope this time it will work
the problem is I dont get the whole picture.
are you trying to replase #Reference# with
multiple value from list box?
Hello #Replase# ok
has to look like
Hello one,two,three ok
if the values in list box are
one
two
three
staffCount = lstSelected.ListCount
'****************************************
'****************************************
Dim sValues As String
For idIndex = 0 To staffCount - 1
sValues = sValues & "," & lstSelected.List(idIndex)
Next
sValues = Left(sValues, Len(sValues) - 1)
rtbCoverLetter = Replace(rtbCoverLetter.Text, "#Reference#", _
sValues, 1, 1, vbTextCompare)
'****************************************************************
'****************************************************************
let me know if this what you need
what kind of obezyanka are you
monky or shinpadzi?
-
Thanks! It finally worked! I changed your code a little bit:
Dim sValues As String
rtbCoverLetter.LoadFile "C:\Temp.txt"
staffCount = lstSelected.ListCount
For idIndex = 0 To staffCount - 1
sValues = sValues & Chr(10) & lstSelected.List(idIndex)
Next
rtbCoverLetter = Replace(rtbCoverLetter.Text, "Helen", _
sValues, 1, 1, vbTextCompare)
*******************************
I'm just obezyanka. I guess, you speak Russian?
-
of cource
if you have any questions
[email protected]