Sunday, April 12, 2009

MOTD: Auto-update the date

I've talked before about making Word templates out of commonly used documents. I've also talked about date fields and SaveDate fields. Some documents might use language like "Executed this 15th day of March, 2009." There isn't a good way to have the day read "2nd", "3rd", "4th" ect. Now most people might think this isn't a big deal, its easily changed manually. But the whole point of my template is to cut down on ugly typos when I forget to update something. So, I created a macro.

In my template, I inserted a date field by going to Word's Insert tab and clicking Quick Parts and then selecting Date from the Field names list (you can use SaveDate if you want - see this post for more about the SaveDate field). The Date field displays the current date. The Date formats box is used to control how the date is displayed. The box uses a code system; "d" stands for day, "M" stands for month, "y" stands for year, "h" stands for hour, "m" stands for minute, and "s" stands for second. Note that you capitalize M for month because lower case m stands for minute. The formating is a little difficult to explain so examples are the only way to go. If you want the date to be formatted like "3/15/09", type M/d/yy. For "3/15/2009," type M/d/yyyy. For "Sunday, March 15, 2009," type dddd, MMMM dd, yyyy. Most of the common formats are in the list so you don't generally need to figure the formatting out. However, I want to do something out of the ordinary. I want the document to read something like "Executed on the 1st," so clear out the Date formats box and just place a "d" there.


Right after the Date box we just inserted, insert a MacroField by selecting MacroField from the Field names list. In the Display text box put something like "st" (because this box will say "st", "nd", or "rd"). In the Macro name box just leave the default there (AcceptAllChangesInDoc) because we will add our own Macro next. Your document looks something like this:

Click on the "st" and type Shift + F9. This will expand the MacroField to display its code. Replace AcceptAllChangesInDoc with DateSuperScript - the name of the macro we will make next.


I want the month and the year at the end so I insert a second Date field using the code MMMM, yyyy.



The finished product looks like this:

Now type Alt + F11 to open up the Visual Basic editor. Then select the Project template for your document in the project window (it will have the name of your Word document). Expand the project. Expand the Microsoft Word Objects tab and select the ThisDocument icon and then paste this code in the document:

Sub AutoSuperScript()
Dim oStory As Range
Dim oField As Field
For Each oStory In ActiveDocument.StoryRanges
For Each oField In oStory.Fields

If InStr(1, oField.Code.Text, " MACROBUTTON DateSuperscript") Then
oField.Code.Text = " MACROBUTTON DateSuperscript " + GetDateSuperscript
End If
oField.Update
Next oField
Next oStory
End Sub

Private Sub Document_New()
AutoSuperScript
End Sub

Private Function GetDateSuperscript()
'1-> st
'2->nd
'3->rd
'0, >3 ->th
strDt = Str(Day(Now))
strEnd = Right(strDt, 1)
If strEnd = 1 Then
GetDateSuperscript = "st"
ElseIf strEnd = 2 Then
GetDateSuperscript = "nd"
ElseIf strEnd = 3 Then
GetDateSuperscript = "rd"
Else
GetDateSuperscript = "th"
End If
End Function


Now every time you create a new document from your template the date will be updated. If you really want to get wild, add the AutoSuperScript function to the UpdateAll function I talked about in this post. That way, the superscript will get updated every save.

Sunday, April 5, 2009

MOTD: Print Current Page Shortcut

I use the print current page setting in the Print dialog of Word enough that I recorded a macro for it. 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 PrintCurrent()
'
' PrintCurrent Macro
' Macro recorded 3/9/2009 by Michael Shubeck
'
Application.PrintOut FileName:="", Range:=wdPrintCurrentPage, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _
False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0
End Sub