create macrobutton with addin from VBA?

D

Dar-Shyang Lee

Hi,

Can someone tell me how to attach a macrobutton field with Addin
subfield to a range of text from VBA inside Word? I tried several
variations of the following code. However, the Addin either preceeds
the macrobutton field, or completely replaces it. Thanks very much.

Dim wd as Range
Dim fd as Field
Dim dd as Field
set wd = Selection.Words(1)
Set fd = ActiveDocument.Fields.Add(wd, wdFieldMacroButton, "MySub " &
wd.text)
Set dd = ActiveDocument.Fields.Add(wd, wdFieldAddin)
dd.data = "my arguments"

Dar-Shyang Lee
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Dar-Shyang Lee > écrivait :
In this message, < Dar-Shyang Lee > wrote:

|| Hi,
||
|| Can someone tell me how to attach a macrobutton field with Addin
|| subfield to a range of text from VBA inside Word? I tried several
|| variations of the following code. However, the Addin either preceeds
|| the macrobutton field, or completely replaces it. Thanks very much.
||
|| Dim wd as Range
|| Dim fd as Field
|| Dim dd as Field
|| set wd = Selection.Words(1)
|| Set fd = ActiveDocument.Fields.Add(wd, wdFieldMacroButton, "MySub " &
|| wd.text)
|| Set dd = ActiveDocument.Fields.Add(wd, wdFieldAddin)
|| dd.data = "my arguments"
||

What you need is called "nested fields".
You cannot do that with the range object.
This is one of the few cases where you must use the Selection object.

See
http://word.mvps.org/FAQs/MacrosVBA/NestedFieldsWithVBA.htm
and in case you need more background on what you are doing:
http://word.mvps.org/FAQs/TblsFldsFms/UsingMacroButton.htm
http://word.mvps.org/FAQs/TblsFldsFms/AddinFields.htm

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

Dar-Shyang Lee

Bonjour, Jean-Guy. Thanks very much for the clarification. However,
since my macro runs in the background while the user is typing inside
Word, I can not use Selection. Besides using nested fields like Addin
and Private, is there any other way to create a macrobutton field for
a range object, and pass in arguments created on the fly to the macro
associated with the field? Thanks.

Dar-Shyang
 
J

Jonathan West

Dar-Shyang Lee said:
Bonjour, Jean-Guy. Thanks very much for the clarification. However,
since my macro runs in the background while the user is typing inside
Word, I can not use Selection. Besides using nested fields like Addin
and Private, is there any other way to create a macrobutton field for
a range object, and pass in arguments created on the fly to the macro
associated with the field? Thanks.

You could do this with nested fields. What you need to do is create an
AutoText entry with a Macrobutton field that has a Private field nested
within it.

Insert the AutoText. If you need to modify either the text associated with
the Macrobutton or the data contained in the private field, then you can
modify the Code property of either field.
 

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