7 Replies Latest reply: Aug 2, 2010 5:38 AM by 788138 RSS

    Problem entering values in table after creating it in Word using OLE2

    smilingbandit
      Hi,

      I want to create a Word document from a Oracle Forms 6i application. The document will have standard text followed by a table. The cells of this table are to be populated from fields in the Forms application.

      I have created a sample code below. When executed from within the Forms application, it creates a document fine. It writes the text "Hello World". It then creates a table as expected. But when I try to enter a value "Cell Value" in the table, the value does not get written to the cell of the table. Instead it writes the value outside the table.

      Can someone please tell me what I am doing wrong here and what I need to do so that after writing the text on the document I can write values into the table also.

      Any help will be much appreciated.

      Here is the code(it works but then it doesn't ):

      PROCEDURE TEST_PROC IS

      hApplication OLE2.OBJ_TYPE;
      hDocuments OLE2.OBJ_TYPE;
      hDocument OLE2.OBJ_TYPE;
      hSelection OLE2.OBJ_TYPE;
      hParagraphFormat OLE2.OBJ_TYPE;
      hRange OLE2.OBJ_TYPE;
      hFont OLE2.OBJ_TYPE;
      hTables OLE2.OBJ_TYPE;
      hTable OLE2.OBJ_TYPE;

      args OLE2.LIST_TYPE;

      wdAlignParagraphLeft CONSTANT number(3) := 0; --Default
      wdword9tablebehavior CONSTANT NUMBER (5) := 1;
      wdautofitfixed CONSTANT NUMBER (5) := 0;
      ---- wdUnits Class members
      wdcharacter CONSTANT NUMBER (5) := 1;
      wdmove CONSTANT NUMBER (5) := 0; --Default

      --wdBorderType Class members
      wdborderleft CONSTANT NUMBER := -2;
      wdborderright CONSTANT NUMBER := -4;
      wdbordertop CONSTANT NUMBER := -1;
      wdborderbottom CONSTANT NUMBER := -3;
      --WdLineStyle Class member
      wdlinestylenone CONSTANT NUMBER := 0;


      BEGIN
      hApplication:=OLE2.CREATE_OBJ('Word.Application');
      OLE2.SET_PROPERTY(hApplication, 'Visible', 1);

      hDocuments := OLE2.GET_OBJ_PROPERTY(hApplication, 'Documents');
      hDocument := OLE2.INVOKE_OBJ(hDocuments, 'Add');
      hSelection := OLE2.GET_OBJ_PROPERTY(hApplication, 'Selection');
      hFont := OLE2.GET_OBJ_PROPERTY(hSelection, 'Font');
      OLE2.SET_PROPERTY(hFont, 'Name', 'Calibri');
      OLE2.SET_PROPERTY(hFont, 'Size', 11);
      OLE2.SET_PROPERTY(hFont, 'Bold', FALSE );
      OLE2.INVOKE(hSelection, 'TypeParagraph');
      hParagraphFormat := OLE2.GET_OBJ_PROPERTY(hSelection, 'ParagraphFormat');
      OLE2.SET_PROPERTY(hParagraphFormat, 'Alignment', wdAlignParagraphLeft);
      OLE2.RELEASE_OBJ(hParagraphFormat);
      args := OLE2.CREATE_ARGLIST;
      OLE2.ADD_ARG(args, 'Hello World');
      OLE2.INVOKE(hSelection, 'TypeText', args);
      OLE2.DESTROY_ARGLIST(args);
      OLE2.INVOKE(hSelection, 'TypeParagraph');


      htables := ole2.get_obj_property (hdocument, 'Tables');
      hrange := ole2.get_obj_property (hselection, 'Range');
      args := ole2.create_arglist;
      ole2.add_arg_obj (args, hrange); --Range
      ole2.add_arg (args, 1); --NumRows
      ole2.add_arg (args, 6); --NumColumns
      ole2.add_arg (args, wdword9tablebehavior); --DefaultTableBehavior
      ole2.add_arg (args, wdautofitfixed); --FitBehavior
      htable := ole2.invoke_obj (htables
      ,'Add'
      ,args
      );
      args := ole2.create_arglist;
      ole2.add_arg (args, wdcharacter);
      ole2.add_arg (args, 1);
      ole2.add_arg (args, wdmove);
      ole2.invoke (hselection
      ,'MoveLeft'
      ,args
      );
      ole2.destroy_arglist (args);
      args := ole2.create_arglist;
      ole2.add_arg (args, 'CellValue');
      ole2.invoke (hselection
      ,'TypeText'
      ,args
      );
      ole2.destroy_arglist (args);
      ole2.RELEASE_OBJ (happlication);
      ole2.RELEASE_OBJ (hdocuments);
      ole2.RELEASE_OBJ (hdocument);
      ole2.RELEASE_OBJ (hfont);
      ole2.RELEASE_OBJ (hparagraphformat);
      ole2.RELEASE_OBJ (htables);
      ole2.RELEASE_OBJ (hrange);

      END;
        • 1. Re: Problem entering values in table after creating it in Word using OLE2
          John Roberts
          This is how I've done it in the past:

          Create a word document as a template including template text and tables.
          Create word bookmarks where you want to insert text programatically - eg. the 1st cell in you table.

          In your form, open the template, navigate to your bookmarks and insert your text.
          For tables, use "MoveRight" to cycle through each of the table cells in a loop.
          When finished, "SaveAs" your new document.
          • 2. Re: Problem entering values in table after creating it in Word using OLE2
            smilingbandit
            Hi John,

            Thanks for your post. Unfortunately I do not have much experience using ole2 and am still in the learning phase.
            Do you have some sample code that I can use? I don't know how to use ole2 to create a word document as a template or template text.
            • 3. Re: Problem entering values in table after creating it in Word using OLE2
              John Roberts
              Here's a simple example. This forum and Google have many better OLE2 examples.

              In MS Word, create a document with some boilerplate text and a table. Add bookmarks where you want to add your database text. You'll need to look at the Word help for how to do this.

              Account Number: +<BM1>+

              --------------------+
              Product Quantity
              --------------------+
              +<BM2>+
              --------------------+

              --------------------+

              --------------------+

              --------------------+

              Save the document as C:\MyDoc.doc

              In your form, open the document using OLE2, navigate to the bookmarks and put your text there.

              hApplication := ole2.create_obj ('Word.Application');
              ole2.set_property (hApplication, 'Visible', 1);
              hDocuments := ole2.get_obj_property (hApplication, 'Documents');
              args := ole2.create_arglist;
              ole2.add_arg(args, 'C:\MyDoc.doc');
              hDocument :=ole2.invoke_obj (hDocuments, 'Open', args);
              hSelection := ole2.get_obj_property(hApplication, 'Selection');

              ((select account_number into l_acc_no from customer_table where blah, blah, blah))

              -- Go to the 1st bookmark

              args := ole2.create_arglist;
              ole2.add_arg (args, -1);
              ole2.add_arg (args, 0);
              ole2.add_arg (args, 0);
              ole2.add_arg (args, 'BM1'); -- the name you gave your 1st bookmark in your template
              ole2.invoke (hSelection,'GoTo',args);
              ole2.destroy_arglist (args);
              ole2.set_property (hSelection, 'Text', l_acc_no); -- Print your text value

              -- Go to the 2nd bookmark on the line below your column headings in the 1st empty cell of your table

              args := ole2.create_arglist;
              ole2.add_arg (args, -1);
              ole2.add_arg (args, 0);
              ole2.add_arg (args, 0);
              ole2.add_arg (args, 'BM2'); -- the name you gave your 2nd bookmark in your template.
              ole2.invoke (hSelection,'GoTo',args);
              ole2.destroy_arglist (args);

              for x in ((select product,quantity from blah, blah, blah))
              loop
              ole2.set_property (hSelection, 'Text', x.product); -- Put text in 1st cell

              args := ole2.create_arglist;
              ole2.add_arg (args, 12);
              ole2.invoke (hSelection, 'MoveRight', args);
              ole2.destroy_arglist (args);

              ole2.set_property (hSelection, 'Text', x.quantity); -- Put text in 2nd cell

              args := ole2.create_arglist;
              ole2.add_arg (args, 12);
              ole2.invoke (hSelection, 'MoveRight', args); -- Will move to next line
              ole2.destroy_arglist (args);

              ole2.set_property (hSelection, 'Text', x.product); -- Put text in 3rd cell

              args := ole2.create_arglist;
              ole2.add_arg (args, 12);
              ole2.invoke (hSelection, 'MoveRight', args);
              ole2.destroy_arglist (args);

              ole2.set_property (hSelection, 'Text', x.quantity); -- Put text in 4th cell

              args := ole2.create_arglist;
              ole2.add_arg (args, 12);
              ole2.invoke (hSelection, 'MoveRight', args);
              ole2.destroy_arglist (args);
              end loop;

              -- Save the document as a new file

              hNewDocument := ole2.get_obj_property (hApplication, 'ActiveDocument');
              ole2.set_property (hNewDocument, 'ReadOnlyRecommended', -1);
              args := ole2.create_arglist;
              ole2.add_arg (args, 'C:\MyNewDoc.doc');
              ole2.invoke (hDocuments, 'SaveAs', args);
              ole2.destroy_arglist (args);

              -- Quit Word

              args := ole2.create_arglist;
              ole2.add_arg (args, 0);
              ole2.invoke (appl, 'QUIT', args);
              ole2.destroy_arglist (args);
              ole2.release_obj (hApplication);
              ole2.release_obj (hDocuments);
              ole2.release_obj (hSelection);
              • 4. Re: Problem entering values in table after creating it in Word using OLE2
                smilingbandit
                Once again thanks for the help John. Unfortunately I cannot use only bookmarks, because the text to be entered from the application is of undetermined length and therefore since the possibility of wrapping exists, it would not be able to give me the same formatting abilty as using a table.

                However, the good news is that I have been able to find the solution. I am posting my code here so that it is available to anyone else who might have the same issue. Thanks again.

                Here is my code and my comments in it explain what it does.

                PROCEDURE extract_to_word
                IS
                /* First of all I would like to acknowledge the help I have got from experts who have posted a lot of this code on the web which I have used here. Special thanks go to someone by the name of sfvb

                found here http://www.tek-tips.com/userinfo.cfm?member=sfvbsfvb

                This procedure does the following:
                1. Opens a word document.
                2. Creates a header and pastes a picture in the header e.g your company logo with address. Please note that the image should have both the address and the logo in it. It can be created using paint where you can write your address at one end and paste a bmp or jpg picture of your logo at the other end.
                3. Writes text with specified font and size. It calls a separate procedure PRINT_LINE to do this.
                4. Creates a table on the document with specified number of columns with fixed width.
                5. Populates the cells of the table-
                first with headers written by calls to WRITE_HEADINGS procedure.
                next with data from a block of the Forms application by calls to WRITE_DATA procedure
                6. Comes out of the table and continues to write text.

                The three procedures that are repeatedly called are described after the end of this procedure.

                Note: While I have made every attempt to ensure that the code does not generate error on account of my writing the explanatory notes, I cannot be sure of having made some typos. If the code does not compile, it is most probably be on account of something I have missed in entering the comments and explanation.

                Disclaimer:
                I am not the sole author of this code. I have taken help from other open sources and copyright infringement if any is entirely unintentional.

                */
                happlication ole2.obj_type;
                hwindow ole2.obj_type;
                hpane ole2.obj_type;
                hview ole2.obj_type;
                hdocuments ole2.obj_type;
                hdocument ole2.obj_type;
                hselection ole2.obj_type;
                hparagraphformat ole2.obj_type;
                hrange ole2.obj_type;
                hfields ole2.obj_type;
                hfont ole2.obj_type;
                hinlineshapes ole2.obj_type;
                hpars ole2.obj_type;
                hpar ole2.obj_type;
                htabstops ole2.obj_type;
                hactivedocument ole2.obj_type;
                htables ole2.obj_type;
                htable ole2.obj_type;
                hcolumns ole2.obj_type;
                hcells ole2.obj_type;
                hrows ole2.obj_type;
                hshading ole2.obj_type;
                hinsertrow ole2.obj_type;
                hborders ole2.obj_type;
                hshading ole2.obj_type;
                args ole2.list_type;
                wdalignparagraphleft CONSTANT NUMBER (3) := 0;
                wdalignparagraphcenter CONSTANT NUMBER (3) := 1;
                wdalignparagraphright CONSTANT NUMBER (3) := 2;
                wdseekcurrentpageheader CONSTANT NUMBER (3) := 9;
                wdseekcurrentpagefooter CONSTANT NUMBER (3) := 10;
                wdseekmaindocument CONSTANT NUMBER (3) := 0;
                wdfieldpage CONSTANT NUMBER (3) := 33;
                wdfieldnumpages CONSTANT NUMBER (3) := 26;
                wdpagebreak CONSTANT NUMBER (3) := 7;
                wdstory CONSTANT NUMBER (3) := 6;
                wdword CONSTANT NUMBER (5) := 2;
                wdsentence CONSTANT NUMBER (5) := 3;
                wdword8tablebehavior CONSTANT NUMBER (5) := 0;
                wdword9tablebehavior CONSTANT NUMBER (5) := 1;
                wdautofitcontent CONSTANT NUMBER (5) := 1;
                wdautofitfixed CONSTANT NUMBER (5) := 0;
                wdautofitwindow CONSTANT NUMBER (5) := 2;
                wdunderlinesingle CONSTANT NUMBER (5) := 1;
                ---- wdUnits Class members
                wdcell CONSTANT NUMBER (5) := 12;
                wdcharacter CONSTANT NUMBER (5) := 1;
                wdword CONSTANT NUMBER (5) := 2;
                wdsentence CONSTANT NUMBER (5) := 3;
                wdline CONSTANT NUMBER (5) := 5;
                ---- wdMovementType Class members
                wdextend CONSTANT NUMBER (5) := 1;
                wdmove CONSTANT NUMBER (5) := 0;
                --wdBorderType Class members
                wdborderleft CONSTANT NUMBER := -2;
                wdborderright CONSTANT NUMBER := -4;
                wdbordertop CONSTANT NUMBER := -1;
                wdborderbottom CONSTANT NUMBER := -3;
                --WdLineStyle Class member
                wdlinestylenone CONSTANT NUMBER := 0;
                mytab CONSTANT VARCHAR2 (1) := CHR (9);
                myblue CONSTANT NUMBER (8) := 16711680; --FF0000
                mygreen CONSTANT NUMBER (8) := 65280; --00FF00
                myred CONSTANT NUMBER (8) := 255; --0000FF
                mydkgreen CONSTANT NUMBER (8) := 32768; --008000
                myblack CONSTANT NUMBER (8) := 0; --000000
                mytext VARCHAR2 (2000);
                BEGIN
                happlication := ole2.create_obj ('Word.Application');
                ole2.set_property (happlication
                ,'Visible'
                ,1
                );
                hdocuments :=
                ole2.get_obj_property (happlication, 'Documents');
                hdocument := ole2.invoke_obj (hdocuments, 'Add');
                ------------------------------------------
                -------- Create Header and Footer --------
                ------------------------------------------
                hwindow :=
                ole2.get_obj_property (happlication, 'ActiveWindow');
                hpane := ole2.get_obj_property (hwindow, 'ActivePane');
                hview := ole2.get_obj_property (hpane, 'View');
                ---- Header Section ---
                ole2.set_property (hview
                ,'SeekView'
                ,wdseekcurrentpageheader
                );
                hselection :=
                ole2.get_obj_property (happlication, 'Selection');
                hfont := ole2.get_obj_property (hselection, 'Font');
                ole2.set_property (hfont
                ,'Name'
                ,'Calibri'
                );
                ole2.set_property (hfont
                ,'Size'
                ,10
                );
                ole2.set_property (hfont
                ,'Bold'
                ,FALSE
                );
                ole2.set_property (hfont
                ,'Color'
                ,myblack
                );
                ole2.RELEASE_OBJ (hfont);
                args := ole2.create_arglist;
                --Below is the location of your jpg file that contains the logo and address
                ole2.add_arg (args, 'C:\temp\AMHeader2.jpg');
                hinlineshapes :=
                ole2.get_obj_property (hselection, 'InlineShapes');
                ole2.invoke (hinlineshapes
                ,'AddPicture'
                ,args
                );
                ole2.destroy_arglist (args);
                ole2.RELEASE_OBJ (hinlineshapes);
                ole2.set_property (hview
                ,'SeekView'
                ,wdseekmaindocument
                );
                ole2.RELEASE_OBJ (hview);
                ole2.RELEASE_OBJ (hpane);
                ole2.RELEASE_OBJ (hwindow);
                -----------------------------
                -------- Insert Text --------
                -----------------------------
                hfont := ole2.get_obj_property (hselection, 'Font');
                ole2.set_property (hfont
                ,'Name'
                ,'Calibri'
                );
                ole2.set_property (hfont
                ,'Size'
                ,9
                );
                ole2.set_property (hfont
                ,'Bold'
                ,FALSE
                );
                ole2.set_property (hfont
                ,'Color'
                ,myblack
                );
                ole2.invoke (hselection, 'TypeParagraph');
                hparagraphformat :=
                ole2.get_obj_property (hselection, 'ParagraphFormat');
                ole2.set_property (hparagraphformat
                ,'Alignment'
                ,wdalignparagraphleft
                );
                ole2.RELEASE_OBJ (hparagraphformat);
                print_line (hselection
                , 'Date ' || TO_CHAR (TRUNC (SYSDATE), 'MM/DD/YYYY')
                ,NULL
                );
                print_line (hselection
                ,' '
                ,NULL
                );

                --The following prints the address of the recipient of the letter.
                print_line (hselection
                ,addr_line1
                ,NULL
                );
                print_line (hselection
                ,addr_line2
                ,NULL
                );
                print_line (hselection
                ,addr_line3
                ,NULL
                );
                print_line (hselection
                ,addr_line4
                ,NULL
                );
                print_line (hselection
                ,addr_line5
                ,NULL
                );
                --Call like the one below are to insert blank lines
                hselection :=
                ole2.get_obj_property (happlication, 'Selection');
                print_line (hselection
                ,NULL
                ,NULL
                );
                print_line (hselection
                ,'Your salutation'
                ,NULL
                );
                print_line (hselection
                ,NULL
                ,'NULL
                );
                print_line (hselection
                ,'Ref: '
                ,NULL
                );
                print_line (hselection
                ,NULL
                ,NULL
                );
                print_line
                (hselection
                ,'Whatever text you want to appear on this line.'
                ,NULL
                );
                print_line (hselection
                ,NULL
                ,NULL
                );
                ------------------------------
                -------- Create Table --------
                ------------------------------
                htables := ole2.get_obj_property (hdocument, 'Tables');
                hrange := ole2.get_obj_property (hselection, 'Range');
                args := ole2.create_arglist;
                ole2.add_arg_obj (args, hrange); --Range 
                ole2.add_arg (args, 1); --NumRows    The rest of the rows are created later as and when required
                ole2.add_arg (args, 6); --NumColumns This creates a table of 6 columns.
                ole2.add_arg (args, wdword9tablebehavior); --DefaultTableBehavior
                ole2.add_arg (args, wdautofitfixed); --FitBehavior
                htable := ole2.invoke_obj (htables
                ,'Add'
                ,args
                );
                ole2.destroy_arglist (args);

                -- [Borders removal start]The following piece of code removes all the borders. Use of this is optional.

                hborders := ole2.get_obj_property (htable, 'Borders');          
                ole2.set_property (hborders                                        
                ,'OutsideLineStyle'
                ,wdlinestylenone
                );
                ole2.set_property (hborders
                ,'InsideLineStyle'
                ,wdlinestylenone
                );
                ole2.RELEASE_OBJ (hborders);
                --[Borders removal end]

                --The following code makes the cursor enter the table. I took quite some time
                --figuring this out.

                hselection := ole2.get_obj_property (hdocument, 'Tables');
                hselection := ole2.get_obj_property (happlication, 'Selection');

                --WRITE_HEADINGS is the procedure called each time only to write headings.

                write_headings (hselection
                ,'Heading 1 on cell at row 1 column 1'
                ,8
                );
                --The following moves the cursor to the next cell on the right.

                args := ole2.create_arglist;
                ole2.add_arg (args, wdcell);
                ole2.add_arg (args, 1);
                ole2.add_arg (args, wdmove);
                ole2.invoke (hselection
                ,'MoveRight'
                ,args
                );
                ole2.destroy_arglist (args);

                write_headings (hselection
                ,'Heading 1 on cell at row 1 column 2'
                ,7
                );
                args := ole2.create_arglist;
                ole2.add_arg (args, wdcell);
                ole2.add_arg (args, 1);
                ole2.add_arg (args, wdmove);
                ole2.invoke (hselection
                ,'MoveRight'
                ,args
                );
                ole2.destroy_arglist (args);
                write_headings (hselection
                ,'Heading 1 on cell at row 1 column 3'
                ,7
                );
                args := ole2.create_arglist;
                ole2.add_arg (args, wdcell);
                ole2.add_arg (args, 1);
                ole2.add_arg (args, wdmove);
                ole2.invoke (hselection
                ,'MoveRight'
                ,args
                );
                ole2.destroy_arglist (args);
                write_headings (hselection
                ,'Heading 1 on cell at row 1 column 4'
                ,8
                );
                args := ole2.create_arglist;
                ole2.add_arg (args, wdcell);
                ole2.add_arg (args, 1);
                ole2.add_arg (args, wdmove);
                ole2.invoke (hselection
                ,'MoveRight'
                ,args
                );
                ole2.destroy_arglist (args);
                write_headings (hselection
                ,'Heading 1 on cell at row 1 column 5'
                ,5
                );
                args := ole2.create_arglist;
                ole2.add_arg (args, wdcell);
                ole2.add_arg (args, 1);
                ole2.add_arg (args, wdmove);
                ole2.invoke (hselection
                ,'MoveRight'
                ,args
                );
                ole2.destroy_arglist (args);
                write_headings (hselection
                ,'Heading 1 on cell at row 1 column 6'
                ,8
                );
                --The following moves the cursor back to the first cell on row 1 column 1
                args := ole2.create_arglist;
                ole2.add_arg (args, wdcell);
                ole2.add_arg (args, 6);
                ole2.add_arg (args, wdmove);
                ole2.invoke (hselection
                ,'MoveLeft'
                ,args
                );
                ole2.destroy_arglist (args);

                --Now we need to write data sourced from a multi record block in Oracle Forms

                GO_BLOCK ('your_block');
                FIRST_RECORD;
                record_cnt := 0;

                LOOP
                record_cnt := record_cnt + 1;

                ---In my application I gave the choice to the user to put a check mark on those records that he would want to print ------on the document and so the following.

                IF :your_block.cb_print = 'Y'          
                THEN               
                --This creates a new row below the first row that contains the column headers.
                args := ole2.create_arglist; ole2.add_arg (args, 1);
                ole2.invoke (hselection
                ,'InsertRowsBelow'
                ,args
                );
                ole2.destroy_arglist (args);

                --WRITE_DATA is called each time to write the data in the cells.

                write_data (hselection
                , :your_block.item);
                args := ole2.create_arglist;
                ole2.add_arg (args, wdcell);
                ole2.add_arg (args, 1);
                ole2.add_arg (args, wdmove);
                ole2.invoke (hselection
                ,'MoveRight'
                ,args
                );
                ole2.destroy_arglist (args);
                write_data (hselection
                               , :your_block.item);
                args := ole2.create_arglist;
                ole2.add_arg (args, wdcell);
                ole2.add_arg (args, 1);
                ole2.add_arg (args, wdmove);
                ole2.invoke (hselection
                ,'MoveRight'
                ,args
                );
                ole2.destroy_arglist (args);
                write_data (hselection
                               ,:your_block.item);
                args := ole2.create_arglist;
                ole2.add_arg (args, wdcell);
                ole2.add_arg (args, 1);
                ole2.add_arg (args, wdmove);
                ole2.invoke (hselection
                ,'MoveRight'
                ,args
                );
                ole2.destroy_arglist (args);
                write_data (hselection
                     ,:your_block.item);
                args := ole2.create_arglist;
                ole2.add_arg (args, wdcell);
                ole2.add_arg (args, 1);
                ole2.add_arg (args, wdmove);
                ole2.invoke (hselection
                ,'MoveRight'
                ,args
                );
                ole2.destroy_arglist (args);
                write_data (hselection
                     ,:your_block.item);
                args := ole2.create_arglist;
                ole2.add_arg (args, wdcell);
                ole2.add_arg (args, 1);
                ole2.add_arg (args, wdmove);
                ole2.invoke (hselection
                ,'MoveRight'
                ,args
                );
                ole2.destroy_arglist (args);
                write_data (hselection
                ,:your_block.item);
                END IF;

                EXIT WHEN :SYSTEM.LAST_RECORD = 'TRUE';
                NEXT_RECORD;
                END LOOP;

                --The following makes the cursor exit the table and back on document for any further
                --writing of text.

                args := ole2.create_arglist;
                ole2.add_arg (args, wdline);
                ole2.add_arg (args, 1);
                ole2.add_arg (args, wdmove);
                ole2.invoke (hselection
                ,'MoveDown'
                ,args
                );

                print_line (hselection           --As mentioned earlier, this enters a blank line.
                ,NULL
                ,NULL
                );


                print_line (hselection
                ,'Boilerplate text: '
                ,your_variable
                );


                print_line (hselection
                ,'Boilerplate text: '
                ,your_variable
                );


                --You can print as many lines as required by making call to the PRINT_LINE procedure.

                ole2.RELEASE_OBJ (htables);
                ole2.RELEASE_OBJ (hfont);
                ole2.RELEASE_OBJ (hselection);
                ole2.RELEASE_OBJ (hdocument);
                ole2.RELEASE_OBJ (hdocuments);
                ole2.RELEASE_OBJ (happlication);
                END;




                PROCEDURE PRINT_LINE
                ( v_sel OLE2.OBJ_TYPE
                , v_text VARCHAR2
                , v_field varchar2)
                IS

                /* This procedure takes in three arguments.
                The first v_sel is the VB selection object.
                v_text is the static or boilerplate text that needs to be printed on the document.
                v_field is the value of the item or variable from the program.
                */
                v_args      OLE2.LIST_TYPE;
                hTab     CONSTANT varchar2(1) := chr(9); --This is the vb constant for tabstop
                BEGIN
                v_args := OLE2.CREATE_ARGLIST;
                OLE2.ADD_ARG(v_args, v_text);
                OLE2.INVOKE(v_sel, 'TypeText', v_args);
                OLE2.DESTROY_ARGLIST(v_args);
                --The following replicates tabbing. This was used so that the field values are aligned at a tabstop.
                --When printing a blank line NULL is passed in as the argument
                v_args := OLE2.CREATE_ARGLIST;
                     OLE2.ADD_ARG(v_args, hTab);
                     OLE2.INVOKE(v_sel, 'TypeText', v_args);
                     OLE2.DESTROY_ARGLIST(v_args);
                --The following writes the value of the variable at the tabstop.      
                     v_args := OLE2.CREATE_ARGLIST;
                OLE2.ADD_ARG(v_args, v_field);
                OLE2.INVOKE(v_sel, 'TypeText', v_args);
                OLE2.DESTROY_ARGLIST(v_args);

                OLE2.INVOKE(v_sel, 'TypeParagraph');
                END;



                PROCEDURE WRITE_HEADINGS (SELIN OLE2.OBJ_TYPE, TEXTIN VARCHAR2, COUNTIN NUMBER)
                IS

                /* This procedure is used to write headings in the first row of the table.
                SELIN is the Selection object
                TEXTIN is the boilerplate text to be written as the header of a column.
                COUNTIN is the number of characters in the header text. This is required
                to replicate the action bolding the font of the header.
                */


                hFont          OLE2.OBJ_TYPE;
                v_args OLE2.LIST_TYPE;

                wdCharacter CONSTANT number(5) := 1; --Default
                --wdUnderline Class members
                wdUnderlineSingle                     CONSTANT NUMBER(5) := 1;


                ---- wdMovementType Class members
                wdExtend CONSTANT number(5) := 1;
                wdMove CONSTANT number(5) := 0; --Default

                ---- WdParagraphAlignment Class members
                wdAlignParagraphCenter CONSTANT number(5) := 1;
                wdAlignParagraphLeft CONSTANT number(5) := 0;
                wdAlignParagraphRight CONSTANT number(5) := 2;

                ---- HexColor = BBGGRR
                myLightBlue CONSTANT number(8) := 16755370; --FFAAAA


                BEGIN
                     
                hFont := OLE2.GET_OBJ_PROPERTY(selin, 'Font');
                OLE2.SET_PROPERTY(hFont, 'Name', 'Calibri');
                OLE2.SET_PROPERTY(hFont, 'Size', 10);
                OLE2.SET_PROPERTY(hFont, 'Bold', True);
                OLE2.RELEASE_OBJ(hFont);

                v_args := OLE2.CREATE_ARGLIST;
                OLE2.ADD_ARG(v_args, textin);
                OLE2.INVOKE(selin, 'TypeText', v_args);
                OLE2.DESTROY_ARGLIST(v_args);

                v_args := OLE2.CREATE_ARGLIST;
                OLE2.ADD_ARG(v_args, wdCharacter);
                OLE2.ADD_ARG(v_args, countin);
                OLE2.ADD_ARG(v_args, wdMove);
                OLE2.INVOKE(selin, 'MoveLeft', v_args);
                OLE2.DESTROY_ARGLIST(v_args);

                v_args := OLE2.CREATE_ARGLIST;
                OLE2.ADD_ARG(v_args, wdCharacter); --Unit
                OLE2.ADD_ARG(v_args, countin); --Count
                OLE2.ADD_ARG(v_args, wdExtend); --Extend
                OLE2.INVOKE(selin, 'MoveRight', v_args);
                OLE2.DESTROY_ARGLIST(v_args);

                hFont := OLE2.GET_OBJ_PROPERTY(selin, 'Font');
                OLE2.SET_PROPERTY(hFont, 'Underline', wdUnderlineSingle);
                OLE2.RELEASE_OBJ(hFont);


                END;




                PROCEDURE WRITE_DATA (SELIN OLE2.OBJ_TYPE, TEXTIN VARCHAR2)
                IS

                /*This procedure writes data in the cells of the table
                */
                hFont     OLE2.OBJ_TYPE;
                v_args OLE2.LIST_TYPE;

                wdCharacter CONSTANT number(5) := 1; --Default
                --wdUnderline Class members
                wdUnderlineSingle                     CONSTANT NUMBER(5) := 0;


                ---- wdMovementType Class members
                wdExtend CONSTANT number(5) := 1;
                wdMove CONSTANT number(5) := 0; --Default

                ---- WdParagraphAlignment Class members
                wdAlignParagraphCenter CONSTANT number(5) := 1;
                wdAlignParagraphLeft CONSTANT number(5) := 0;
                wdAlignParagraphRight CONSTANT number(5) := 2;

                ---- HexColor = BBGGRR
                myLightBlue CONSTANT number(8) := 16755370; --FFAAAA


                BEGIN
                     
                hFont := OLE2.GET_OBJ_PROPERTY(selin, 'Font');
                OLE2.SET_PROPERTY(hFont, 'Name', 'Calibri');
                OLE2.SET_PROPERTY(hFont, 'Size', 10);
                OLE2.SET_PROPERTY(hFont, 'Bold', False);
                OLE2.SET_PROPERTY(hFont, 'Underline', wdUnderlineSingle);
                OLE2.RELEASE_OBJ(hFont);

                v_args := OLE2.CREATE_ARGLIST;
                OLE2.ADD_ARG(v_args, textin);
                OLE2.INVOKE(selin, 'TypeText', v_args);
                OLE2.DESTROY_ARGLIST(v_args);




                END;

                Edited by: smilingbandit on 31-Mar-2010 7:05 PM

                Edited by: smilingbandit on 31-Mar-2010 8:23 PM

                Edited by: smilingbandit on Apr 1, 2010 6:14 AM: Reason - Fixed formatting of comments.
                • 6. Re: Problem entering values in table after creating it in Word using OLE2
                  788138
                  Great solution - thanks for posting the code :o)
                  • 7. Re: Problem entering values in table after creating it in Word using OLE2
                    788138
                    Hi

                    I was wondering whether you have met this problem too .... I have several bookmark items which are populated first, then I want to insert a dynamic table in the middle of the page. If I run the create table script as stand-alone I get a lovely table at the top of the page with columns and rows which are filled according to the data but if I try to put it in the middle of other data or items then I have a problem. I can build the table in the right position by setting the range to a bookmark when I invoke the table but then the header rows/data print at the top of the page. I think this is because 'Selection' is set to 'application' but if I set it to anything smaller I get a runtime error.

                    Any ideas?

                    (I can add code snippets if anyone would like to see it but it's based on the above example)

                    Thanks in advance
                    Helen (OLE2 newbie!)