ms-wordsasdde

Copy and paste a table from one word document to another SAS DDE


I'm using SAS v9.4, running a connection via DDE to Word 2010.

I would like to copy and paste an entire table from onw word document to another. The table is bookmarked "IDX" and I am able to select the table using the following code:

options noxsync noxwait xmin;
filename sas2word dde 'winword|system';
data _null_;
  file sas2word;
  put '[EditGoTo.Destination = "IDX"]';
  put '[TableSelectTable]';
run;

I have tried put '[ctrl+c]';, put '[copy]';, put '[TableCopy]';, put '[SelectionCopy]'; but nothing seems to work, and the code crashes. Does anyone know the syntax to copy the entire table, then paste it into a different document?


Solution

  • Here is SAS 9.4M4 sample code that use experimental ODS WORD destination to create two Word documents, and copies a table from one to the other. YMMV, and you probably have addition work regarding issues such a table wrapping and anchoring.

    filename one "c:\temp\one.docx";
    filename two "c:\temp\two.docx";
    
    ods _all_ close;
    
    title; footnote;
    
    options nocenter nonumber nodate;
    
    ods word file=one;
      proc print data=sashelp.class (obs=5);
      proc print data=sashelp.cars (obs=5);
      proc print data=sashelp.demographics (obs=5);
      proc print data=sashelp.class (obs=5);
      run;
    ods word close;
    
    ods word file=two;
      proc print data=sashelp.cars (obs=10);
      run;
    ods word close;
    
    * start WORD;
    options noxsync noxwait xmin;
    %sysexec start "Yada yada yada" winword;
    %let rc = %sysfunc(sleep(5,1));
    %put NOTE: &=rc;
    
    * define channel for sending commands;
    filename word_cmd dde 'winword|system';
    
    * put will send the commands to WORD;
    data _null_;
      file word_cmd;
      cmd = cats ( "[FileOpen.Name=", quote(trim(pathname("One"))), "]");
      put cmd;
    
      put '[EditBookmark name:="IDX3", goto:=1]';
      put '[NextObject]';
      put '[GoToNextSection]';
      put '[TableSelectTable]';
      put '[EditCopy]';
    
      cmd = cats ( "[FileOpen.Name=", quote(trim(pathname("Two"))), "]");
      put cmd;
      put '[Selection.Goto(wdGotoLine, wdGotoLast)]';
      put '[EditPaste]';
    run;
    

    The Word command ListCommands will create a document containing a table of all the Word Commands and active key mappings.

    data _null_;
      file word_cmd;
      put '[ListCommands]';
    run;
    

    The list goes on for 10 pages in Word 2016. Word commands are also invokable from dde connections. Unfortunately ListCommands lists a descriptive command name, and not the command that dde actually requires, and does not actually list all commands. The WordMVP site (https://wordmvp.com) has assembled a list - "Word for Windows commands"

    Word has a built-in command ListCommands, which produces a table of all the Word commands with their current key and menu assignments. However, it does not list the commands using their actual names; nor does it include descriptions of what the commands actually do.

    WordCmndsPDF.zip contains a list of all interceptable Word commands (Word 97 and above), using their correct English names

    Another reference for Word commands can be found at "Visual Basic Equivalents for WordBasic Commands", 6/13/2014.

    A SAS conference paper search for "WORD DDE" will also provide additional material.