Multiple Apps

Create an instance of Word

Ease of Use

Intermediate

Version tested with

2002, 2003 

Submitted by:

Ken Puls

Description:

This code demonstrates how to create an instance of Microsoft Word from another office application. (It does, of course, require that you have Microsoft Word installed on your PC.) 

Discussion:

There may be times where you wish to open a word document and send something to it from Excel, Powerpoint, or any other MS Office program. This is a basic example of how to create an instance of Microsoft Word, and set it to visible. Demonstrations using both Excel and Powerpoint are included in the example files, although it will also work from Access, Publisher, or any other Office app. This code uses "Late Binding" to accomplish the task, which means that you do not have to set a reference to Word to use it. You should be aware, however, that if you include any word constants in your code, however, they will not compile and need to be replaced by their numerical equivalents. Help for this kind (and any other issues) can be found in the VBAExpress forums. 

Code:

instructions for use

			

Option Explicit Sub CreateWordInstance() 'Macro purpose: Create a Microsoft Word instance via code ' using late binding. (No references required) Dim wdApp As Object Dim docWord As Object 'Create a new instance of Word Set wdApp = CreateObject("Word.Application") 'Create a new document Set docWord = wdApp.Documents.Add 'Anything you want to do to the document should be done here docWord.Range.InsertAfter ("I was generated by a macro from Excel!") 'Set the instance of Word visible. (It's been hiding until now) wdApp.Visible = True 'Release the document and application objects to free up memory Set docWord = Nothing Set wdApp = Nothing End Sub

How to use:

  1. Copy above code.
  2. In the application press Alt + F11 to enter the VBE.
  3. Press Ctrl + R to show the Project Explorer.
  4. Right-click desired file on left (in bold).
  5. Choose Insert -> Module.
  6. Paste code into the right pane.
  7. Press Alt + Q to close the VBE.
  8. Save file before any other changes.
 

Test the code:

  1. Press Alt + F8 to display the Macro dialog box.
  2. Choose CreateWordInstance and click Okay.
 

Sample File:

CreateWordInstance.zip 17.36KB 

Approved by mdmackillop


This entry has been viewed 104 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express