2 Replies Latest reply on Oct 10, 2008 11:33 AM by newRandomMark

    Word 2007 COM object

    newRandomMark
      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.
        • 1. Re: Word 2007 COM object
          Level 7
          The last time I had to deal with a requirement like this, which was
          about 2000-2001, we eventually scrapped the word COM thing. Even then
          it was such a fragile and perilous solution, fraught with problems and
          headaches.

          What we ended up doing, that was much easier, was to create a .rtf, rich
          text format, file. We could do all the required formating in this
          format, and 90% of the uses would have this file open in MS Word and not
          even know they did not get a .doc file.

          How we did it was create a .rtf template with dummy text that had all
          the desired formating. Then we could read in the .rtf file, which is
          just a marked-up text file, not a binary, and string replace the dummy
          text with the dynamic text. Save it again and viola a rich text
          formated file with the required content.

          These days, I would push for doing this in PDFs and use ColdFusion's
          <cfdocument...> and|or <cfpdf...> functionality. But if it has to be
          Word, I suppose it has to be Word.
          • 2. Re: Word 2007 COM object
            newRandomMark Level 1
            I looked at doing it in PDF. What I found was the field size remained the same as the field I input in originally. So if I have a name, like "My Name is" and the name was "This is a long name" it wouldn't work. I may have dismissed it early with out much research, and there maybe a work around, but that is what I found.

            I will look into the RTF solution.

            If anyone has any other input on this, I would appreciate it.