How to Create a New Word Document from a Template in VBA

How to Create a New Word Document from a Template in VBA thumbnail
Typing in new VBA code to open a Word template.

Each of the programs in the Microsoft Office suite, such as Word or Excel, contains a stripped-down version of the Visual Basic programming language called VBA. You can create a piece of code called a macro using the VBA editor that will automate repetitive tasks such as opening up a new Word document that contains a pre-built template. Opening a new Word document through VBA is a quick process that only requires one line of simple code.

Instructions

    • 1

      Open up the Microsoft Office document you want to modify to contain the VBA code. Navigate to the tab labeled "Developer" and then click the link named "Visual Basic."

    • 2

      Create a new procedure by typing in the opening "Sub MyNewProcedure ()" command. You can change "MyNewProcedure" to any title you'd like to identify what the procedure does, such as "OpenTemplate" or something similar.

    • 3

      Hit the "Enter" key a three times to move down three lines. Type in the ending "End Sub" command to let the Visual Basic editor know where the procedure's code ends.

    • 4

      Move back up a line so that the cursor is positioned in between the opening and closing procedure commands.

    • 5

      Use the "Add" method of the "Documents" object to create a new Word document from an existing template. For example, type "Documents.Add Template:="C:\Program Files\Microsoft Office\Templates\1033\EquityMergeLetter.dotx" if you wanted to create a document using the "EquityMergeLetter" template.

    • 6

      Save your changes. Click the "Macro" button and then highlight the macro name you specified earlier in the opening procedure command. Click "OK" to run the macro and create the new Word document using the specified template.

Tips & Warnings

  • You can use the VBA code in the Visual Basic editor of any of the Microsoft Office programs. For example, you could open up a Microsoft Word template using the VBA code in a macro for the Excel spreadsheet software if you wanted to copy information from the spreadsheet to the Word template.

  • In Microsoft Office 2003 and earlier you can access the VBA editor by clicking "Tools," "Macros" and then "Visual Basic Editor."

  • You can find the names of the various available templates by navigating to the folder where you installed the Microsoft Office suite. Open the folder named "Templates" to see the list of templates.

Related Searches:

References

Resources

  • Photo Credit typing image by Aditia Patria Warman from Fotolia.com

Comments

You May Also Like

Related Ads

Featured