How automatically fill in fields in a Word document?

I need to automize the filling of some fields in a Word template, converting the filled in document to PDF file. I tried to use org.apache.poi.xwpf and org.apache.pdfbox but the layout of the Word document isn’t respected : Logo disapeared, footer is messed up and indents and fonts are not respected.
What do you recommend?

1 Like

The Microsoft family of applications, like Word, Excel and Projects, have its own programming language called VBA (Visual Basic for Applications) so that you can automate some functions with a “macro”. The macro can be a whole bunch of programs, or methods, that you code to do whatever you want–maybe even add a Start button on your document to launch your code.

Just note that a macro within a document is deemed a security concern and will be indicated with a different file extension.

I wouldn’t use Katalon but I don’t know more than what you say above.

Edit: To work on macros, you will have to add “Developer” to your main menu:
File > Options > Customize Ribbon > put check on Developer (in right hand box). Click OK to save.

Use VBA. It is the only way to achieve what you want.

Logo disapeared, footer is messed up and indents and fonts are not respected.

Apache POI is a well-made mimic, but is not equal to the genuine MS Word. If you want to output a visually-complete .docx, you shouldn’t use POI. Any Java/Groovy technologies including Katalon would be short for you.

I have ever done a project of VBA programming to achieve a similar job as yours. Was it easy? — no, VBA is difficult for me. However, if we are asked to work on MS Office suites, no other choice than VBA we have. To me, VBA is a terrible language to learn and use, but it runs fine if I could write right codes.

Let me show you a part of my previous development. The following code is a VBA class named BbDocTransformer, which does the following

  1. A caller VBA code reads an Excel file (.xlsx) that contains a list of “association members”, constructs of Key-Value pairs, like { “name” : “kazurayam” }
  2. The caller calls this class instance passing params of the TemplateWordFile path, the KeyValuePairs, the RusultWordFile path
  3. the instance reads the Word template file as “template of a letter to memebers”
  4. apply the Key-Value pairs to the Word template while interpolating the placeholders.
  5. save the interpolation result into a new Word file named something like “letter to kazurayam-20250416.docx”
  6. repeat the 3, 4 & 5 for all rows in the Excel sheet.
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "BbDocTransformer"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit


' BbDocTransformer Word�����ϊ��@
'
' �e���v���[�g�Ƃ��Ă�Word�t�@�C����ǂ݁A�V����Word�t�@�C���𐶐����ď������ށB
' Excel���[�N�V�[�g�ɏ����ꂽ����i�l�̖��O�Ƃ��̑����j�̂P�s�Ԃ�̃f�[�^����͂Ƃ��Ď󂯎��B
' �e���v���[�g�̒��ɂ���placeholders�i���Ƃ��� ${���O}�j�����[�N�V�[�g����擾�����l�Œu������B
'
' �e���W���[����Excel���[�N�V�[�g���炽�Ƃ���2�O�O�l���̖����ǂ݁A
' DocTransformer��2�O�O��Ăяo���΁AWord�t�@�C����2�O�O�������鏈�������������邱�Ƃ��ł���B
'
' Word�t�@�C����IO���邽�߂�Word.Application�I�u�W�F�N�g��K�v�Ƃ��邪�A�p�t�H�[�}���X���l�����āA
' �e���W���[����Word.Application�I�u�W�F�N�g�𐶐����āAInitialize���\�b�h���Ăяo���ēn�����Ƃ����߂�B
'

Dim wordApp As Word.Application

Public Sub Initialize(wa As Word.Application)
    Set wordApp = wa
End Sub

Public Sub Transform( _
    ByVal TemplateWordFile As String, _
    ByVal KeyValuePairs As Dictionary, _
    ByVal ResultWordFile As String)
    
    Dim key As Variant
    
    ' �e���v���[�g�Ƃ��Ă�Word�t�@�C�������ʂƂ��Ă�Word�t�@�C���փR�s�[
    FileCopy TemplateWordFile, ResultWordFile
    
    ' ���ʂƂ��Ă�Word�t�@�C����open����
    Dim doc As Word.Document: Set doc = wordApp.Documents.Open(ResultWordFile)
    
    ' Word�����̂Ȃ��̃v���[�X�z���_�[��Excel������o�����f�[�^�Œu��������
    Dim KeysArray: KeysArray = KeyValuePairs.keys
    For Each key In KeysArray
        ' Debug.Print Key & "=" & KeyValuePairs.Item(Key)
        Dim placeholder As String: placeholder = "${" & key & "}"
        
        doc.Content.Find.Execute findText:=placeholder, _
                                ReplaceWith:=KeyValuePairs(key), _
                                MatchCase:=True, Replace:=wdReplaceAll
    Next
    
    ' �u�����ʂ��t�@�C���ɕۑ�����
    doc.Save
    
    ' ��n��
    doc.Close   ' Close����̂�Y����
                ' Word.Document�I�u�W�F�N�g��Close��Y���Ǝ�����s����FieCopy�ŃG���[�R�[�h71���������Ď��s����
    Set doc = Nothing
    
End Sub

The comments contain a lot of � characters, which were originally encoded in Shift_JIS (Japanese) and were garbled when I copy & pasted it here. Please just ignore the comments.

For example, a template could be

To: $name
From: Foo Bar Association
Date: $date

The interpolation result could be

To: kazurayam
From: Foo Bar Association
Date: 16 April, 2025

The following statement in the BbDocTransformer class does interpolating placeholders in a Word document with given values:

        doc.Content.Find.Execute findText:=placeholder, _
                                ReplaceWith:=KeyValuePairs(key), _
                                MatchCase:=True, Replace:=wdReplaceAll

This answers to the question from @johan.delaat, I believe.

My code saves the result into a Word file (.docx), but it is possible to tell Word to save the same into a PDF file. Please research how to for yourself.

In fact, I developed approximately 10 times more of VBA codes to accompilish an usefull Excel+Word application. It took me months to study VBA programming and write right codes.

@johan.delaat

Good luck.

@grylion54 Thanks for your reply. There is a problem : my organization blocks the use of VBA macros. On top of that I need Kathalon to collect the data from an application so that I can used it to fill them in in the Word template. Once that is done the Word document needs to be converted to a PDF document. Next step is to upload this PDF document in the application, create a new communication with this PDF document as attachment.
At the moment my colleague does this manually. I want to automate it so she can save a lot of time. She just passes me a list of references, for each reference I look up (using Katalon) the needed data in the application. The idea is to create the PDF document automatically so that (using Katalon again) I can do all the other steps in an automated way in the application.

Then, your organization will never be successful in automating MS Word.

By the way, why do you and your collegue use MS Word to generate a PDF eventually?

Your Groovy script in Katalon project should be able to generate a nice looking HTML, which can be converted into a PDF using PDFBOX.

https://www.baeldung.com/java-html-to-pdf

Here you don’t need Word at all.

It’s de business who has developed the template Word-document. That’s their way of working. When it’s only a few communications a day that need to be created, they’ll continue to do all the steps manually. It’s only when the backlog becomes to big they want it to be handled automatically. I’m using Katalon to test the application where data is stored, so they asked if I could (quickly) help them by automating all the steps.

Well, you will have to stay away from Katalon v10, as the below states, it does not support Windows desktop testing in that version, but otherwise, read the below and set yourself up: