Word – Getting data from MySQL into Microsoft Word

microsoft wordMySQLvba

I am preparing a Word document with fields that will be populated from MySQL. As an example, I have the following sentence in my document:

The value of your portfolio is $xxxx.

xxxx is the result of the following query which I would like to send to MySQL:

SELECT sum(colA) WHERE colB='yourname' colC='equity'

So my questions are:

  1. How to connect Word to MySQL?
  2. How to send queries to MySQL to get results?
  3. How to create and use merge fields to display those results dynamically in my document?

Some more info:

I am trying to create an automated dynamic report template in Word. I want to be able to copy over this template, change a few parameters and then refresh the connection to MySQL. The MySQL 'fields' (like xxxx above) in the template will be refreshed automatically. The copy of the Word document will then be converted to pdf and emailed to the audience.

Any help will be greatly appreciated. Thanks in advance.

Best Answer

You HAVE to have the MariaDB ODBC connector

Here is the connector 32 bit Here is the connector 64 bit

The MariaDB is a fork of the mysql project so this connector should work just fine for mysql.

Had a bizarre error where it wouldn't save the dsn connection setting up from control panel before going into MS word

ODBC tools ODBC tools

Pre-existing connection(s) shown here Pre-existing connection(s) shown here

MariaDB connection MariaDB connection

Name and description Name and description

Database login details Database login details


Options Options

Optional encryption Optional encryption

More options More options

DSN setup DSN setup

Launch word

New Source New Source

Advanced Advanced

specify DSN from before specify DSN from before

Select table(s) Select table(s)

Final DONE!!! :) DONE!

Here are all the columns in my table Here are all the columns in my table

Related Question