jaguardesigns 0 Newbie Poster

Hi
My application is running on Windows Server 2003, Coldfusion MX 7, IIS6.
I am trying to process a mail merge from a SQL Datasource to MS Word using cfobject and com.
Everything processes fine, the merged doc is created and saved to the hard drive, however, as soon as the cfscript tags in the cfloop are processed the server encounters an error and JRun closes.
The exact error is
"Server Error
The server encountered an internal error and was unable to complete your request.
JRun closed connection."
If I remove the cfscript code from the cfloop the file is created successfully.
The cfscript code is processed to change special characters in the word document e.g. bold some text in a string, underline or italic etc.

I have tried to debug this code and it appears that the call to the following code causes the server error: docRep = docFind.Replacement;

Hope you can help!

<cfparam name="Error" default="False"> 
  
<cfscript> 
        Message         = ''; 
        GotObject       = false; 
  
</cfscript> 
  
<!--- setup the merge doc params ---> 
  
<cfset DocToOpen = "/Uploads/Results/LotFinal_Stamps_Template.doc"> 
<cfset DocToSave = "/Uploads/Results/LotFinal_Stamps.doc"> 
  
<cfif FileExists(ExpandPath(DocToSave))> 
        <cffile action="DELETE" file="#FileExists(ExpandPath(DocToSave))#"> 
</cfif> 
  
<cfset structdelete(application, "Objword")> 
  
<cfset start = GetTickCount()> 
  
<!--- create the object ---> 
  
<cftry> 
  
        <cfobject 
                type="COM" 
                name="Application.objWord" 
                class="Word.Application.10" 
                action="CONNECT" 
                context="LOCAL"> 
  
        <cfcatch> 
                 
                <cfobject 
                        type="COM" 
                        name="Application.objWord" 
                        class="Word.Application.10" 
                        action="CREATE" 
                        context="LOCAL"> 
  
        </cfcatch> 
  
</cftry> 
  
<cflock timeout="20" throwontimeout="no" type="exclusive" scope="application"> 
        <cfscript> 
                /* This will open Word if running locally */ 
                Application.objWord.Visible = 0; 
                Application.objWord.DisplayAlerts = 0; 
                Application.objWord.AutomationSecurity = 1; 
                Application.objWord.DefaultSaveFormat = ''; 
                /* This returns the 'Documents' collection of the Word object */ 
                objDoc = Application.objWord.Documents; 
        </cfscript> 
         
        <cfif Application.objWord.Visible NEQ ""> 
                <cfset GotObject = True> 
        </cfif> 
         
        <cfif NOT GotObject> 
                <cfset Message ="<b>Error:</b> Could not create Word Object on ColdFusion Server. Aborted...<br></cfoutput>"> 
                <cfoutput>#Message#</cfoutput> 
                <cfabort> 
        </cfif> 
         
        <!--- Specify the Merge Document to open and trap errors---> 
        <cftry> 
                <cfset newDoc = objDoc.Open("#ExpandPath(DocToOpen)#", Val(0), Val(-1), Val(0))> 
                <cfcatch type="Object"> 
                        <cfset Message="<b>Error:<br>#cfcatch.Message#:-</b><br>File: #ExpandPath(DocToOpen)#<br><p>#cfcatch.Detail#<br>"> 
                        <cfset newDoc = ""> 
                </cfcatch> 
        </cftry> 
         
        <cfscript> 
                newDoc.Activate(); 
                newDoc.EmbedLinguisticData = False; 
                newDoc.EmbedSmartTags = False; 
                newDoc.EmbedTrueTypeFonts = False; 
        </cfscript> 
         
        <cfset ProcessTimerStart = GetTickCount()> 
         
        <cfscript>               
                objMerge = newDoc.MailMerge; 
                objMerge.Destination = 0; 
                objMerge.MainDocumentType = 3; 
                objMerge.SuppressBlankLines = True; 
                //mDS = objMerge.DataSource; 
                 
                /* Do the Mail Merger */ 
                //mDS.FirstRecord = 1; 
                //mDS.LastRecord = mDS.RecordCount; 
                objMerge.Execute(); 
        </cfscript> 
  
        <cfloop collection="#objDoc#" item="thisDoc"> 
                 
                <cfscript> 
                        // Do Bolding 
                        IsFound = True; 
                        docSel = Application.objWord.Selection; 
                        docFont = docSel.Font; 
                        docFind = docSel.Find; 
                        docRep = docFind.Replacement; 
                        docSel.HomeKey(6); 
                        while (IsFound)  { 
                                docFind.ClearFormatting(); 
                                docFind.Text = "~B~*~b~"; 
                                docRep.Text = ""; 
                                docFind.Forward = -1; 
                                docFind.Wrap = 0; 
                                docFind.Format = 0; 
                                docFind.MatchCase = -1; 
                                docFind.MatchWholeWord = -1; 
                                docFind.MatchAllWordForms = 0; 
                                docFind.MatchSoundsLike = 0; 
                                docFind.MatchWildcards = -1; 
                                docFind.Execute(); 
                                // Bold the Found Text items 
                                docFont.Bold = -1; 
                                if (docFind.Found EQ 0) { 
                                        IsFound = False; 
                                } 
                        } 
                        // Remove the ~B~ and ~b~ characters 
                        docFind.ClearFormatting(); 
                        docRep.ClearFormatting(); 
                        docSel.HomeKey(6); 
                        docFind.Execute("~b~",0,0,0,0,0,-1,1,0,"",2,0,0,0,0); 
                        // Do Italics 
                        IsFound = True; 
                        docSel.HomeKey(6); 
                        while (IsFound)  { 
                                docFind.ClearFormatting(); 
                                docFind.Text = "~I~*~i~"; 
                                docRep.Text = ""; 
                                docFind.Forward = -1; 
                                docFind.Wrap = 0; 
                                docFind.Format = 0; 
                                docFind.MatchCase = -1; 
                                docFind.MatchWholeWord = -1; 
                                docFind.MatchAllWordForms = 0; 
                                docFind.MatchSoundsLike = 0; 
                                docFind.MatchWildcards = -1; 
                                docFind.Execute(); 
                                // Italicise the Found Text items 
                                docFont.Italic = -1; 
                                if (docFind.Found EQ 0) { 
                                        IsFound = False; 
                                } 
                        } 
                        // Remove the ~I~ and ~i~ characters 
                        docFind.ClearFormatting(); 
                        docRep.ClearFormatting(); 
                        docSel.HomeKey(6); 
                        docFind.Execute("~i~",0,0,0,0,0,-1,1,0,"",2,0,0,0,0); 
                        // Do Foreign Word Italics 
                        IsFound = True; 
                        docSel.HomeKey(6); 
                        while (IsFound)  { 
                                docFind.ClearFormatting(); 
                                docFind.Text = "~F~*~F~"; 
                                docRep.Text = ""; 
                                docFind.Forward = -1; 
                                docFind.Wrap = 0; 
                                docFind.Format = 0; 
                                docFind.MatchCase = -1; 
                                docFind.MatchWholeWord = -1; 
                                docFind.MatchAllWordForms = 0; 
                                docFind.MatchSoundsLike = 0; 
                                docFind.MatchWildcards = -1; 
                                docFind.Execute(); 
                                // Italicise the Found Text items 
                                docFont.Italic = -1; 
                                if (docFind.Found EQ 0) { 
                                        IsFound = False; 
                                } 
                        } 
                        // Remove the ~F~ characters 
                        docFind.ClearFormatting(); 
                        docRep.ClearFormatting(); 
                        docSel.HomeKey(6); 
                        docFind.Execute("~f~",0,0,0,0,0,-1,1,0,"",2,0,0,0,0); 
                        // Do Underlining 
                        IsFound = True; 
                        docSel.HomeKey(6); 
                        while (IsFound)  { 
                                docFind.ClearFormatting(); 
                                docFind.Text = "~U~*~u~"; 
                                docRep.Text = ""; 
                                docFind.Forward = -1; 
                                docFind.Wrap = 0; 
                                docFind.Format = 0; 
                                docFind.MatchCase = -1; 
                                docFind.MatchWholeWord = -1; 
                                docFind.MatchAllWordForms = 0; 
                                docFind.MatchSoundsLike = 0; 
                                docFind.MatchWildcards = -1; 
                                docFind.Execute(); 
                                // Underline the Found Text items 
                                docFont.Underline = -1; 
                                if (docFind.Found EQ 0) { 
                                        IsFound = False; 
                                } 
                        } 
                        // Remove the ~U~ and ~u~ characters 
                        docFind.ClearFormatting(); 
                        docRep.ClearFormatting(); 
                        docSel.HomeKey(6); 
                        docFind.Execute("~u~",0,0,0,0,0,-1,1,0,"",2,0,0,0,0); 
                </cfscript>              
                 
                <cfset ProcessTimerStop = GetTickCount()> 
                <cfset TotalProcessTime = ProcessTimerStop - ProcessTimerStart> 
                <!--- <cfoutput>Merge and Formating time: #TotalProcessTime# ms<br><br></cfoutput> ---> 
                <cftry> 
                        <cfif FileExists(ExpandPath(DocToSave))> 
                                <cffile action="DELETE" file="#FileExists(ExpandPath(DocToSave))#"> 
                        </cfif> 
                        <cfcatch type="any"> 
                                <cfoutput><br>#cfcatch.Message#<br>#cfcatch.Detail#<br></cfoutput> 
                        </cfcatch> 
                </cftry> 
                <cftry> 
                        <cfset SA = thisDoc.SaveAs("#ExpandPath(DocToSave)#")> 
                        <cfcatch type="any"> 
                                <cfset Message ="<b>Error:<br>#cfcatch.Message#</b><br><p>#cfcatch.Detail#<br>"> 
                                <cfset Error = True> 
                        </cfcatch> 
                </cftry> 
                <cfoutput> 
                        <p align="center"> 
                                <b>Lot Final Stamps :: Word Document</b><br /> 
                                <br /> 
                                <a href="#DocToSave#" target="_blank">Click here to open.</a> 
                                <br/> 
                                <em>Report Complete</em><br /> 
                                <em>Execution Time: #int((getTickCount()-start)/1000)# Seconds</em><br /> 
                        </p> 
                </cfoutput> 
                <cfset CL = thisDoc.Close(0)> 
                <cfexit>         
        </cfloop> 
</cflock> 
<cfset Application.objWord.Quit()> 
<cfabort>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.