How To Integrate Excel Data Into A Word Document

Ads by Google

integrate excel into wordDuring your work week, there are probably lots of times that you find yourself copying and pasting information from Excel into Word, or the other way around.

This is how people often produce written reports based on data that’s accumulated and updated in an Excel spreadsheet. We’ve offered a lot of really cool tips and tools for Excel over the years, such as Saikat’s article on producing professional looking charts, and Steve’s article on cool Excel templates for project management.

In this article, I’m going to dive a little more into the background VBA scripting that allows you to actually program connections between data that might be stored in an Excel file and Word documents where you may be producing reports.

It’s actually surprisingly easy to integrate Excel data into Word once you know how to add the right references, and how to lay out the syntax of the background VBA code.

Integrating Data From Excel Into Your Word Document

In this example, I’m going to start out with a fairly simple Excel spreadsheet. In reality, the Excel file can consist of multiple spreadsheets with lots of data, it doesn’t matter.

So long as you know where the data resides in the spreadsheet, you’ll be able to reach in and grab it using VBA.

Ads by Google

Here’s what my sample spreadsheet looks like. It’s a list of expense totals that have been calculated throughout the entire year.

integrate excel into word

Now, lets say you have a manager that would like to see a nicely formatted report that describes the expenses, grouping together like items and laying it all out in a layout that’s a little more pleasing to the eye (and easier for the big boys upstairs to understand).

You can do this by incorporating objects like textboxes or labels into your Word document. When you’re in Word, just click on the Developer menu tab, and then select “Design Mode” in the Controls box. Use the Legacy Tools dropdown icon to find where you can insert labels into your document.

move excel data to word

Once you have the label placed in the document where you want it (not always an easy task), you’re ready to program the data feed. But first you’ll need to name the label so that the VBA can identify it. Right click on the label and go into Properties. Find the “(Name)” field and call it something that you’ll remember.

Now, add a Command Button from the same Legacy Tools dropdown list, place it in your document, and double click it to open up the VBA editor. When you get your code working later, you can modify it so that the code runs on the Document Open() event. You’ll see that in the object dropdown boxes in the editor window.

To get started connecting Word to Excel, you’ll need to reference Excel. Click on Tools, and then References. Scroll down the list until you see the “Microsoft Excel 12.0 Object Library” and select it.

move excel data to word

Once you’ve done this, the rest is just a matter of writing a ridiculously simple VBA script to pull in data from an Excel spreadsheet and automatically update the label caption with the data. Here’s how that works.

Private Sub CommandButton1_Click()
Dim objExcel As New Excel.Application
Dim exWb As Excel.Workbook

Set exWb = objExcel.Workbooks.Open("c:\temp\expenses.xls")

ThisDocument.yrTotal.Caption = exWb.Sheets("Sheet1").Cells(12, 2)

exWb.Close

Set exWb = Nothing

End Sub

See how that works? The “exWb” Excel application object opens the Excel file at the path you provide it, and it’ll go right into the specific sheet and cell number, extract the data, and place it into the Caption property of the label that you named “yrTotal“.

Here’s the VBA macro in action.

move excel data to word

The hard part with dealing with labels in Word is that it’s sometimes hard to align it at the end of a sentence or alongside any other text.

One way of overcoming that is actually incorporating some of the text alongside the data in the VBA code itself.  As you can see here, I’ve put the static text right into the Caption when I create the label itself.

excel data to word

Now, all you have to do is include that text when you update the label with your VBA script, and just append the data from the Excel file to the end of that text. Here’s what that kind of code would look like.

Dim objExcel As New Excel.Application
Dim exWb As Excel.Workbook

Set exWb = objExcel.Workbooks.Open("c:\temp\expenses.xls")

ThisDocument.yrTotal.Caption = exWb.Sheets("Sheet1").Cells(12, 2)
ThisDocument.totHotels.Caption = "Hotels: " & exWb.Sheets("Sheet1").Cells(5, 2)
ThisDocument.TotDining.Caption = "Dining Out: " & exWb.Sheets("Sheet1").Cells(2, 2)
ThisDocument.totTolls.Caption = "Tolls: " & exWb.Sheets("Sheet1").Cells(3, 2)
ThisDocument.totFuel.Caption = "Fuel: " & exWb.Sheets("Sheet1").Cells(10, 2)

exWb.Close

Set exWb = Nothing

You can use the string concatenation “&” symbol to place connect the static text with the data extracted from the Excel sheet. Here’s what the final results look like in the updated Word document.

integrate excel into word

Again, if you don’t want to have a big, ugly grey command button in your word document, just have the data-update script run on Document.Open(), and it’ll all take place behind the scenes.

In fact, in many cases you could create the initial document once, and then you’ll never have to create it again. All you’ll have to do is open it, and all of the labels will automatically update for you with the data from the updated Excel file. All you have to do is click to Print, and submit the report to your manager. A 30 minute report just turned into a 1 minute printout!

Can you think of any other cool uses for this data-integration technique using VBA? Share some of your own ideas and thoughts in the comments section below.

Shutterstock

Join live MakeUseOf Groups on Grouvi App Join live Groups on Grouvi
Windows Hacks & Customization
Windows Hacks & Customization
97 Members
Windows_10
Windows_10
87 Members
Best Windows Software
Best Windows Software
59 Members
Windows Troubleshooting
Windows Troubleshooting
59 Members
Windows Firewalls & Antivirus
Windows Firewalls & Antivirus
19 Members
Ads by Google
Comments (11)
  • Katherine Parra

    Hello Ryan!
    Is it possible to direct the script to a file/row defined by me with a cell value?
    I’m right now working in the script and it works perfect for the cell specified in the script.
    Let me explain myself, I have a report where I have to add birthdate, an unique ID, gender and due date. This is all in a excel sheet that is continuously updated, all the info I need to transfer to my report is in 1 row that starts with an specific number (test number). So what I want is to open the report write number of test an get the rest by clicking from the excel sheet.
    Is that possible?
    Thanks
    KP

  • Juli

    Holy wow. My boss just walked in and did this EXACT THING:
    “Now, lets say you have a manager that would like to see a nicely formatted report that describes the expenses, grouping together like items and laying it all out in a layout that’s a little more pleasing to the eye (and easier for the big boys upstairs to understand).”

    I was able to google it in like 3 seconds, find this article, and send it to him. Thanks for making me look awesome and predicting precisely what we need!

  • Steve Sullivan

    Sorry, I meant Ryan!

  • Steve Sullivan

    Thanks Brian. Is the any way to make add a conditional (IF) statement to suppress lines that have a .00 total?

    Steve

  • zem

    (“C:UsersZESDesktopCOSTINGTRUNIONGANTRY2.xlsx”) I am getting an error for this path.Help.

Load 10 more
Affiliate Disclamer

This review may contain affiliate links, which pays us a small compensation if you do decide to make a purchase based on our recommendation. Our judgement is in no way biased, and our recommendations are always based on the merits of the items.

For more details, please read our disclosure.
Affiliate Disclamer

This review may contain affiliate links, which pays us a small compensation if you do decide to make a purchase based on our recommendation. Our judgement is in no way biased, and our recommendations are always based on the merits of the items.

For more details, please read our disclosure.