atw: Word Macro to Complete the Dates in an Invoice from the Filename ...

  • From: Neil Maloney <maloneyn@xxxxxxxxxxx>
  • To: austechwriter@xxxxxxxxxxxxx
  • Date: Wed, 04 Aug 2010 20:12:17 +1000

 Just in case this code is useful to anyone on the list ...

I have always included the date range for my invoices in the invoice filename as well as in the invoice body text. The date details in the filename are in parentheses, e.g. "(19 July - 1 August)".

While I always update the filename when copying and then changing an invoice document for a new billing period, I sometimes (reasonably regularly) forget to update the date range in the body text.

So, I have just written a little macro that reads the filename when the document is open, parses it for the date -- based on the date being included between an opening and closing parenthesis -- and then saves the result in the Subject field in Document Properties.

I have added a "{subject}" field to my invoices, where the date used to be in plain text. I have also added the macro to a toolbar and called it "Update Invoice".

After inserting the date result into the Subject field in Document Properties, the macro selects the entire document and updates all fields within it. Voilà, I have the date range filled in automatically for me ... now if I can only remember each time to click the Update Invoice button !!!

The code (based on VBA for Word 2003) is shown below.

In relation to the recent thread on typefaces, I thought Peter Martin was a font of wisdom, but I also liked Geoffrey Marnell's style ...

Neil.


Sub Update_Invoice()

FileName = Documents(1).Name
Find_the_Opening_Parenthesis:
FirstMarker = FirstMarker + 1
    Character = Mid(FileName, FirstMarker, 1)

    If Character = "(" Then
        SecondMarker = FirstMarker
        FirstMarker = FirstMarker + 1
Find_the_Closing_Parenthesis:
    SecondMarker = SecondMarker + 1
        Character = Mid(FileName, SecondMarker, 1)

        If Character = ")" Then
        Else
            GoTo Find_the_Closing_Parenthesis
        End If

    Else
        GoTo Find_the_Opening_Parenthesis
    End If

Length = SecondMarker - FirstMarker
DateRange = Mid(FileName, FirstMarker, Length)

With ActiveDocument
    .BuiltInDocumentProperties(wdPropertySubject) = DateRange
End With

    Selection.WholeStory
    Selection.Fields.Update

End Sub

Other related posts:

  • » atw: Word Macro to Complete the Dates in an Invoice from the Filename ... - Neil Maloney