Microsoft Word Application to Generate Form Letters
An exercise for a beginning programming class.
- This application is written with Visual Basic for Applications in Microsoft Word.
- There is one form which generates a form letter based on a template.
- The letter is completed with information that was entered in the form.
- Below is the form, followed by the code "behind" the form.
- The final image is the letter generated by this application.
Option Explicit
----------------------------------------------------------------------------------
' Run this procedure when the document opens.
Private Sub document_open()
' Select the current document and minimize it.
Documents(Me).Activate
ActiveWindow.WindowState = wdWindowStateMinimize
' --------------
' Assigns the module ShowForm() to the shortcut keys Alt+L
' Set the current document as the location of the code to be run.
CustomizationContext = ActiveDocument
' Define Alt+L as the KeyCode in the KeyBindings container.
KeyBindings.Add KeyCode:=BuildKeyCode(Arg1:=wdKeyAlt, Arg2:=wdKeyL), _
KeyCategory:=wdKeyCategoryMacro, Command:="ShowForm" ' Link the KeyCode to the module "ShowForm"
' --------------
' Now load and show the UserForm.
ShowForm
End Sub
----------------------------------------------------------------------------------
' This procedure loads and shows the UserForm "frmSalesLetter"
Sub ShowForm()
Load frmSalesLetter ' Loads the form into memory.
frmSalesLetter.Show ' Shows the form on the screen.
End Sub