T
Tergiver
I have the following VBA functions which I use extensively in Access:
Public Function FirstXDayOfYear(year As Integer, xday As Integer) As Date
FirstXDayOfYear = DateValue("1/1/" + Str(year))
While (DatePart("w", FirstXDayOfYear) <> xday)
FirstXDayOfYear = DateAdd("w", 1, FirstXDayOfYear)
Wend
End Function
Public Function DateFromWeek(week As Integer, year As Integer) As Date
DateFromWeek = DateAdd("w", (week - 1) * 7, FirstXDayOfYear(year,
vbSaturday))
End Function
I have a word document that accesses a database to perform mail merges. I
cannot modify this database or the query used or I would add a field for the
information I need.
I know you can enter VBA code into a word document through the Visual Basic
Editor (Alt+F11), so I can get the code into the document, but I can't figure
out how to execute it from within the document.
What I need is essentially this field:
{ = DateFromWeek([Week], [Year]) }
Where [Week] comes from the mail merge and [Year] needs to be filled in by
the user. I use FILLIN fields often, so that part is easy, but I can't figure
out how to get the function called. I've tried every combination of syntax I
can imagine, but I either get !Syntax Error, or nothing at all from the
various fields I've tried. I've started with a blank document (inserted the
above functions) and specify explicit parameters:
{ = DateFromWeek(1, 2007) }
But it just won't work.
Note that I'm stuck using Word 2000 for this.
Public Function FirstXDayOfYear(year As Integer, xday As Integer) As Date
FirstXDayOfYear = DateValue("1/1/" + Str(year))
While (DatePart("w", FirstXDayOfYear) <> xday)
FirstXDayOfYear = DateAdd("w", 1, FirstXDayOfYear)
Wend
End Function
Public Function DateFromWeek(week As Integer, year As Integer) As Date
DateFromWeek = DateAdd("w", (week - 1) * 7, FirstXDayOfYear(year,
vbSaturday))
End Function
I have a word document that accesses a database to perform mail merges. I
cannot modify this database or the query used or I would add a field for the
information I need.
I know you can enter VBA code into a word document through the Visual Basic
Editor (Alt+F11), so I can get the code into the document, but I can't figure
out how to execute it from within the document.
What I need is essentially this field:
{ = DateFromWeek([Week], [Year]) }
Where [Week] comes from the mail merge and [Year] needs to be filled in by
the user. I use FILLIN fields often, so that part is easy, but I can't figure
out how to get the function called. I've tried every combination of syntax I
can imagine, but I either get !Syntax Error, or nothing at all from the
various fields I've tried. I've started with a blank document (inserted the
above functions) and specify explicit parameters:
{ = DateFromWeek(1, 2007) }
But it just won't work.
Note that I'm stuck using Word 2000 for this.