Accessing a PRIVATE field within a MACROBUTTON

S

sprungli

Hello,



My project uses macrobuttons to display and process portions of text within
a document. Each MACROBUTTON contains a PRIVATE field, whose value (text) is
used by the macro called upon clicking the MACROBUTTON. For example, if



{ MACROBUTTON Some text to find } is the MACROBUTTON,



then the value of its code would be



"  PRIVATE Some text to find MACROBUTTON MacroBtnClicked Some text to find
"



where MacroBtnClicked is the name of the macro to call.



When the user clicks the MACROBUTTON, the MacroBtnClicked performs some
processing with the value of the PRIVATE field. At some point I need to find
the text contained within a MACROBUTTON, present the user with the
possibility to edit this text (this happens in a dialog), then save the
changes back to the MACROBUTTON. One possible solution is to find the
MACROBUTTON displaying the text in question, unlink it, then recreate it
with the changed text for both the MACROBUTTON itself and the inner PRIVATE
field, as well. I have been wondering though whether there was a more
elegant (and lasier) way of achieving this. I can change the text of the
MACROBUTTON with something like:



Dim txtToFind As String



txtToFind = "Some text to find"



'switch codes On

ActiveDocument.Fields.ToggleShowCodes



With Selection.Find

.ClearFormatting

.Forward = True

.Wrap = wdFindStop Or wdFindContinue

.Text = txtToFind

.Execute



The line above selects the searched text; it can be easily replaced and,
with Codes Off, the MACROBUTTON displays the new text. The problem is the
PRIVATE field: I can't access it, to change its text too. See the code
below. The value of aField is Empty and the Selection.Fields collection is
of length 0.



'switch codes Off

ActiveDocument.Fields.ToggleShowCodes



privateFieldTxt = "  PRIVATE " + txtToFind



Do While Selection.Find.Found

For Each aField In Selection.Fields

If StrComp(aField.Code.Text,
privateFieldTxt, 1) = 0 Then

'set the aField.Code.Text to
the new text here.

End If

Next aField

Loop

End With



Am I on the right track? I am almost sure this can be done; but how to get
to the PRIVATE field?



Thanks in advance for any possible solutions.
 
H

Howard Kaikow

You don't want to unlink the fields.

Just parse the content of the Macrobutton field to find the Private field
and replace the content of the Private field, not the field itself.
 
S

sprungli

Thanks Howard,

I had already tried this, but it doesn't work. If you just replace it that
way, the MACROBUTTON diappears from the document. The code is replaced by
its value, although only some text of the field is changed, not the field
itself.

Is there another way of getting to the PRIVATE field inside a MACROBUTTON?
 
J

Jonathan West

Hi sprungli

In the macro that runs from the macro button, you can guarantee that the
MACROBUTTON field is selected. because thw PRIVATE field is nested, you can
therefore also guarantee that the PRIVATe field is the second field of the
selection, so the following line of code would retriebe the code of that
field.

strPrivateCode = Trim$(Selection.Fields(2).Code)

Then you can do whatever you want with that text.
 
S

sprungli

Hi Jonathan,

and thank you again. Yes I'd tried this, too, but Selection.Fields
collection is
of length 0 and Selection.Fields(2).Code returns <The requested member of
the collection does not exist.> Here is the code:

'select the txt of the macrobutton
ActiveDocument.Fields.ToggleShowCodes

With Selection.Find
.ClearFormatting
.Forward = True
.Wrap = wdFindStop Or wdFindContinue
.Text = "Some text to find"
.Execute

If Selection.Find.Found Then
'try to access Selection.Fields(1 or 2).Code here
End If
End With
 
H

Howard Kaikow

You have work within the fields.
One approach is to make sure field codes are displayed and hidden text is
displayed.
Then parse the Macrobutton field to find the Private field and change the
content of the Private field.
 
H

Howard Kaikow

There could be other fields within the Macrobutton field.
Private will not always be 2nd.
 
S

sprungli

Impossible, I know they are exactly two, since I create them. It is easy to
see in the debugger, or with a message box. The macrobutton always comes
first, then the private field inside it.

Thanks for your guess anyway.

sprungli
 
J

Jonathan West

sprungli said:
Hi Jonathan,

and thank you again. Yes I'd tried this, too, but Selection.Fields
collection is
of length 0 and Selection.Fields(2).Code returns <The requested member of
the collection does not exist.> Here is the code:

'select the txt of the macrobutton
ActiveDocument.Fields.ToggleShowCodes

With Selection.Find
.ClearFormatting
.Forward = True
.Wrap = wdFindStop Or wdFindContinue
.Text = "Some text to find"
.Execute

If Selection.Find.Found Then
'try to access Selection.Fields(1 or 2).Code here
End If
End With


The following works for me.

I created a MACROBUTTON with an embedded private field, which looks like
this when field codes are displayed

{ { PRIVATE this is some text } MACROBUTTON GetPrivateCode Click here to
reveal the private code}

The matching macro was as follows

Sub GetPrivateCode()
Dim strPrivate As String
strPrivate = Trim$(Selection.Fields(2).Code.Text)
MsgBox strPrivate
End Sub

Double-clicking on the Macrobutton field causes a message box to display
containing the code of the Private 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