Generating a word document with formatting

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2023 06:41 AM
I want to create a word document programmatically.
The code I'm using is shown below:
var inc = new GlideRecord("incident");
inc.addQuery("sys_id", "ede122932f8da5108d3fe36ef699b634");
inc.query();
if (inc.next()) {
var gsa = new GlideSysAttachment();
gsa.write(inc, 'test.doc', 'application/msword', '<h1>Test</h1>');
}
The problem I have with this code is the formatting. When I open the system created word file, the HTML tags appear as string and are not rendered.
I remember this used to work earlier, what am I missing guys?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2023 10:32 AM
Word files using the old .doc extension are binary files. I doubt that HTML will be considered by any app built to read such field as a valid file and will interpret it. Even the newer .docx format is a zipped XML file (not HTML) that has a very complex and verbose format (a lot of boilerplate) even for the most basic document (like a Hello world document).
You probably need to use the .html (or .htm) extention(s).

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2023 01:47 PM
Give this code a try:
var gr = new GlideRecord("kb_knowledge");
gr.addQuery("sys_id", "464544d32feb51908d3fe36ef699b638");
gr.query();
if (gr.next()) {
var gsa = new GlideSysAttachment();
gsa.write(gr, 'test.doc', 'application/msword', '<table style="border-collapse: collapse; width: 99.995%;" border="1"><tbody><tr><td style="width: 48.501%;"><strong>Label</strong></td><td style="width: 48.501%;"><strong>Name</strong></td></tr><tr><td style="width: 48.501%;">Caller</td><td style="width: 48.501%;">caller_id</td></tr></tbody></table>');
}
This actually creates a word document with html properly rendered! 🙂

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2023 01:48 PM
But this one does not render!
gsa.write(inc, 'test.doc', 'application/msword', '<h1>Test</h1>');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2023 03:29 PM - edited 02-22-2023 07:34 PM
Opening such a file in LibreOffice also works. But this is just the courtesy of the application one happens to force to open such a file. The format is just plain HTML, has nothing to do with office formats and if it works it works just by chance and at the whims of the MS Office. I, for one, would not expect or rely on this (html files with .doc extension) being properly interpreted by MS Office/Word.
On the other hand, if the file uses the proper extension (.html) should make MS Work react properly: to correctly interpret even the basic HTML. But it is true that such a file might not automatically be opened in MS Word if double-clicked by a user in a browser.