How to link a field with a VB MACRO ?

J

joes

Hello

I just have the stupid idea that I like to include dynamic content inmy
word template. My approach is to inlcude at some positions in my
document fields which are linked to a macro. The content of these felds
are set by the macro.

I went quickly through the field types of Word, but I found only the
MACROBUTTON which runs a MACRO but it shows always a static text. Is
there another command or way to achieve my goal?

If someone could provide me an example would be very nice.

many thanks
Joes
 
W

Word Heretic

G'day "joes" <[email protected]>,

There are several ways to skin this cat:

1) Macro buttons - ugly but they sometimes are effective. Eg

Enter your Hyperlink here : ____________________ [GO]

2) Wizard: A VBA form that is invoked by document_open that steps user
through supplying the decisions to create the document.

3) Online form: Use the forms toolbar to embed formfields and then use
the OnEntry and OnExit events of the formfield to call your macros.

Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


joes reckoned:
 
J

joes

Many Thanks Steve, this brings me forward. I like to go for the third
option, but I have some more questions.
3) Online form: Use the forms toolbar to embed formfields and then use
the OnEntry and OnExit events of the formfield to call your macros.


- How can I specify which macro should be called during the "OnEntry"
and "OnExit" events. I saw it in the property window of the from field
but can this also be achieved programmatically rather than using this
dialog?

- Is there a way to pass parameters to my macro?


Many thanks
Joes
 
C

Cindy M -WordMVP-

<[email protected]>
<[email protected]>
Newsgroups: microsoft.public.word.vba.beginners
NNTP-Posting-Host: 243.197.62.81.cust.bluewin.ch 81.62.197.243
Path: number1.nntp.ams.giganews.com!number1.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newshub.sdsu.edu!msrtrans!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Lines: 1
Xref: number1.nntp.dca.giganews.com microsoft.public.word.vba.beginners:27171

Hi Joes,
- How can I specify which macro should be called during the "OnEntry"
and "OnExit" events. I saw it in the property window of the from field
but can this also be achieved programmatically rather than using this
dialog?
Yes, this is the basic pattern:
ActiveDocument.FormFields(1).EntryMacro
ActiveDocument.FormFields(1).ExitMacro

Note that the document must be in an unprotected state when you make
this assignment.
- Is there a way to pass parameters to my macro?
Not directly, no. But you can write the information to a Document
VARIABLE (that's an object in the Word object model), then pick it up
from the doc Variable. That's how I often work with form field macros.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
W

Word Heretic

G'day "joes" <[email protected]>,

My last such project required rich parms provided by GUI users, so
used a two comment approach. My code takes the range of content
starting from the end of the triggered field and continuing to the end
of the document, then extracts the contents of the first two comments
to use as its parms. Comment 1 holds the prompt text, and Comment 2
holds the parms space separated. So, the answer is "It depends on your
functional spec".

Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


joes reckoned:
 
J

joes

I found a way by myself eith the hints from you.. thanks
I see for my needs 2 ways either us "bookmarks" or a combination of
"docvars" as fields.
Here an example what i need: The field should execute a VB macro which
gets the data from a database. simplified code call:
getName("id_345")

The "bookmark" approach would allow to insert a dynamic "field" into
the word where I want but the "bookmark" itself does not allow to
define meta data. i.e. to define the query parameters for the
information you like to get. therefore you have to code the required
parameter directly as the name of the bookmark. Tha name looks like
this "GET_NAME_ID_345"
This is enough for simple things but as soon as you need multiple
parameters it would become a mess.

The "DOCVAR" and "field" approach is now my preferred way. The VB API
of word allows to iterate quickly over both and accessing the vlaues in
it i.e.
For i = 1 To ActiveDocument.fields.Count
For i = 1 To ActiveDocument.variables.Count
The simple tricky thing is only to understand that a "docvar" is also a
simple "field". If you iterate through the fields you will see that.So
this gives us the possiblity to
1. first scan the field codes for getting meta information and
parameters you do to interpret the fields
2. Execute your macro with parameters
3. insert the value by adressing the field as doc var

a little bit more practise
Insert some DOCVARS, toggle the field codes and add for each field some
particular options (meta data) i.e.

"{DOCVARIABLE varName \id 345 \cmd name}.

This is the full code as text of this field the "\id 345" "\cmd name"
are the parameters which you then later can pass to your macro. With
the following statement you can access the code of the field. with this
notation you aceessing the DOCVAR as a normal field.

ActiveDocument.fields.Item(i).Code.Text

Now you can parse the text i.e. by using regexp for retrieving the
parameter values. After that you can execute your macro.
Last step is to adreess the field now as a variable and assign the
value:

ActiveDocument.Variables(varName).value = value

so hope this helps all ...

regards
Mark Egloff
 
W

Word Heretic

G'day "joes said:
This is enough for simple things but as soon as you need multiple
parameters it would become a mess.

Correct.

However, instead of fields, I generally use

ActiveDocument.Variables
ActiveDocument.CustomDocumentProperties

but I can see your point.

Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


joes reckoned:
 

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