Thanks a lot, this really cleared up a lot of my questions.
----- Cindy M -WordMVP- wrote: -----
Hi =?Utf-8?B?RmVuZw==?=,
I'm kind of familiar with inserting nested fields from VB. I have done this
with an includepicture field and a merge field. However in this case, I don't
even know the syntax of inserting a normal if field or a skipif field using VB.
I'm currently trying to automate this on Word 97.Right, I just wanted to know the extent of your background with Word/fields.
Is there somewhere that has the syntax of how to insert all the word fields and what each field does?
What each field does: look it up in Word's Help files
Inserting individual fields: the .Add method of the Fields collection. You can
use the .Type argument with a wdField-constant (you can get a list of these from
the Object Browser in Word's Help window by searching on wdField) OR you can
simply include the field's name in the TEXT argument.
Nesting fields is a trickier proposition, however, as Microsoft didn't give us
any way to do this directly using VBA. Usually, I use Word's Find functionality.
I insert the outermost field code first, and put some kind of place holder text
where the inserted field should go. I then use Find to get hold of the
placeholder and insert the nested field in its place.
Here's a sample:
Sub CreateFieldInField()
Dim fld As Word.Field
Dim szQuotes As String
szQuotes = Chr(34)
Set fld = ActiveDocument.Fields.Add(Range:=Selection.Range, _
Type:=wdFieldIf, Text:="bkm <> bkm " & szQuotes & _
"Please change the bkmAUTHORbkm setting in File/Properties" _
& szQuotes, PreserveFormatting:=False)
InsertFieldInFieldCode fld.Code, "bkm", "Author"
InsertFieldInFieldCode fld.Code, "bkm", "UserName"
InsertFieldInFieldCode fld.Code, "bkm", "Symbol 34"
InsertFieldInFieldCode fld.Code, "bkm", "Symbol 34"
fld.Update
End Sub
Function InsertFieldInFieldCode( _
ByRef rng As Word.Range, _
ByRef szBkm As String, _
ByRef szField As String, _
Optional ByRef PF As Boolean = False) As Boolean
InsertFieldInCode = False
With rng.Find
.Text = szBkm
.Execute
If .Found Then
ActiveDocument.Fields.Add _
Range:=rng, Text:=szField, _
PreserveFormatting:=PF
InsertFieldInCode = True
End If
End With
End Function
Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.mvps.org/word
This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail