View Raw SPL
/*****************************************************************************
*                                                                            *
*   MSWORD4.SPL     Copyright (C) 2019 DSP Development Corporation           *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:       Randy Race                                                 *
*                                                                            *
*   Synopsis:     Example of ActiveX support with MS Word                    *
*                                                                            *
*   Revisions:    10 Apr 2019  RRR  Creation                                 *
*                                                                            *
*****************************************************************************/


#if @HELP_MSWORD4

    MSWORD4

    Purpose: Inserts a table of values and metafile into MS Word using ActiveX.

    Syntax:  MSWORD4("title", data)

                "title" - Optional. A string, the title of the document.
                          Defaults to "Document Title".

                   data - Optional. An array, the value to place in the
                          table. Defaults to a 10x5 array of random values.

    Returns: Nothing - creates and inserts a table and window into MS Word

    Example:
             msword4

             Creates a new data in W1, then starts MS Word
             and inserts a data table and the metafile of W1 into the current
             Word document.

    Example:
             msword4("Example Data", randn(3, 6));

             Creates a new data in W1, then starts MS Word
             and inserts a 3x6 data table and the metafile of W1 into the
             current Word document. The title is set to "Example Data".

    Remarks:
             MSWORD4 is a simple example of how to invoke an external
             application (MS Word) as an ActiveX server using SPL.
             SPL uses an ActiveX syntax similar to C++ and Visual Basic.

             The plot is pasted into Word as a standard (WMF) metafile.

    See Also:
             Createobject
             Msword
             Msword2
             Msword3
#endif


/* example of adding a table and metafile to MS Word via ActiveX */
msword4(title, data)
{
        local word, doc, range, table, j, k, mf_w, mf_h, mf_p, mf_u;

        if (argc < 2)
        {
                if (argc < 1)
                {
                        title = "Document Title";
                }

                data = rand(10, 5);
        }

        /* data size */
        (nr, nc) = size(data);

        /* start Word */
        word = createobject("word.application");

        /* Word document */
        doc = word.documents.Add();

        /* document range to beginning of document */
        range = doc.Range(0, 0);

        /* title */
        range.insertbefore(title);

        /* bold, arial font, 16pt */
        range.font.name = "Arial";
        range.font.bold = 1;
        range.font.size = 16;

        /* center */
        range.paragraphformat.alignment = 1;

        /* 3 paragraphs for spacing */
        doc.paragraphs.add();
        doc.paragraphs.add();
        doc.paragraphs.add();

        /* set to last paragraph */
        range = doc.paragraphs(doc.paragraphs.count).range;

        /* add nr x nc table */
        table = doc.Tables.Add(range, nr, nc);

        /* add text to each cell */
        loop (j = 1..nr)
        {
                loop (k = 1..nc)
                {
                        table.Cell(j, k).Range.Text = sprintf("%g", data[j, k]);
                }
        }

        /* 11pt typewriter font */
        table.range.font.name = "Consolas";
        table.range.font.size = 11;

        /* spacing */
        doc.paragraphs.add();
        doc.paragraphs.add();

        /* set to last paragraph */
        range = doc.paragraphs(doc.paragraphs.count).range;

        /*
         *  setup metafile for clipboard transfer in case current user settings
         *  are different - see Tools > Options > Print Preferences > Metafile
         */

        /* set 6x6 metafile file */
        mf_w = setconfig("metafile_width",  6.0);
        mf_h = setconfig("metafile_height", 6.0);

        /* use screen resolution */
        mf_p = setconfig("metafile_use_printer", 0);

        /* direct clipboard copy (no file) */
        mf_u = setconfig("metafile_use_file", 0);

        /* do not show window number in label */
        printopt(-1, -1, -1, -1, -1, 0);

        /* generate series in W1 */
        W1 := (integ(gnorm(10000, 1/1000));setvunits("V");label("Test Data"));

        /* copy plot in W1 */
        copydata(w1);

        /* paste plot as WMF metafile */
        range.pastespecial(0, 0, 0, 0, 3);

        /* restore original metafile settings */
        setconfig("metafile_width",       mf_w);
        setconfig("metafile_height",      mf_h);
        setconfig("metafile_use_printer", mf_p);
        setconfig("metafile_use_file",    mf_u);

        /* add text */
        range.insertafter("Data Plot of W1");

        range.font.name   = "Arial";
        range.font.size   = 10;
        range.font.italic = 1;

        word.Visible = 1;
}