Tray this custom tag:
<!--- This code is a coldfusion custom tag.
tag name: cf_msword_replace
tag description: With this tag it is possible to find
and replace text strings in word documents.
tag requirements: This tag only works on win nt boxes
with an installed version of msword and an enabled cfobject tag. If you use
an old version of msword please look for a special msoffice servicepack on
the ms website.
tag sysntax:
<cf_msword_replace
*find="text_to_find"
*replace="replacement_text"
(the replace attribute is optional
if stFindAndReplace is used instead)
*open="filepath_and_filename"
save="filepath_and_Filename"
stFindAndReplace="structure_with_keys_to_replace"
replaceall="yes_or_no">
*Required parmeters.
The asterix is not part of the sysntax.
The tag save the word document with the same path and name
when you specify no save parameter.
copyright 2002 by Martin Orth of cfsolutions
contact: [EMAIL PROTECTED]
Simple Example:
<cf_msword_replace open="c:\docs\my_text.doc" find="Hello" replace="World"
save="c:\docs\my_text_new.doc">
Advanced Example:
Create a structure for replacement. So cf_msword_replace will replace the
key_names found in the word document with their values.
<cfset params=StructNew()>
<cfset params.My_company="cfsolutions">
<cfset params.My_contact="Martin Orth">
<cfset params.My_address1="Hornstrasse 127">
<cfset docpath_open="c:\inetpub\wwwroot\read\">
<cfset docpath_save="c:\inetpub\wwwroot\write\">
<cfset docname="blanko_invoice.doc">
<cfset savename="inv_"&Replace(getinfo.firma,"
","_","all")&"_"&LSDateFormat(getinfo.datum_von,"ddmmyyyy")&".doc">
<cf_msword_replace open="#docpath_open##docname#"
stFindAndReplace="#params#" save="#docpath_save##savename#"
replaceall="yes">
version: 1.1
date: 05.08.2001
last modification: 16.04.2002
version history:
1.0 beta 1 05.08.2001 it�s done; core functions are up and runing
1.1 list replacement function now works. So it is possible
to replace a list of text strings within a word document.
better error handling
--->
<!--- generate default parameters --->
<cfparam name="attributes.find" default="">
<cfparam name="attributes.replace" default="">
<cfparam name="attributes.stFindAndReplace" default="">
<cfparam name="attributes.open" default="">
<cfparam name="attributes.save" default="">
<cfparam name="attributes.replaceall" default="No">
<!--- look for the required parameters --->
<cfif (trim(attributes.find) is "" and not
IsStruct(attributes.stFindAndReplace)) or (trim(attributes.replace) is ""
and not IsStruct(attributes.stFindAndReplace)) or trim(attributes.open) is
"" or ((trim(attributes.find) is trim(attributes.replace)) and not
IsStruct(attributes.stFindAndReplace))>
There are missing tag parameters. Please take look at the tag sysntax for
required parameters.<p>
<b>tag sysntax:</b><p>
<cf_msword_replace <br>
<b>*</b>find="text_to_find" <br>
<b>*</b>replace="replacement_text"<br>
<b>*</b>open="filepath_and_filename"<br>
save="filepath_and_Filename"<br>
replaceall="yes_or_no"><p>
<b>*</b>Required parmeters. <br>
The asterix is not part of the sysntax.<p>
The find and the replace string can not be the same.<p>
The tag save the word document with the same path and name <br>
when you specify no save parameter.<p>
<cfexit>
</cfif>
<cftry>
<!--- Try to connect to the Word application object --->
<CFTRY>
<!--- If it exists, connect to it --->
<CFOBJECT
ACTION="CONNECT"
CLASS="Word.Application"
NAME="objWord"
TYPE="COM">
<CFCATCH>
<!--- The object doesn't exist, so create it --->
<CFOBJECT
ACTION="CREATE"
CLASS="Word.Application"
NAME="objWord"
TYPE="COM">
</CFCATCH>
</CFTRY>
<!--- This will open Word if running locally --->
<cfset objWord.Visible = false>
<!--- This returns the 'Documents' collection of the Word object --->
<cfset objDoc = objWord.Documents>
<!--- specify a document to open --->
<cfset newDoc = objDoc.open("#trim(attributes.open)#")>
<cfset ActDoc = newDoc.application>
<cfset ObjActDoc = ActDoc.ActiveDocument>
<cfif isdefined("attributes.stFindAndReplace") and
IsStruct(attributes.stFindAndReplace)>
<cfloop collection="#attributes.stFindAndReplace#" item="structkey">
<!--- helper variable for the condition loop --->
<cfset search_or_die=1>
<!--- main find and replace loop --->
<cfloop condition="search_or_die is 1">
<!--- select the documents text and elements --->
<cfset temp0 = ObjActDoc.select()>
<cfset sel_pr = ActDoc.selection>
<!--- find the text --->
<cfset fd = sel_pr.find>
<cfset fd.text = "#structkey#">
<cfset temp01 = fd.Execute()>
<!--- replace the string or stop the loop if nothing was found --->
<cfif trim(fd.found) is "YES">
<cfset sel_pr.text="#attributes.stFindAndReplace[structkey]#">
<cfelse>
<cfset search_or_die=0>
</cfif>
<!--- stop the loop if attributes.replaceall is No --->
<cfif trim(attributes.replaceall) is "No">
<cfset search_or_die=0>
</cfif>
</cfloop>
</cfloop>
<cfelse>
<cfset ActDoc = newDoc.application>
<cfset ObjActDoc = ActDoc.ActiveDocument>
<!--- helper variable for the condition loop --->
<cfset search_or_die=1>
<!--- main find and replace loop --->
<cfloop condition="search_or_die is 1">
<!--- select the documents text and elements --->
<cfset temp0 = ObjActDoc.select()>
<cfset sel_pr = ActDoc.selection>
<!--- find the text --->
<cfset fd = sel_pr.find>
<cfset fd.text = "#attributes.find#">
<cfset temp01 = fd.Execute()>
<!--- replace the string or stop the loop if nothing was found --->
<cfif trim(fd.found) is "YES">
<cfset sel_pr.text="#attributes.replace#">
<cfelse>
<cfset search_or_die=0>
</cfif>
<!--- stop the loop if attributes.replaceall is No --->
<cfif trim(attributes.replaceall) is "No">
<cfset search_or_die=0>
</cfif>
</cfloop>
</cfif>
<!--- Save the changes --->
<cfif trim(attributes.save) is "">
<cfset temp02 = newDoc.SaveAs("#attributes.open#")>
<cfelse>
<cfset temp03 = newDoc.SaveAs("#attributes.save#")>
</cfif>
<!--- Close the document --->
<cfset temp04 = newDoc.Close()>
<!--- Quit Word --->
<cfset temp05 = objWord.Quit()>
<cfcatch>
An error has occured while executing the cf_msword_replace tag.<p>
<cfoutput>#cfcatch.Message# #cfcatch.Detail#</cfoutput>
<cfexit>
</cfcatch>
</cftry>
______________________________________________________________________
Signup for the Fusion Authority news alert and keep up with the latest news in
ColdFusion and related topics. http://www.fusionauthority.com/signup.cfm
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists