Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Creating Nested Word Fields through VBA

105 views
Skip to first unread message

Chris Webb

unread,
Sep 28, 1999, 3:00:00 AM9/28/99
to
Hi there, I am trying to write a VBA macro which creates some fields within
my document. The problem is that these fields contain within them other
fields. For example I have an IF field, with the condition part being based
upon the contents of another field. The Value if True needs to print a
mergefield in the document.

{IF {DUPLICATE} = "on" "{MERGEFIELD Name}" "" }

Any ideas would be greatly appreciated, if you dont understand what i'm
trying to achieve, please contact me.

Many thanks in advance;

Chris...

ibby

unread,
Sep 29, 1999, 3:00:00 AM9/29/99
to
Hi Chris,

The following should create the nested field that you gave as an example.

------------------------------------------------------------

Public Sub NestedFields()

ActiveWindow.View.ShowFieldCodes = True

Set myField = ActiveDocument.Fields.Add _
(Range:=Selection.Range, Type:=wdFieldEmpty)

With ActiveDocument.Fields(1)

.Code.Text = ""
.Code.Select

Selection.InsertAfter "If "
Selection.Collapse direction:=wdCollapseEnd

Set myField1 = .Code.Fields.Add _
(Range:=Selection.Range, Type:=wdFieldEmpty)

Selection.InsertAfter " = " & Chr(34) & _
"on" & Chr(34) & " " & Chr(34)

Selection.Collapse direction:=wdCollapseEnd

Set myField2 = .Code.Fields.Add _
(Range:=Selection.Range, Type:=wdFieldEmpty)

Selection.InsertAfter Chr(34) & _
" " & Chr(34) & Chr(34)


With myField1
.Code.Text = ""
.Code.Select
Selection.InsertAfter "DUPLICATE"
End With

With myField2
.Code.Text = ""
.Code.Select
Selection.InsertAfter "MERGEFIELD Name"
End With

End With

'ActiveWindow.View.ShowFieldCodes = False

End Sub

----------------------------------------------------------

I don't work much with fields, so I haven't tested whether the field created
returns a valid result. Let us know. Please post any replies to the
newsgroup.

Hope it helps.

Chris Webb <chris...@zoom.co.uk> wrote in message
news:7sqkn5$uh$1...@taliesin2.netcom.net.uk...

0 new messages