Ask HN: Have you run Microsoft Word in headless mode?

5 points by bredren 14 days ago

Have you or your organization run Microsoft Word in headless mode? How do you do it, and why?

For example, "a node.js server on a Windows machine that takes a file, saves it onto disk and runs a Visual Basic application on it which itself starts Microsoft Word headless to [perform some operation]" [1]

I need to manipulate Word documents that include accurate page numbers for elements like Table of Contents and List of Figures. These dynamic elements are populated on first open by Word (with user approval)

I'd like to automate all of this, and potentially manipulate the resulting .docx afterward.

From my initial research, doing this well requires processing by Word's layout engine.

I need this to be available as a web service to another application written in Python.

Other solutions I've considered include using LibreOffice in a headless mode, or a commonly mentioned but high priced solution [2] Aspose.words.

[1] https://news.ycombinator.com/item?id=17425712

[2] https://products.aspose.app/words

ksherlock 12 days ago

This advice is old enough to drive, but Joel Spolsky suggested doing just that.

"Word and Excel have extremely complete object models, available via COM Automation, which allow you to programmatically do anything. In many situations, you are better off reusing the code inside Office rather than trying to reimplement it. Here are a few examples..."

https://www.joelonsoftware.com/2008/02/19/why-are-the-micros...

nullindividual 14 days ago

Microsoft essentially says 'don't install Word server-side' [0]. The 3rd party you've linked to would be a more appropriate route.

You can automate in other ways, such as interacting with COM via a scripting language or development language client-side. That is supported. If you're dead-set on a web-based application, then yeah, go 3rd party.

[0] https://support.microsoft.com/en-us/topic/considerations-for...

  • bredren 14 days ago

    Thanks for the feedback.

    I don't think client side manipulation is an option as the result needs to be made available directly via a web-based SaaS.

    The licensing issue seems thorny. If this were as easy to set up and use as the comment I quoted suggested, we would need to get some validation that any end user performing "exports" had their own valid license to Word at the time of export.

    I'm not sure what the secret sauce is in the third party libraries is that can't be done at a much more limited scope by manipulating the docx directly.

    The most complex thing seems to be the determination of page numbers associated with various headings and the ability to refresh this on the fly.

    • nullindividual 14 days ago

      Licensing is a thorny issue. Office follows the Universal License Terms [0] which have various restrictions, such as not offering Office to 3rd parties and all users internal to a company who use a service like yours would require an Office CAL themselves. You would need a Volume License subscription for Office client. Retail copies are most definitely not licensed for this use-case. This would include anyone using your software -- they would need to have a volume licensed copy of Office.

      I would suggest talking to a company like CDW who can assist with license terms or Microsoft Licensing -- don't take an HN or any other forum reply as a definitive answer. Microsoft typically doesn't let you get away 'cheap' when you use their software like this. And they've covered all of the bases you can think of to be cheap.

      Since docx is 'open', any masochistic third party can write a library to interact with it -- or just take from the likes of LibreOffice and adapt it for server-side software. This avoids the Microsoft licensing discussion.

      [0] https://www.microsoft.com/licensing/terms/product/ForallSoft...

x0x0 13 days ago

For reasons, I've written code using docx4j to convert to and from .docx. We did examine Aspose.

Given the complexity of all the things you can do with Word and the immense feature set, if high fidelity is the goal, I don't believe using anything but actual Word and the real Word layout engine will give you that. You'll have to figure out if the licensing problem is one you can live with.

transfire 13 days ago

My company just bought a used Olympus microscope. It requires Word to generate metrology reports. So apparently it runs Word behind the scenes. Is that considered a headless mode?

Unfortunately M$ Word is the only option. Can’t say I’m happy about it. But it is what it is.

solardev 12 days ago

What about the Word web app? Can you use Playwright or similar to fake a login to that and extract the document data there?

wruza 12 days ago

I automated Excel and other specific COM app at work with `npm i winax`. Word should be similar. There’s a few gotchas with reference counts and MS’s idiotic out-of-process behaviors, but otherwise it works.

LibreOffice cannot open even simple documents correctly ime, but ymmw.

In case the “user approval” isn’t a part of COM connection, I’d look for headful word instead with an AHK/etc script triggering it.

  • bredren 9 days ago

    Can you go into more detail on the problems you had with LibreOffice opening documents correctly?

    In my comparisons for page numbering for generated elements like table of contents etc. it seemed to work pretty well.