Replacing MERGEFIELD fields via JavaScript/postMessage API

Hello Collabora Support Team,

I am currently integrating Collabora Online Development Edition (CODE) into my web application using the WOPI protocol and the iframe postMessage API.

I am able to successfully replace normal text in documents using the following UNO command via postMessage:

iframe.contentWindow.postMessage(
JSON.stringify({ MessageId: “Send_UNO_Command”, Values: {
Command: ‘.uno:ExecuteSearch’,
Args: {
‘SearchItem.SearchString’: { type: ‘string’, value: searchText },
‘SearchItem.ReplaceString’: { type: ‘string’, value: replaceText },
‘SearchItem.Command’: { type: ‘short’, value: 3 },
}
} }),
‘*’
);

However, I have Word documents (.docx) that contain Mail Merge fields (MERGEFIELD). These fields are visible in the Collabora Navigator under “Fields”.

My question is: Is it possible to replace or fill MERGEFIELD values programmatically from JavaScript using the postMessage API? For example, replacing a field named “TestField” with a specific value like “123456” — without opening any dialogs.

I have already tried the following approaches without success:

  • .uno:ReplaceAll — does not find MERGEFIELD fields
  • .uno:ModifyField — opens a dialog, cannot be used programmatically
  • .uno:ExecuteSearch with SearchItem.ValueSearch — no effect on fields

Is there a supported way to do this via postMessage/UNO commands? Or is this a planned feature for a future release?

Thank you very much for your support.

Best regards

Hi @Muhanad,

Welcome to the Collabora Online forum :slight_smile:

This is actually a known limitation rather than something you’re doing wrong. MERGEFIELDs are structured field objects in the LibreOffice document model, not inline text, so the text-layer search/replace commands (.uno:ExecuteSearch, .uno:ReplaceAll) simply do not see them.

The postMessage API only exposes a subset of UNO commands, and unfortunately it does not include the lower-level document model APIs you would need (XTextFieldsSupplier, XDependentTextField, etc.) to enumerate and modify fields programmatically.

After some research, I would recommend handling this on the server side instead.

The cleanest integration pattern would be to add a preprocessing step in your WOPI flow:

User triggers merge → Your app calls fill_merge_fields() →
Writes filled doc to WOPI storage → Collabora loads the filled doc

This approach lets Collabora focus on what it does best (collaborative editing and rendering), while your backend handles the merge logic where you have full document model access.

It also keeps your implementation independent from the postMessage API surface, so it is less likely to break with future Collabora version changes.

If you absolutely need a client-side-only solution (without a server round-trip), the only possible approach I can think of would be embedding a macro in the document that performs the replacement on open. However, Collabora usually restricts macro execution for security reasons, so this would be fragile and not generally recommended.

Server-side preprocessing is the standard and most reliable solution for this use case.

Also, I’m quite curious about your application. What kind of project are you integrating this into? Is it for a commercial application or more of a fun hacking/project experiment? :grinning_face_with_smiling_eyes:

We have several integration examples already, and your use case could also make for a very interesting example for others in the community :slightly_smiling_face:

Thanks,
Darshan