Adding a field to a bookmark

G

Graham Mayor

Set rText = ActiveDocument.Bookmarks("BookmarkName").Range
rText.Text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit"
With ActiveDocument
.Bookmarks.Add "BookMarkName", rText
.Fields.Update
End With

The above extract will write the text string into the Bookmark
"BookmarkName" however I wish to insert a field into the range rText rather
than plain text, which has so far eluded me. Can someone please offer the
way to do this.

Thanks

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
L

Lene Fredborg

I am not quite sure whether the following does what you want - but that is
how I understand your description:

Sub InsertFieldInBookmark()

Dim sText As String
Dim oField As Field
Dim oRange As Range

sText = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit"
Set oRange = Selection.Range

Set oField = oRange.Fields.Add(Range:=oRange, Type:=wdFieldMacroButton,
Text:=sText, PreserveFormatting:=False)

'Extend oRange to include the field
oRange.End = oField.Result.End

'Add bookmark around oField
With ActiveDocument
.Bookmarks.Add Name:="BookMarkName", Range:=oRange
.Fields.Update
End With

Set oRange = Nothing
Set oField = Nothing

End Sub

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
G

Greg Maxey

Something like this:

Sub OnExitCB1()
Dim rText As Range
Dim oFld As FormFields
Set oFld = ActiveDocument.FormFields
Set rText = ActiveDocument.Bookmarks("Check1Result").Range
If oFld("Check1").CheckBox.Value = True Then
rText.Fields.Add rText, wdFieldIncludeText, "C:\Doc1.doc"
rText.MoveEnd wdCharacter, 1
Else
rText.Fields.Add rText, wdFieldIncludeText, "C:\Doc2.doc"
rText.MoveEnd wdCharacter, 1
End If
With ActiveDocument
.Bookmarks.Add "Check1Result", rText
.Fields.Update
End With
End Sub
 
G

Graham Mayor

Thanks both - that should resolve my problem

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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