Insert value from VBA function into document.

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.
 
T

Tergiver

What is the function of VBA code in a Word document? If it cannot modify the
document's contents, I'm not able to see what purpose it can serve.
 
T

Tergiver

I rewrote this 'report' as an Access report and solved the problem that way.

I seem to be doing a lot of that lately. My biggest peave about Access
reports is that a control cannot switch fonts or font attributes
mid-sentence. That means if you have a sentence where you want to underline a
word or bold a few words, you have to make multiple text boxes and try to
line them up. If the mid-sentence value is calculated or comes from a
database, you either have to format it so that it's always the same number of
characters, or just live with the fact that your sentence appears broken up a
bit.

Is there a better way to make pretty reports that can use both calculated
and data access information?
 

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

Top