4 Replies - 1426 Views - Last Post: 24 May 2016 - 01:30 PM Rate Topic: -----

#1 malibu65k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-May 16

Replace text in MS-Word with an HTML Table

Posted 24 May 2016 - 05:58 AM

I've created an html table using ColdFusion to get the data from a query. I want to replace some text used as a place holder in a word document(actually rtf) with the table. I've been using the Replace() function to replace other text but I would like to know how to replace the text with a table. I would like it to look like the example below but with borders. Wondering if this is possible and if someone could offer some assistance. The data will be pulled from a query. Again, the table is created already, I only want to place it in a specific place on the document is why I was wanting to know if it's possible to use the Replace() Function and how I would go about to reference the table.

Name Class Instructor Day Time
Lisa 101 Jones Mon 1:00
Mark 101 Jones Mon 1:00
June 102 Miller Mon 2:00

Is This A Good Question/Topic? 0
  • +

Replies To: Replace text in MS-Word with an HTML Table

#2 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 2024
  • View blog
  • Posts: 3,609
  • Joined: 13-January 08

Re: Replace text in MS-Word with an HTML Table

Posted 24 May 2016 - 06:25 AM

Welcome to DIC malibu65k!

So, if you've already been using the Replace function you're most of the way there already. The code for a table is no different than a text string. Where you would do this where you replace "dog" with "cat"...

<cfset variables.textString = "The dog ate the food.">
<cfset variables.textString = Replace(variables.textString,'dog','cat')>



...you could insert a table instead of "cat":

<cfset variables.textString = "The dog ate the food.">
<cfset variables.tableToInsert = "<table style='border: 1px solid black;'><tr><td>mouse</td></tr></table>">
<cfset variables.textString = Replace(variables.textString,'dog',variables.tableToInsert)>



The thing to note there is that if you use double quotes to establish the content of your variable, you need to use single quotes inside the variables content (like what I did with the table's style attribute).

It's also easier, as I did in the example, to set up the table and its contents as its own variable that you then use as the substituting content for a Replace function. That way, as you build the page, you can dump out the table to the screen to see that it's built the way you want before inserting it into the document content via Replace.

Anyway, give that a spin and see if that doesn't get you where you want to be. Good luck!
Was This Post Helpful? 1
  • +
  • -

#3 malibu65k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-May 16

Re: Replace text in MS-Word with an HTML Table

Posted 24 May 2016 - 07:52 AM

Thanks for the response. I have tried your example. I agree that I am close and this is very helpful. However, the table is in just html tags. The data in my list are there from the query but not the headings and data and not in table form.

This is the code...

<cfset variables.tableToInsert ="<table style='width:35% border=2' >
                         <thead>
                               <tr>
                                   <cfloop list='#ArrayToList(qGetEnclSecData.getColumnNames())#' index='col' >
                                           <th><cfoutput>col</cfoutput></th>
                                   </cfloop>
                               </tr>
                         </thead>
                         <tbody>
                         <cfloop query = 'qGetEnclSecData'>
                               <tr>
                                   <cfloop list='#ArrayToList(qGetEnclSecData.getColumnNames())#' index='col' >
                                               <td>qGetEnclSecData[col][currentrow]</td>
                                   </cfloop>
                               </tr>
                         </cfloop> 
                         </tbody>   
                   </table>"> 


This is my result...

<table style='width:35% border=2' > <thead> <tr>
<cfloop list='Last Name,Class Name' index='col' > <th><cfoutput>col</cfoutput></th> </cfloop> </tr>
</thead> <tbody> <cfloop query = 'qGetEnclSecData'> <tr>
<cfloop list='Last Name,Class Name' index='col' >
<td>qGetEnclSecData[col][currentrow]</td> </cfloop> </tr>
</cfloop> </tbody> </table>

This works in the Browser...

<table border='2' style='width:35%'>
                         <thead>
                               <tr>
                                   <cfloop list='#ArrayToList(qGetEnclSecData.getColumnNames())#' index='col' >
                                           <th><cfoutput>#col#</cfoutput></th>
                                   </cfloop>
                               </tr>
                         </thead>
                         <tbody>
                         <cfloop query = 'qGetEnclSecData'>
                               <tr>
                                   <cfloop list='#ArrayToList(qGetEnclSecData.getColumnNames())#' index='col' >
                                               <td><cfoutput>#qGetEnclSecData[col][currentrow]#</cfoutput></td>
                                   </cfloop>
                               </tr>
                         </cfloop> 
                         </tbody>   
                   </table>


I get a table with borders, the headings and the data. I had to remove some of the # signs and quotes to get it to run and replace in the word document without errors but I suspect that may have something with the data not displaying. I'm going to continue to play around with it but any suggestions you can give me would be greatly appreciated.
Was This Post Helpful? 0
  • +
  • -

#4 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 2024
  • View blog
  • Posts: 3,609
  • Joined: 13-January 08

Re: Replace text in MS-Word with an HTML Table

Posted 24 May 2016 - 10:56 AM

So, first things first: to get a variable to appear in text string you do have to "pound" (#) it. So you will need to put the variable "col" inside pound signs (#col#) and that will, in turn, need to be within a <cfoutput> block. That should handle the building of your table column headers.

The second half though is a little more involved. You're using a CFLOOP to loop over your query result set and that can work but you're trying to do it using a currentrow attribute which is only available when you use CFOUTPUT with a query attribute...and you're not using that here. If you know the names of the column headers you're going to get back from your query then you can simply call them inside of a CFOUTPUT query loop. If you keep things the way they are here though (a nested loop series) then you'll need to replace your reference to currentrow with a manually incrementing variable that you'll need to explicitly build and interate for each outside loop series. I wouldn't use the word "currentrow" as the variable name either as that is a reserved word in some circumstances and why run the risk of it giving the CF server fits?

Short version: replace currentrow with a counter of your own building and pound all your variables and you should be good to go.
Was This Post Helpful? 1
  • +
  • -

#5 malibu65k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-May 16

Re: Replace text in MS-Word with an HTML Table

Posted 24 May 2016 - 01:30 PM

Thank you. I see what you're saying. Though it will require a little more thought for me to accomplish. I won't know the names of the columns or the number of columns being it loops through to get the table names and then gets the field list from another table. I tried using #col# with the <cfoutput> but I kept getting an error that the col was undefined which makes sense with your explanation. I think I am headed on the right track. I really appreciate the assistance.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1