View Raw SPL
/*****************************************************************************
*                                                                            *
*   MSWORD5.SPL   Copyright (C) 2024 DSP Development Corporation             *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:       Randy Race                                                 *
*                                                                            *
*   Synopsis:     Example of ActiveX support with MS Word                    *
*                                                                            *
*   Revisions:     9 Feb 2024  RRR  Creation                                 *
*                                                                            *
*****************************************************************************/


#if @HELP_MSWORD5

    MSWORD5

    Purpose: Inserts a disk based Worksheet metafile into MS Word using ActiveX

    Syntax:  MSWORD5(len)

             len - Optional. An integer, the length of the series for the
                   generated Worksheet. Defaults to 30000 samples.

    Returns: Nothing - creates and inserts a Worksheet into MS Word

    Example:
             msword5

             Creates a new example Worksheet in DADiSP. The print preferences
             are set to write the metafile to disk when the worksheet is
             copied to the clipboard. MS Word is started and the disk based
             metafile of the Worksheet is inserted into the current
             Word document.

    Example:
             msword5(200000)

             Same as above except the series are 200000 samples. This creates
             a larger metafile.

    Remarks:
             MSWORD5 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.

             MSWORD5 bypasses the clipboard and writes the metafile to disk.
             Word is automated to insert the metafile as a picture.

             Here are the pertinent SPL statements:

             msword5_write_metafile(); // copy Worksheet metafile to disk

             word = CreateObject("Word.Application");           // start Word
             doc = word.Documents.Add();                        // get Doc object
             range = doc.Range();                               // get Range Object
             word.Selection.InlineShapes.AddPicture(metafname); // insert disk file
             word.Visible = 1;                                  // show on screen

             The MSWORD5_WRITE_METAFILE() co-routine sets up the printer
             preferences to bypass the clipboard. It then writes the
             Worksheet metafile to disk and restores the original settings.

             See MSWORD for an example of copying text into.

    See Also:
             Createobject
             Getproperty
             Msword
             Setproperty

#endif


/* configuration states */
static mf_uf = {};
static mf_fn = "";



/* demonstrates ActiveX calls to MS Word */
msword5(len)
{
        local word, doc, metafname;

        if (argc < 1)
        {
                len = 30000;
        }


        /* new 3 Window worksheet */
        if (newworksheet(3, 0) == 0) return;

        /* W1 formula */
        setwf(W1, sprintf("gsin(%lld, 1/%lld, 20);W0 + gnorm(length, deltax)", len, len));

        W2 := slp(w1);
        W3 := (xy(w1, w2);points;setsym(14);rainbow);

        /* copy worksheet metafile to disk */
        metafname = msword5_write_metafile();

        echo("Starting Word...");

        // start Word, equivalent to Visual Basic's:
        // Dim word As New Word.Application
        word = CreateObject("Word.Application");

        /* remove messages */
        echo("");

        // get "Document" object
        doc = word.Documents.Add();

        // Add a new Document and get range object
        range = doc.Range();

        /* insert worksheet metafile as picture */
        word.Selection.InlineShapes.AddPicture(metafname);

        // let's see it!
        word.Visible = 1;

        /*
         *  Note: ActiveX objects are automatically released when the
         *        local (or global) variables are deleted
         */

        // return the handle so we can assign it (if desired)
        if (outargc > 0)
        {
                return(word);
        }
}


/* write worksheet metafile to disk */
msword5_write_metafile(fname)
{
        if (argc < 1)
        {
                /* metafile in temp folder */
                fname = getmiscpath(1, 1) + "dspmeta.emf";
        }

        /* write metafile to file instead of clipboard */
        mf_uf = setconfig("metafile_use_file", 1);

        /* metafile name - single / to // */
        mf_fn = setconfig("metafile_filename", fixslash(fname));

        /* copy worksheet - redirected to to file */
        copyworksheet();

        /* restore */
        setconfig("metafile_use_file", mf_uf);
        setconfig("metafile_filename", mf_fn);

        mf_uf = {};
        mf_fn = "";

        /* return filename */
        return(fname);
}


/* error handler */
msword5_write_metafile_error(errnum, errmes)
{
        if (not(isempty(mf_uf)))
        {
                setconfig("metafile_use_file", mf_uf);
                mf_uf = {};
        }

        if (not(strlen(mf_fn) == 0))
        {
                setconfig("metafile_filename", mf_fn);
                mf_fn = "";
        }

        error(errmes);
}