Passing params from MACROBUTTON

S

sprungli

Hello,

In a project I have a routine (thanks Chad DeMeyer) that finds a text and
then replaces it with a macrobutton displaying the same text. This happens
in a loop, as shown below.

Do While Selection.Find.Found
Selection.Fields.Add Range:=Selection.Range, _
Type:=wdFieldEmpty, _
Text:="MACROBUTTON MySub " +
Selection.Range, _
PreserveFormatting:=False
.Execute
Loop

Now, MySub (below) expects a string as a parameter and I want to pass the
value of Selection.Range (above) for each loop and then display this value
whenever the macrobutton is clicked.

Sub MySub(someTxt As String)
'do smt w/ the string param
MsgBox someTxt
End Sub

I do it with:
....
Text:="MACROBUTTON MySub(" + Selection.Range + ") " + Selection.Range
....

where the 1st Selection.Range is the parameter passed to MySub and the 2nd
one is the text displayed by the macrobutton.
As a result the macrobutton stops responding to clicks. Is there a
workaround this problem or I am trying to achieve something impossible?

Thank you in advance for any help.
 
J

Jonathan West

Hi springli.

The normal approach to doing this is to include a PRIVATE field nested
within the MACROBUTTON field like this

{ { PRIVATE your data goes here } MACROBUTTON MySub Your visible text here}

Then, when the user clicks on the macrobutton field and calls MySub, the
Selection.Fields(2).Code property contains the text of the nested PRIVATE
field.

To insert nested fields in this way, you need to modify your code as follows

Dim oField as Field
Dim oRange as Range
Do While Selection.Find.Found
Set oField = Selection.Fields.Add(Range:=Selection.Range, _
Type:=wdFieldEmpty, Text:="MACROBUTTON MySub " & Selection.Range, _
PreserveFormatting:=False)

Set oRange = ActiveDocument.Range(oField.Code.Start + 1, oField.Code.Start
+ 1)
ActiveDocument.Fields.Add Range:=oRange, Type:=wdFieldPrivate, _
Text:="My data goes here", PreserveFormatting:=False

.Execute
Loop
 
S

sprungli

Hello Jonathan and thank you for the suggestion!

It makes sense but it doesn't work. I tested it by putting Selection.Range
as the text property of the private field (this is my goal):

....
ActiveDocument.Fields.Add Range:=oRange, Type:=wdFieldPrivate, _
Text:=Selection.Range, PreserveFormatting:=False
....

Then, when clicking the macrobutton, the value of Selection.Fields(2).Code
is " PRIVATE ".

If a literal is used instead of Selection.Range in the text property of the
private field, as shown below,

....
ActiveDocument.Fields.Add Range:=oRange, Type:=wdFieldPrivate, _
Text:="Grrrr!", PreserveFormatting:=False
....

then the value of Selection.Fields(2).Code upon macrobutton click is "
PRIVATE Grrrr! ".

Is there a way to assign Selection.Range dynamically to the
Selection.Fields(2).Code?

Thank you very much for your help.

sprungli
 
J

Jonathan West

Put the value of Section.text into a string variable before you insert the
outer MacroButton field. Then assign the string variable to the TExt
property of the Private field when you create that.
 

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