Have you ever wanted to automatically create reports or cover letters in Word using data compiled in Excel? With Excel VBA, you can control the Word application behind the scenes to automate everything from document creation to text entry and saving.
In this article, I will explain the process of launching Word, writing text to a new document, and saving it using VBA, in a way that is easy for beginners to understand.
Preparation: About Reference Settings (Recommended)
There are two main ways to control other Office applications with VBA: Late Binding and Early Binding.
The code in this article uses “Late Binding,” which does not require any special settings. However, if you are doing serious development, “Early Binding” is recommended.
To use Early Binding:
- Open Tools > References in the VBE (Visual Basic Editor) menu.
- Check the box for “Microsoft Word XX.X Object Library” (where XX.X is the version number).
This enables code auto-completion (IntelliSense) and significantly improves development efficiency.
The Completed VBA Code
Below is the code to operate Word from Excel, create a new document, and save it.
Sub CreateWordDocumentFromExcel()
' Declare variables
Dim wordApp As Object ' Word Application
Dim wordDoc As Object ' Word Document
' --- 1. Launch Word Application ---
' Create an instance of Word using CreateObject
Set wordApp = CreateObject("Word.Application")
' Uncomment the following line if you want to see the Word screen during processing
' wordApp.Visible = True
' --- 2. Add a new document ---
Set wordDoc = wordApp.Documents.Add
' --- 3. Manipulate the Word document ---
With wordDoc
' Enter text at the beginning of the document
.Range(0, 0).Text = "This document was created automatically from Excel VBA." & vbCrLf & "This is the second line."
' Save the document (Save in the same folder as this Excel file)
.SaveAs ThisWorkbook.Path & "\VBA_AutoCreated_Doc.docx"
' Close the document
.Close
End With
' --- 4. Quit Word and cleanup ---
' Quit Word Application
wordApp.Quit
' Release object variables
Set wordDoc = Nothing
Set wordApp = Nothing
MsgBox "Word document creation is complete."
End Sub
Explanation of Key Points
1. Launching the Word Application
Set wordApp = CreateObject("Word.Application")
' wordApp.Visible = True
When you run CreateObject("Word.Application"), Word launches in the background in a hidden state (invisible to the user). The control of this instance is stored in the wordApp variable.
If you want to check the behavior during development, uncomment wordApp.Visible = True. This will show the Word window, allowing you to visually see VBA operating the application.
2. Adding a New Document
Set wordDoc = wordApp.Documents.Add
By executing the .Documents.Add method on the launched Word application (wordApp), a new blank document is created. Control of that document is stored in the wordDoc variable.
3. Entering Text (.Range)
.Range(0, 0).Text = "..."
The .Range(Start, End) method specifies a specific range within the document. Specifying .Range(0, 0) means “the very beginning of the document.” By assigning a string to its .Text property, text is entered at that position. vbCrLf is the line break code in VBA.
4. Saving and Quitting
.SaveAs ThisWorkbook.Path & "\VBA_AutoCreated_Doc.docx"
.Close
wordApp.Quit
Use .SaveAs to save the document with a filename, and .Close to close that document.
The most important part here is executing wordApp.Quit at the end. If you forget this, the hidden Word process will remain in your PC’s memory, which causes resource consumption.
5. Releasing Objects
Set wordDoc = Nothing
Set wordApp = Nothing
Finally, assign Nothing to the object variables to completely release the memory. It is good practice to remember this as a standard procedure when manipulating external objects in VBA.
Summary
The basics of controlling Word from Excel VBA are:
- Launch Word with
CreateObject. - Create a new document with
.Documents.Add. - Enter text and format using the document object’s properties and methods.
- Save with
.SaveAsand always close the application with.Quit.
By applying these basics, you can automate a wide range of tasks, such as transferring Excel cell data to Word tables or creating invoices by combining standard text with Excel values.
