Sunday, March 29, 2009

MOTD: Insert Merge Field

I've posted before about the amazing potential of Word's Mail Merge feature, I use it so much that I've created a macro to insert MergeFields when I'm creating a merge document. I then assign the macro to a shortcut so I can quickly insert these fields in my documents. I simply recorded a macro without editing any code so you can record it yourself using this post and this post, or you can put the following code in the Normal template and assign a shortcut to it (use this post):

Sub InsertMergeField() 'ALT CTRL M
' InsertMergeField Macro
' Macro recorded 3/13/2009 by Michael Shubeck
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"MERGEFIELD INSERT ", PreserveFormatting:=True
Selection.MoveLeft Unit:=wdCharacter, Count:=1
End Sub

Sunday, March 22, 2009

Macro of the Day (MOTD): Keybord Shortcut For Envelope Printing

I do a lot of envelope printing in my job and I use Word's Envelope Wizard to do most of my envelope printing. I don't know why they have a whole wizard for this though. I would prefer if I could simply highlight the person's address in the letter I was drafting, type a shortcut, and have it print. So, I created a macro. Type Alt + F11 to open the Visual Basic editor. I would put this code in the Normal template. The Normal template is the basis for all Word documents. By putting code there, it will run in any document you are using.

In the Project window on the left side of the Visual Basic editor, open up the Normal tab and the Microsoft Word Objects tab under that. Double click on the ThisDocument icon. Paste the following code in.

Sub PrintEnvelope()
' PrintEnvelope Macro
' Macro recorded 3/10/2009 by Michael Shubeck
ActiveDocument.Envelope.PrintOut ExtractAddress:=False, OmitReturnAddress _
:=True, PrintBarCode:=False, PrintFIMA:=False, Height:=InchesToPoints( _
4.13), Width:=InchesToPoints(9.5), Address:=Selection.Text, AutoText _
:="ToolsCreateLabels3", ReturnAddress:= _
"The Law Offices of Gregory A. Yates", ReturnAutoText:= _
"ToolsCreateLabels2", AddressFromLeft:=wdAutoPosition, AddressFromTop:= _
wdAutoPosition, ReturnAddressFromLeft:=wdAutoPosition, _
ReturnAddressFromTop:=wdAutoPosition, DefaultOrientation:=wdLeftLandscape _
, DefaultFaceUp:=False, PrintEPostage:=False
End Sub

Now lets hook the macro up to a keyboard shortcut. In Word 2007, click on the Office Button Then click on Word Options at the bottom of the menu that pops out. Then hit the Customize tab. Finally, at the bottom of the Customize window, hit the customize keyboard shortcuts button. In the Categories box, scroll down to Macros. Then, in the Macros box, select our PrintEnvelope macro. Now, click in the Press New Shortcut Key box. Type the key combination that you want to assign to the macro. If the key combination is already used it, the window will show text that says "Currently assigned to." Usually I just replace what was assigned by the Microsoft people because they have shortcuts for features I will never use. Finally, hit the Assign button (don't forget this - I do all the time).

Now you can highlight a person's address in your document, use your shortcut, and the envelope will print.

As a final note, watch out where the envelpe prints if you have multiple printers installed. The envelope will print to the printer listed in the print menu.

Sunday, March 15, 2009

MOTD: Autoupdate fields on save

This will be the first of a series of posts on cool Microsoft Word macros I have been using to make me more efficient at producing documents and cut down on embarrassing errors. Bill and I have been preaching the benefits of macros for a while on this blog. Check this post out for an introduction.

I use templates for all of my commonly used documents, from my basic letter or fax template to more complex pleading documents. I have discussed how to create templates before. Word allows you to insert blanks called fields in a document. These fields will automatically fill in information at certain times. In many of my templates I have fields that automatically fill in the date or generate tables of contents, among other things. These fields all need to be manually updated by right clicking on them and clicking update. Sometimes I forget to update the field before printing so I created a macro to automatically update the fields when I hit the save button. Credit goes to these blog posts: 1, 2.

For experimentation, create a template using the procedures from the creating templates tutorial. Then insert a save date field in that template. In Office 2007, you go to the Insert tab on the Ribbon bar . Next, go to the Quick Parts button . Then select the Field menu item.

In the Field dialog, select
SaveDate. Select whatever date format you desire.

A SaveDate field displays the date the document was saved (after it is updated) and, thus, is handy for letters and other documents that are printed and sent on the date they are saved.

Now lets bring up the Visual Basic editor so we can start adding some code, type Alt + F11. In the Project window on the left, find the letter project and right click on it. Next click insert -> module.

A new item called a code module will be inserted under your project in a modules folder. It will be named Module1.

Now insert a new class module just like you inserted the code module. The class module will be named Class1.

Your Project window should look like this:

The specifics of what ThisDocument, Module1, and Class1 are not very important at this point so I wont go into detail. We will just paste code to make things work. If you have a desire to understand code, see here.

Double click on the ThisDocument icon in the Project window. Now paste the following code into the code window:

Private Sub Document_New() 'has to be Document_New not Document_Open for it to work in a template

    Call Register_Event_Handler

End Sub

Now double click on the Module1 icon and paste this code in:

Dim X As New Class1
Public Sub Register_Event_Handler()
    Set X.App = Word.Application
End Sub

Finally, paste this code into Class1:

Public WithEvents App As Word.Application

Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)


End Sub

Sub UpdateAll()
Dim oStory As Range
Dim oField As Field
Dim bSaveDate As Boolean
bSaveDate = False

On Error Resume Next
For Each oStory In ActiveDocument.StoryRanges
For Each oField In oStory.Fields
If oField.Type = wdFieldSaveDate Then
    bSaveDate = True
End If
Next oField
Next oStory
If bSaveDate = True Then
    MsgBox "Save a second time to update a save date field"
End If
On Error GoTo 0
End Sub

SaveDate fields present a special paradox. The document has to be saved before the field can be updated. I have the following code to remind me to press save twice to update the field:
If bSaveDate = True Then
    MsgBox "Save a second time to update a save date field"
End If

Now save the template and double click on the template file to create a new document using that template. When you hit save the message box will remind you to save a second time, and when you do, the date field will be updated automatically.

This same code will automatically update tables of contents, numbering and any other Word field that automatically generates information.