Adding a field to .docx

Hi LibreOffice Gurus!
I am wanting to add a field “CustomerCustomerNumber” to a .docx.
So as to have the field «ContactContactNumber» to appear below the «ContactPostalAddress». When the “View > Field Shadings” is highlighted then the field will appear as «ContactContactNumber»
I have attempted this via “Inset > Field > more Fields…” then “Variables > Type (User Field) > Format (General)”. This doesn’t enable the appearance of the field as «ContactContactNumber».
Here is a screenshot of the document:

LibreOffice details:
Version: 25.8.4.2 (X86_64)
Build ID: 290daaa01b999472f0c7a3890eb6a550fd74c6df
CPU threads: 32; OS: Linux 6.18; UI render: default; VCL: gtk3
Locale: en-AU (en_AU.UTF-8); UI: en-US
Calc: threaded

thanks in advance for your help!

edit: adding .docx
Invoice.docx (24.6 KB)

There are two flaws in your question.

View>Field Shadings will only highlight locations where you have fields. If you want to see the field name, enable View>Field Names but this will mask field values.

More seriously, your existing fields are not variables. They are column names of records in a database. Therefore, you insert them from the Database tab instead of Variables. The database is not attached to your question and I can’t guess how it is structured. I suppose there are at Least Contact, Invoice, Organisation and Tax tables. I let you do the insertion.


Apparently, you are under Linux. Why do you save DOCX? This is an alien format needing conversion on open and save, with the risk of document corruption. Yes, your document is “simple” but it uses database fields, which is an advanced feature. Therefore, I would question the reliability and long-term stability across conversions. Spare yourself potential trouble and save .odt.

And considering the simplicity of your document, I’d suggest you recreate it wit the DB frontend Base (instead of Writer) which has a very nice report generator.

If these are custom fields or data from a database, I’m afraid they won’t be saved correctly in a .docx file.
I opened your document in Writer and couldn’t find any such fields.


If you are working with Writer, you should always save in the native ODT format!

How to open files from MS-Office 2007 or 2010 (.DOCX, .XLSX,…)?

This document is designed for use with Mail Merge technology, which is supported in both MS Office and LO.
To generate documents, a file containing a data source (e.g., a spreadsheet) is also required.
If you can upload a sample data source file (e.g., .xlsx), we can describe the technique for migrating Mail Merge from MS Office to LO.

Hi ajlittoz, Hrbrgr and sokol92
Thank you for taking the time to send thoughtful replies!
The reason this is a DOCX is that this file is the invoice template given to me by SAS accountancy provider Xero. Xero is the choice of my accountant, so I have to use this SAS. If the accountant used a Linux based accountancy package, I would use that.
Yes, I agree, using DOCX is alien and risks corruption of data. However, Xero will only accept a DOCX upload.
I want to have the customers phone number printed on the paper invoice that is generated from Xero. The only way Xero have for me to do this is to include the field «ContactContactNumber» in the DOCX and return it to Xero.

sokol92 As Xero is an online accountancy package, I can’t supply the database or a sample data source. Though this has given me idea on how to solve this by making a dummy DB. TY!

ajlittoz your idea of using Base to make a DB is brilliant!
Xero publish all the fields Xero invoice fields

I look at how to do this tomorrow.

kind regards

russelld

Solution is:

  1. Locate and download fields from SAS website
  2. Create spreadsheet with fields, with each column headed with field name
  3. Save spreadsheet as “Invoice_Fields.ods”
  4. Open template “invoice.docx” in Librewriter
  5. Click where to locate the new field in “invoice.docx”
  6. Open Fields dialog box with Ctrl+F2
  7. Open tab “Database”, the in “type” select “Mail merge fields”
  8. Below box “Database Selection”; click “Browse” to add the spreadsheet above “Invoice_Fields.ods”
  9. When spreadsheet is added, click the down arrow next to the spreadsheet name
  10. Select “Sheet1” and click down arrow
  11. Select “ContactContactNumber”
  12. On bottom Right: Click “Insert”
  13. Save document
  14. Upload to SAS
  15. Test by making a dummy sale and print the invoice

Many thanks to ajlittoz, Hrbrgr and sokol92 for hints and suggestions on how to solve this.
kind regards
russelld

1 Like