Hello,

This is Gokul Rangarajan. I need a suggestion or code in JavaScript which reads the MS Word document page settings and number of pages. I know how to open and read a MS Word document, but i couldn't able to get page properties. Some one please help me with this.

Thank you,
Gokul Rangarajan

Dani AI

Generated

Nice find by — using COM/ActiveX to open Word, call Word’s statistics/layout API and read PageSetup gives a reliable desktop result when run interactively on Windows with Word installed. (codevba.com)

A few important clarifications and pitfalls:

  • Word’s PageSetup and page measurements are returned in points (1 point = 1/72 inch), not pixels; convert to pixels with px = points * (DPI / 72) when you really need screen pixels. (learn.microsoft.com)
  • ActiveX/COM automation like this only runs in IE/WSH/desktop contexts and requires the user’s Word install and appropriate security/trusted settings; it is not a cross‑browser/client solution. Microsoft also warns against automating Office from unattended server code — it can hang or misbehave. (learn.microsoft.com)
  • ComputeStatistics (and similar calls) can sometimes return wrong (or “1”) page counts if Word hasn’t paginated the document (wrong view, layout not finished). If you see inconsistent counts, make sure Word is in Print Layout, allow page layout to finish, or use Range.Information(wdNumberOfPagesInDocument) as an alternative. (stackoverflow.com)

If you need other deployment options:

  • For a quick, best-effort stored value, read the DOCX extended properties (/docProps/app.xml → <Pages>) — that is the last‑saved page count and can be stale. (hellowac.github.io)
  • For server-side accuracy without Word, convert the document to PDF with headless LibreOffice and count pages with pdfinfo (poppler); that is a robust pipeline for batch jobs. (help.libreoffice.org)
  • For rich server APIs that perform layout, consider commercial libraries (for example Aspose.Words exposes Document.PageCount after layout). (reference.aspose.com)

If you want a small, no‑Word fallback to read the saved page count from a .docx, this Node.js sketch extracts the <Pages> value from docProps/app.xml:

const fs = require('fs');
const JSZip = require('jszip');

(async () => {
  const data = fs.readFileSync('file.docx');
  const zip = await JSZip.loadAsync(data);
  const app = await zip.file('docProps/app.xml').async('string');
  const m = app.match(/<Pages>(\d+)<\/Pages>/);
  console.log(m ? Number(m[1]) : 'no stored page count');
})();

Test on the exact target environment (Word build, view mode, and permissions) before shipping — desktop COM automation is fine for local tools but fragile outside that context.

Hi, this is gokul rangarajan. I just found solution for my proble,

var w=new ActiveXObject('Word.Application'); --creare MS word obj.
w.Documents.Open("C:\\ABSTRACT.doc"); -- open a doc..
obj=w.visible=false; --- set visible false
obj.Content; --- to get content in MS word doc..
w.ActiveDocument.ComputeStatistics(2); -- Get number of pages
w.ActiveDocument.ActiveWindow.Panes(1).Pages.Item(1).Width; -get width in pixels..
w.ActiveDocument.ActiveWindow.Panes(1).Pages.Item(1).Height; -get height in pixels..
w.ActiveDocument.PageSetup.TopMargin --get Margin top in pixels
w.ActiveDocument.PageSetup.LeftMargin --get Margin left in pixels
w.ActiveDocument.PageSetup.BottomMargin --get Margin bottom in pixels
w.ActiveDocument.PageSetup.RightMargin --get Margin right in pixels
w.ActiveDocument.PrintOut(); //Print given document


Note: 72 pixel = 1 inches.

Thank you,:cool:
Gokul Rangarajan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.