Results 1 to 16 of 16

Thread: RTF Replace Function

  1. #1
    Guest

    Question

    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!

  2. #2
    Guest
    you are going trough the loop of listbox
    not trough the file in which you have to replace the values

  3. #3
    Guest
    do you know how to resolve it?

  4. #4
    Guest
    what is this value? rtbCoverLetter
    Is it a variable that holds informationt where
    "#Reference#" has to replace?

  5. #5
    Guest
    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


  6. #6
    Guest
    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

  7. #7
    Guest
    I don't need Selected Property of the listbox. I need ALL values from the list box.

  8. #8
    Guest
    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

  9. #9
    Guest
    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!

  10. #10
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    Anna,

    Did you get my email this morning (japan time) with what I thought was wrong?

    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  11. #11
    Guest
    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

  12. #12
    Guest
    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)

  13. #13
    Guest
    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.

  14. #14
    Guest
    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?


  15. #15
    Guest
    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?

  16. #16
    Guest
    of cource
    if you have any questions
    [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width