3

I have a word document with tables laid out to look like a form. I have placeholders like %firstName%, %lastName%, %birthdate%...etc. When I use the replace() function, the %firstName%, %lastName%, %birthdate% and all of the other placeholder fields are replaced on the first and second page. After the second, nothing replaces. All the names of the placeholders on the 3rd and 4th pages are the same as the 1st and 2nd pages. I even copied and pasted the placeholder names and I've made sure there are no added spaced. Curious to know if anyone else has had this happen and can tell me what was done to fix it.

<cfset docPath =  GetDirectoryFromPath(GetCurrentTemplatePath()) & "UserTemplate.rtf" />        
<cflock name="UserTemp" type="exclusive" timeout="30">
    <cfset rtf = FileRead(docPath) />
    <cfquery name = "qUserFormData">
        SELECT * FROM vUserFormData WHERE UserID = 3
    </cfquery>
    <cfset rtf = Replace(rtf,"%firstName%",#firstName#)/>
    <cfset rtf = Replace(rtf,"%lastName%",#lastName#) />
    <cfset rtf = Replace(rtf,"%birthday%",#birthday#) />
</cflock>
<cfheader name="content-disposition" value="filename=UserTemplate.doc" />
<cfcontent type="application/msword"><cfoutput>#rtf#</cfoutput>
Leigh
  • 28,765
  • 10
  • 55
  • 103
malibu65k
  • 95
  • 6

1 Answers1

6

There is a fourth (optional) parameter to the replace() method; scope.

Scope:

one: replaces the first occurrence (default)
all: replaces all occurrences

Notice that "one" is the default and that only replaces the first occurrence. Try adding that fourth parameter like this:

<cfset rtf = Replace(rtf,"%firstName%",firstName,"all") />
<cfset rtf = Replace(rtf,"%lastName%",lastName,"all") />
<cfset rtf = Replace(rtf,"%birthday%",birthday,"all") />

(The hash tags # are not necessary in this bit of code.)

Also be aware that the replace() method you are using is case sensitive.

Leigh
  • 28,765
  • 10
  • 55
  • 103
Miguel-F
  • 13,450
  • 6
  • 38
  • 63