Fun with VBA...
Apr. 16th, 2012 02:27 pmAs time goes by and my translations come more fluidly, it occurs to me that the ratio of time spent formatting to the time spent translating has gone up. Several months ago, I created a set of Styles in Word that reflected the formatting desires of the end client, and since then, the need to pause and take care of things that—frankly—can be automated seems to weigh ever heavier on my psyche.
Take, for example, the task of creating footers. The client likes to see a footer with "Received on MM/DD/YY" in the lower left corner, and a page number in the lower right corner. The conventional way to go about doing this is to click on the Insert tab on the Ribbon, then on Footer, then on Edit Footer, at which point—if your machine is like mine, you've experienced two pauses while code loads—you're ready to make sure the footer is 0.5 inch from the bottom of the page and, of course, to start actually typing the silly text! Inserting the page number involves some more mouse clicks.
There is a better way. With any luck, my comments in the following VBA code ought to be self-explanatory.
e.g., returning the cursor to the original insertion point after running the macro DONE! Leave a comment if you're interested. :^).
One step at a time, that's the ticket!
P.S. UPDATED 04/17 to "zero out" the contents of the previous footer.
Take, for example, the task of creating footers. The client likes to see a footer with "Received on MM/DD/YY" in the lower left corner, and a page number in the lower right corner. The conventional way to go about doing this is to click on the Insert tab on the Ribbon, then on Footer, then on Edit Footer, at which point—if your machine is like mine, you've experienced two pauses while code loads—you're ready to make sure the footer is 0.5 inch from the bottom of the page and, of course, to start actually typing the silly text! Inserting the page number involves some more mouse clicks.
There is a better way. With any luck, my comments in the following VBA code ought to be self-explanatory.
Sub Insert_Client_Footer()Doubtless, there are other tweaks that might become worth my while to implement should not having them start to bug me (
' Insert a client-specific footer in the ActiveDocument
' Execute code at Handler if Style not present
On Error GoTo Handler
' Set footer distance to 0.5 inch.
ActiveDocument.PageSetup.FooterDistance = InchesToPoints(0.5)
' Select the footer in the document's first section.
' (This will need tweaking for multi-section docs where footers
' in later sections do not "link to previous" footers.)
ActiveDocument.Sections(1) _
.Footers(wdHeaderFooterPrimary).Range.Select
' Delete whatever information was in the previous footer
Selection.Delete
' Apply the style (loaded previously) and construct the footer
With Selection
.Style = "Client-Footer-Style"
.TypeText "Received on "
.TypeText Date
.TypeText vbTab
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
Text:="PAGE "
End With
Exit Sub
' If the Client-Footer-Style is not present, it will be added
' and execution will resume. The correct style will display when
' a style sheet with the Client-Footer-Style is loaded.
Handler:
If Err = 5834 Then
ActiveDocument.Styles.Add _
Name:="Client-Footer-Style", Type:=wdStyleTypeParagraph
Resume
End If
End
One step at a time, that's the ticket!
P.S. UPDATED 04/17 to "zero out" the contents of the previous footer.