In this lesson you will learn how to open an existing Word document or create a new Word document with Excel VBA. This involves several key steps. Below is a detailed guide to help you navigate this process effortlessly.
1. Accessing the VBA Editor
First you need to access the VBA editor inside your Excel application.
Open your Excel workbook. Press Alt+F11 to access the Visual Basic for Applications (VBA) editor.
2. Configuring References
Once you are inside the Visual basic environment, then you need to add reference to the Microsoft word object library.
Navigate to the "Tools" menu.
Select "References" from the dropdown list.
Ensure that the "Microsoft Word xx.0 Object Library" is checked by ticking the checkbox.
This step allows Excel VBA to interact with Word documents seamlessly.
3. Inserting VBA Code
In the VBA editor, locate the module where you want to insert the code or create a new module. Copy and paste the following VBA code into the module.
Dim wApp As Word.Application
Dim wDoc As Word.Document
'Create a new instance of the Word application
Set wApp = CreateObject("Word.Application")
wApp.Visible = True ' Make the Word application visible
'Open an existing Word document from your hard drive
Set wDoc = wApp.Documents.Open("D:\VBA LAB\Result.docx")
End Sub
This subroutine will open an existing Word document located at the specified file path ("D:\VBA LAB\Result.docx"). Modify the file path in the Open() method to point to the location of your specific Word document. Below is the detailed explanation of the code.
This line declares a variable named wApp as an object of type Word.Application. It is used to represent an instance of the Microsoft Word application.
This line declares another variable named wDoc as an object of type Word.Document. It will be used to represent a specific Word document.
Here, a new instance of the Word application is created using the CreateObject function. The "Word.Application" parameter specifies that we want to create an instance of the Word application. The Set keyword assigns this newly created instance to the wApp variable.
This line sets the Visible property of the Word application to True. It ensures that the Word application window is visible to the user when it is opened.
This line opens an existing Word document located at the specified file path. The Set keyword assigns the opened document to the wDoc variable. The Documents.Open method is used to open the document within the Word application instance represented by wApp.
In summary, this code initializes variables for representing the Word application (wApp) and a specific Word document (wDoc). It then creates a new instance of the Word application, sets it to be visible, and opens an existing word document.
If you want to create a new word document instead of opening an existing word document, then you can slightly modify the above code as follows.
Dim wApp As Word.Application
Dim wDoc As Word.Document
'Create a new instance of the Word application
Set wApp = CreateObject("Word.Application")
wApp.Visible = True ' Make the Word application visible
'Create a new Word document
Set wDoc = wApp.Documents.Add
End Sub
In this subroutine, only difference is we have
Set wDoc = wApp.Documents.Add
Instead of
Set wDoc = wApp.Documents.Open("D:\VBA LAB\Result.docx")
This new line creates a new Word document by adding a document to the Documents collection of the Word application (wApp).