Micro-guide: Robin, node.js, and edge-js to create COM objects

As a prefatory remark, I’m very disappointed the Microsoft has maintained radio silence about the future of Robin. It would seem to me that common courtesy - which costs nothing - dictates at least some notice to forum members of what is to come.

Nonetheless, with some hesitation due to my concern stated above, I’m posting some code for creating an Office COM object (Word in this case) using node.js, edge-js, and of course Robin.

Just FYI, this Edge link (nothing to do with the browser) may whet your appetite. As Scott Hanselman said in response the question “What problems does Edge.js solve?”:

Ah, whatever problem you have. If you have this problem, this solves it. :grinning:

The Robin code is trivial. On my system:

System.RunDOSCommand \
                DOSCommandOrApplication: """node C:\work\edge\wd14.js"""\
                WorkingDirectory:'' \
                StandardOutput=> StandardOutput \
                StandardError=> StandardError \
                ExitCode=> ExitCode

The heavy lifting is done by node.js and edge.js. Code listing for ‘wd14.js’:

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);
});

Screenshot of resulting Word document, created with C# code from node.js via edge-js:

Regards and best wishes for the future (whatever that is),
burque505

2 Likes