'Add line numbering to a .docx with ActiveXObject('Word.Application')

Hi i want to add line numbering to the opened .docx file before converting it to .pdf and i really didn't find how to do that on code, it is converting my .docx file to pdf but i wanna add line numbering too, am using ActiveXObject with javascript to do that on server

var obj = new ActiveXObject("Scripting.FileSystemObject");
var docPath = WScript.Arguments(0);
docPath = obj.GetAbsolutePathName(docPath);
var pdfPath = docPath.replace(/\.doc[^.]*$/, ".pdf");
var objWord = null;
try
{
    objWord = new ActiveXObject("Word.Application");
    objWord.Visible = false;
    var objDoc = objWord.Documents.Open(docPath);
    var format = 17;
    objDoc.SaveAs(pdfPath, format);

    objDoc.Close();
    WScript.Echo("Saving '" + docPath + "' as '" + pdfPath + "'...");
}
catch(e){
    WScript.Echo(e);
}
finally
{
    if (objWord != null)
    {
        objWord.Quit();
    }
}


Solution 1:[1]

If you want to automate Word but you don't know how to do it in code, start recording a macro, do what you want, then stop recording and review the code. You should be able to figure out what you need from the generated code.

Here's an example of a recorded macro:

Sub Macro1
  With Selection.PageSetup
    With .LineNumbering
        .Active = True
        .StartingNumber = 1
        .CountBy = 1
        .RestartMode = wdRestartContinuous
        .DistanceFromText = wdAutoPosition
    End With
    .Orientation = wdOrientPortrait
    .TopMargin = InchesToPoints(1)
    .BottomMargin = InchesToPoints(1)
    .LeftMargin = InchesToPoints(1)
    .RightMargin = InchesToPoints(1)
    .Gutter = InchesToPoints(0)
    .HeaderDistance = InchesToPoints(0.5)
    .FooterDistance = InchesToPoints(0.5)
    .PageWidth = InchesToPoints(8.5)
    .PageHeight = InchesToPoints(11)
    .FirstPageTray = wdPrinterDefaultBin
    .OtherPagesTray = wdPrinterDefaultBin
    .SectionStart = wdSectionNewPage
    .OddAndEvenPagesHeaderFooter = False
    .DifferentFirstPageHeaderFooter = False
    .VerticalAlignment = wdAlignVerticalTop
    .SuppressEndnotes = False
    .MirrorMargins = False
    .TwoPagesOnOne = False
    .BookFoldPrinting = False
    .BookFoldRevPrinting = False
    .BookFoldPrintingSheets = 1
    .GutterPos = wdGutterPosLeft
  End With
End Sub

Based on that example, I would assume your code would need to include this for line numbers:

objDoc.PageSetup.LineNumbering.Active = true;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1