Studio: create Word document with C# code via Edge-js

After reading @TimDolotkazin’s response to my query elsewhere, I thought I might share the following code running from the Command Prompt activity. Apologies if this is in the wrong category.

node C:\Users\HP6300\Documents\ElectroNeek\wd14.js

The heavy lifting is done via edge-js, a version of Edge.js that works for me at least. This comes with the caveat that all the problems associated with node.js come along with it, such as setting the NODE_PATH variable. Strange things can happen if you don’t add at least the %appdata%\npm\node_modules to this environment variable. But it’s worth it to be able to run C# code from ElectroNeek Studio, at least for me.

I recommend making sure your code runs from the Windows command prompt first before trying to run it from Studio.

Code listing for ‘wd14.js’ is below. If you are running a reasonably current version of Word for desktop (I’m still using Office 16 and am likely to do so for the foreseeable future), this should work “out of the box”.

var edge = require('edge-js');

var word = null;
var docc = edge.func(function () {/*
        async (word) => { 
            
            dynamic wd = Activator.CreateInstance(Type.GetTypeFromProgID("Word.Application"));
            // if you want to see the UI (for example, when debugging)
            wd.Visible = true;
            dynamic doc = wd.Documents.Add();
            wd.ActiveDocument.StoryRanges(1).Select(); // wdMainTextStory = 1
            dynamic range = wd.Selection.Range;
            // wd.Selection.TypeText("Monkeyz!"); // 
            wd.ActiveDocument.tables.Add(range,11,5);
            wd.Selection.Tables(1).Style = "Table Grid";
            wd.Selection.Tables(1).Cell(1,2).Range.Select();
            wd.Selection.TypeText("Result"); 
            wd.Selection.Tables(1).Cell(1,1).Range.Select();
            wd.Selection.TypeText("Inmate Request"); 
            wd.Selection.Tables(1).Cell(2,1).Range.Select();
            wd.Selection.TypeText("Another cell, please!");
            wd.Selection.Tables(1).Cell(2,2).Range.Select();
            wd.Selection.TypeText("Approved."); 
            wd.Selection.MoveDown(5, 12, 0);
            wd.Selection.TypeText("Ending first table.");
            wd.Selection.TypeParagraph();
            wd.Selection.TypeText("Starting second table."); 
            wd.Selection.TypeParagraph();
            dynamic range2 = wd.Selection.Range;
            wd.ActiveDocument.tables.Add(range2,11,5);      
            wd.Selection.Tables(1).Style = "Table Grid";  
            wd.Selection.Tables(1).Cell(1,2).Range.Select();
            wd.Selection.TypeText("B1"); 
            wd.Selection.Tables(1).Cell(1,1).Range.Select();
            wd.Selection.TypeText("A1");  
            wd.Selection.Tables(1).Cell(2,2).Range.Select();
            wd.Selection.TypeText("B2");
            wd.Selection.Tables(1).Cell(4,4).Range.Select();
            wd.Selection.TypeText("Hey! This cell is worse!");            
            wd.Selection.MoveDown(5, 12, 0);  // wdLine := 5, Count has no constant. wdMove is 0, wdExtend is 1
            wd.Selection.TypeText("End of table demonstration");

        return "Word worked";
    }
*/});

docc(word, function (error, result) {
    if (error) throw error;
    console.log(result);
});

Code listing for wd14.js

An animated GIF via imgur, which unfortunately drops some frames:

Regards,
burque505