How to Do a Mail Merge in Word Using UniOffice
Introduction
Mail merge helps you send many personalized documents. You write one template. Then you merge data, like names or addresses, into it. In Microsoft Word, this is known as “word mail merge” or “microsoft word mail merge.”
In this blog, you’ll learn how to do a mail merge in Word—but using the UniOffice SDK.
By the end, you’ll know how to use mail merge in Word, but via code, not the GUI.
What is Mail Merge?
Mail merge in Word is a tool to make many customized documents fast.
You create one main document.
Then you connect it to a data source (Excel, CSV, etc.).
You insert merge fields, like «FirstName».
When you merge, Word fills in the fields and makes one for each person. This is great for letters, labels, envelopes, and emails.
It saves time and makes each document look personal.
Word’s Mail Merge—Traditional Steps
If you were doing this in Word, here’s how:
Open Word.
Go to the Mailings tab.
Click Start Mail Merge → pick type (Letters, Email, Labels)
Choose your starting document (blank or template).
Click Select Recipients and connect Excel, Outlook, or type in Word.
Insert merge fields:
- Address Block, Greeting Line, or Insert Merge Field.
Preview fields with Preview Results.
Complete merge via Finish & Merge—print, edit, or send email
That’s how to mail merge in Word using built‑in tools.
Why Use UniOffice SDK?
UniOffice is a Go library by UniDoc.
It lets you create or modify docx, xlsx, pptx files in Go code.
With UniOffice, instead of clicking through Word, you can script your mail merge.
That’s mail merge word, but in code.
Advantages:
Automate document creation.
Integrate with web servers or other systems.
Avoid needing Word installed.
Setting Up UniOffice Mail Merge
Step 1: Install UniOffice
go get github.com/unidoc/unioffice/v2
UniOffice is commercial, and you need an API key. Sign up at UniCloud for a free key.
Step 2: Clone the examples
UniOffice offers samples, including mail merge.
git clone https://github.com/unidoc/unioffice-examples
cd unioffice-examples/document/mail-merge
You’ll see mm.docx
and main.go
(UniDoc).
Understanding the Code
Here’s what the code does:
import (
"github.com/unidoc/unioffice/v2/common/license"
"github.com/unidoc/unioffice/v2/document"
)
func init() {
license.SetMeteredKey(os.Getenv("UNIDOC_LICENSE_API_KEY"))
}
func main() {
d, _ := document.Open("mm.docx")
defer d.Close()
for _, v := range d.MergeFields() {
log.Println("replacing", v)
}
rep := map[string]string{
"Title": "mr.",
"FirstName": "JOHN",
}
d.MailMerge(rep)
d.SaveToFile("merged.docx")
}
license.SetMeteredKey(...)
loads your license key.document.Open("mm.docx")
opens the template.MergeFields()
lists all field names in the document.You build a map of field names and replacement values.
MailMerge(rep)
applies the merge.SaveToFile("merged.docx")
saves the final document.
That’s how to do a mail merge in Word using UniOffice code.
Full Step-by-Step (In Code)
1. Prepare a Word template
Open Word.
Add placeholders: «Title»
, «FirstName»
, etc.
Save as mm.docx
.
2. Write your Go code
Import UniOffice’s document package.
Load your
.docx
.List the merge field names.
Build a replacement map (string to string).
Call
MailMerge()
.Save the result.
3. Run it
export UNIDOC_LICENSE_API_KEY=your_key
go run main.go
Now you have merged.docx
.
More Advanced Options
You can customize all fields — not just text.
If using Excel or DB, read data into your Go map.
You can loop rows from an Excel file and call
MailMerge()
multiple times.Then aggregate merged docs or save each one separately.
Comparing Word vs UniOffice
Feature | Word GUI | UniOffice SDK |
---|---|---|
Personalized letters | ✔️ | ✔️ |
Folders of labels | ✔️ | ✔️ (via code) |
Send email | ✔️ (via Outlook) | ❌ (Word only) |
Fully automated | ❌ | ✔️ |
Server usage | ❌ | ✔️ |
Scripting & re-use | ❌ | ✔️ |
Common Questions
Question: Can I see all available merge fields?
Answer: Yes. Use d.MergeFields()
to list them.
Question: Can I merge images or formatting?
Answer: Merge fields are text only. For images, you’d insert programmatically.
Question: Can I use Excel as a data source?
Answer: Yes — load Excel using UniOffice’s .xlsx
API or Go’s excelize
, then build your replacement map.
Best Practices & Tips
Name your fields clearly: Match Excel headers or your JSON keys.
Test small: Log your merge fields before replacing.
Validate output: Open
merged.docx
in Word to check formats.Handle missing data: Fill map with default values to avoid blank fields.
Reuse your code: Wrap merge logic in a function.
Why UniOffice Stands Out
No Word/DOCX dependency: It’s pure Go ().
High performance: Good speed even with many rows ().
Scalable: You can run this in microservices or batch jobs.
Flexible: Integrate with web, email and other systems.
Quick Cheatsheet (“Word Mail Merge” vs. “UniOffice Mail Merge”)
Word Mail Merge: GUI → Mailings tab → select document → insert fields → finish & merge.
UniOffice Mail Merge:
Code: load template.
Read fields.
Build replacement map.
Call
MailMerge()
.Save the merged
.docx
.
Sample Full Code
package main
import (
"github.com/unidoc/unioffice/v2/common/license"
"github.com/unidoc/unioffice/v2/document"
)
func init() {
license.SetMeteredKey(os.Getenv("UNIDOC_LICENSE_API_KEY"))
}
func merge(templatePath, outputPath string, data map[string]string) error {
d, err := document.Open(templatePath)
if err != nil { return err }
defer d.Close()
for _, f := range d.MergeFields() {
if val, ok := data[f]; ok {
data[f] = val
}
}
d.MailMerge(data)
return d.SaveToFile(outputPath)
}
func main() {
data := map[string]string{
"Title": "Ms.",
"FirstName": "Alice",
"LastName": "Doe",
}
if err := merge("mm.docx", "alice.docx", data); err != nil {
panic(err)
}
}
Getting Help & Next Steps
Visit UniDoc’s mail merge guide.
Try other examples in the
unioffice-examples/document
folder.Join GitHub or UniDoc forums for help.
Explore advanced features: tables, images, styles.
Conclusion
You now know:
What mail merge in Word is.
How to do Word mail merge via UniOffice.
How it compares to Microsoft Word mail merge skills.
That you can automate mail merge in Word on servers using Go.
In short, learning how to do a mail merge in Word using UniOffice lets you build powerful, scalable document solutions.