Field Code Question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like to have a field within my Word 2003 document that shows the
previous month as well as year, and will update automatically each time I
open the file. For example, if the file were to be opened in January, 2008,
I want the field code to display December, 2007; opened in October, 2007
would display September, 2007, etc.

Can this be done with a field code?

Thank you.
 
I don't know of a field code that will perform that function but the
following macro inserts a prior month/Current month at a user named bookmark
call DateField. The code is placed in the documents OnOpen event.
***********************************************************
Private Sub Document_Open()

Dim dtmToday As Date
Dim dtmPrior As Date
Dim strMonthCurrent As String
Dim strMonthPrior As String
Dim intYearCurrent As Integer
Dim intYearPrior As Integer
Dim strDateVal As String

dtmToday = Date
dtmPrior = DateSerial(Year(dtmToday), Month(dtmToday) - 1, _
Day(dtmToday))

strMonthCurrent = Choose(Month(dtmToday), "January", _
"February", "March", "April", "May", "June", "July", _
"August", "September", "October", "November", "December")
strMonthPrior = Choose(Month(dtmPrior), "January", _
"February", "March", "April", "May", "June", "July", _
"August", "September", "October", "November", "December")
intYearCurrent = Year(dtmToday)
intYearPrior = Year(dtmPrior)

strDateVal = strMonthPrior & "/" & intYearPrior & " - " & _
strMonthCurrent & "/" & intYearCurrent

ActiveDocument.Bookmarks("DateField").Range.Text = strDateVal

End Sub
***********************************************************
 
I don't know of a field code that will perform that function but the
following macro inserts a prior month/Current month at a user named bookmark
call DateField. The code is placed in the documents OnOpen event.
***********************************************************
Private Sub Document_Open()

Dim dtmToday As Date
Dim dtmPrior As Date
Dim strMonthCurrent As String
Dim strMonthPrior As String
Dim intYearCurrent As Integer
Dim intYearPrior As Integer
Dim strDateVal As String

dtmToday = Date
dtmPrior = DateSerial(Year(dtmToday), Month(dtmToday) - 1, _
Day(dtmToday))

strMonthCurrent = Choose(Month(dtmToday), "January", _
"February", "March", "April", "May", "June", "July", _
"August", "September", "October", "November", "December")
strMonthPrior = Choose(Month(dtmPrior), "January", _
"February", "March", "April", "May", "June", "July", _
"August", "September", "October", "November", "December")
intYearCurrent = Year(dtmToday)
intYearPrior = Year(dtmPrior)

strDateVal = strMonthPrior & "/" & intYearPrior & " - " & _
strMonthCurrent & "/" & intYearCurrent

ActiveDocument.Bookmarks("DateField").Range.Text = strDateVal

End Sub
***********************************************************
--
Kevin Backmann







- Tekst uit oorspronkelijk bericht weergeven -

Normaly you should use the field code
{DATE \@ "yyyy MMMM dd"}
to get the day you insert the field {DATE \@ "d"}
to get the monthnumber { DATE \@ "M" }
And now you only need some math:
An if field {if { DATE \@ "M" }="3" "feb" "else, put another if then
else here"}
But, you can insert if fields after each other as well:
{if { DATE \@ "M" }="3" "feb" ""}{if { DATE \@ "M" }="4" "mar" ""}{if
{ DATE \@ "M" }="5" "may" ""}
And then, you need some math to get the right month and year in
december/ january

Good luck!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top