Question
Word 2007 COM object
I am having a heck of a time finding a good example of a word
COM object. I just upgraded from word 2000 to word 2007, in hopes
that would suddenly work for me (which honestly I didn't
expect...). In word 2000, this line worked fine, but not in 2007:
thisDoc.SaveAs("c:\MyDoc.doc");
Here is what I am trying to accomplish: I need to use values submitted by forms and input it into a word document. The word doc needs to have different paragraphs in different formats. I thought I could build the paragraphs somehow, with formatting with something like para1 = docrange.newparagraph(); then insert the text and formatting into para1 and then insert para1 into the document. It does not seem to be working at all. If someone can link me to a good example I can figure it out from there, but I don't seem to be able to find it. Here is my code so far:
<cfquery name="getAttorney" datasource="agreement">
Select * from Attorney where ID = #Form.attorney#
</cfquery>
<cfoutput query="getAttorney">
<cfset attornyInfo =
"#Name#
#email#
#Phone#">
</cfoutput>
<CFTRY>
<!--- If it exists, connect to it --->
<CFOBJECT
ACTION="CONNECT"
CLASS="Word.Application"
NAME="objWordApp"
TYPE="COM">
<CFCATCH>
<!--- The object doesn't exist, so create it --->
<CFOBJECT
ACTION="CREATE"
CLASS="Word.Application"
NAME="objWordApp"
TYPE="COM">
</CFCATCH>
</CFTRY>
<CFSCRIPT>
/* This will open Word if running locally */
objWordApp.Visible = true;
/* This returns the 'Documents' collection the Word Object */
thisDoc = objWordApp.Documents.add();
/* Save the document to a location */
thisDoc.SaveAs("c:\MyDoc.doc");
docRange = thisDoc.Range(0);
docRange.Style=-2;
docRange.InsertAfter("Header1");
docRange.InsertParagraphAfter();
docRange.Style=-3;
docRange.InsertAfter("I am the paragraph you requested");
docRange.InsertBreak();
docRange.InsertAfter("check me out... I'm on the second page!");
para1 = thisdoc.paragraphs.count();
data = docrange.listparagraphs();
data1 = docrange;
/* Save the changes */
thisDoc.Save();
/* Close the document */
thisDoc.Close();
/* Quit Word */
objWordApp.Quit();
</CFSCRIPT>
<cfdump var="#para1#">
<cfdump var="#data#">
<cfdump var="#data1#">
The dumps do dump information on the related objects, giving me methods, puts and gets, but I need more explantion than what is actually provided. Para1 used to dump "1" even though I had more paragraphs than that in the build. Since I upgraded to 2007, I have not gotten it not to error out, I will try to save it as a docx, but a doc is really more appropiate for my purposes.
Thank you for your time.
thisDoc.SaveAs("c:\MyDoc.doc");
Here is what I am trying to accomplish: I need to use values submitted by forms and input it into a word document. The word doc needs to have different paragraphs in different formats. I thought I could build the paragraphs somehow, with formatting with something like para1 = docrange.newparagraph(); then insert the text and formatting into para1 and then insert para1 into the document. It does not seem to be working at all. If someone can link me to a good example I can figure it out from there, but I don't seem to be able to find it. Here is my code so far:
<cfquery name="getAttorney" datasource="agreement">
Select * from Attorney where ID = #Form.attorney#
</cfquery>
<cfoutput query="getAttorney">
<cfset attornyInfo =
"#Name#
#email#
#Phone#">
</cfoutput>
<CFTRY>
<!--- If it exists, connect to it --->
<CFOBJECT
ACTION="CONNECT"
CLASS="Word.Application"
NAME="objWordApp"
TYPE="COM">
<CFCATCH>
<!--- The object doesn't exist, so create it --->
<CFOBJECT
ACTION="CREATE"
CLASS="Word.Application"
NAME="objWordApp"
TYPE="COM">
</CFCATCH>
</CFTRY>
<CFSCRIPT>
/* This will open Word if running locally */
objWordApp.Visible = true;
/* This returns the 'Documents' collection the Word Object */
thisDoc = objWordApp.Documents.add();
/* Save the document to a location */
thisDoc.SaveAs("c:\MyDoc.doc");
docRange = thisDoc.Range(0);
docRange.Style=-2;
docRange.InsertAfter("Header1");
docRange.InsertParagraphAfter();
docRange.Style=-3;
docRange.InsertAfter("I am the paragraph you requested");
docRange.InsertBreak();
docRange.InsertAfter("check me out... I'm on the second page!");
para1 = thisdoc.paragraphs.count();
data = docrange.listparagraphs();
data1 = docrange;
/* Save the changes */
thisDoc.Save();
/* Close the document */
thisDoc.Close();
/* Quit Word */
objWordApp.Quit();
</CFSCRIPT>
<cfdump var="#para1#">
<cfdump var="#data#">
<cfdump var="#data1#">
The dumps do dump information on the related objects, giving me methods, puts and gets, but I need more explantion than what is actually provided. Para1 used to dump "1" even though I had more paragraphs than that in the build. Since I upgraded to 2007, I have not gotten it not to error out, I will try to save it as a docx, but a doc is really more appropiate for my purposes.
Thank you for your time.
