Insert field and bookmark

F

frogman

Scope:
Have user select some text and click macro button the first word
becomes a macro button and a bookmark is created for reference.

The problem is if the first word has some type of hyphen or comma after
it my code creates a field with a space tagged at the end. I want the
field to be just the word.

Sub TextToFieldPara()
Dim strBookMarkName As String
Dim strFirstWordInSentence As String
Dim intStringLength As Integer

strBookMarkName = "P_" & ActiveDocument.Range(0,
Selection.Paragraphs(1).Range.End).Paragraphs.count &
ActiveDocument.Range(0, Selection.Paragraphs(1).Range.End).Words.count
ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:=strBookMarkName
strFirstWordInSentence = Selection.Words(1)
intStringLength = Len(strFirstWordInSentence)

If Right(strFirstWordInSentence, 1) = " " Then
strFirstWordInSentence = Left(strFirstWordInSentence,
intStringLength - 1)
End If

Selection.Fields.Add Range:=Selection.Words(1), Type:=wdFieldEmpty,
Text:="GOTOBUTTON " & strBookMarkName & " " & strFirstWordInSentence &
"", PreserveFormatting:=False
Selection.GoTo What:=wdGoToBookmark, Name:=strBookMarkName
End Sub
 
J

Jean-Guy Marcil

frogman was telling us:
frogman nous racontait que :
Scope:
Have user select some text and click macro button the first word
becomes a macro button and a bookmark is created for reference.

The problem is if the first word has some type of hyphen or comma
after it my code creates a field with a space tagged at the end. I
want the field to be just the word.

Sub TextToFieldPara()
Dim strBookMarkName As String
Dim strFirstWordInSentence As String
Dim intStringLength As Integer

strBookMarkName = "P_" & ActiveDocument.Range(0,
Selection.Paragraphs(1).Range.End).Paragraphs.count &
ActiveDocument.Range(0, Selection.Paragraphs(1).Range.End).Words.count
ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:=strBookMarkName
strFirstWordInSentence = Selection.Words(1)
intStringLength = Len(strFirstWordInSentence)

If Right(strFirstWordInSentence, 1) = " " Then
strFirstWordInSentence = Left(strFirstWordInSentence,
intStringLength - 1)
End If

Selection.Fields.Add Range:=Selection.Words(1), Type:=wdFieldEmpty,
Text:="GOTOBUTTON " & strBookMarkName & " " & strFirstWordInSentence
& "", PreserveFormatting:=False
Selection.GoTo What:=wdGoToBookmark, Name:=strBookMarkName
End Sub

I understand that when you use
Text:="GOTOBUTTON " & strBookMarkName & " " & strFirstWordInSentence
& "",

you are trying to tell Word not to add a space after the last word making up
the field code. But, Word adds a space regardless, not after the last word,
but before the closing "}"!

So, what your code was doing was to delete the space that followed the word
and you did not really notice because word was adding a space at the end of
your field anyway (by the way, when working with fields, it really pays to
check the option to always highlight fields, then you see what is going on:
Tools > Options > View tab > Field Shading > Always).

So, knowing that Word will always add a space at the end of the field, just
delete it at the end. Try this modified version of your code:

'_______________________________________
Sub TextToFieldPara()

Dim strBookMarkName As String
Dim rgeFirstWordInSentence As Range
Dim rgeField As Range
Dim strFirstWordInSentence As String

strBookMarkName = "P_" & ActiveDocument.Range(0, _
Selection.Paragraphs(1).Range.End).Paragraphs.Count _
& ActiveDocument.Range(0, Selection.Paragraphs(1).Range.End).Words.Count

ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:=strBookMarkName
Set rgeFirstWordInSentence = Selection.Words(1)

If Right(rgeFirstWordInSentence.Text, 1) = " " Then
rgeFirstWordInSentence.MoveEnd wdCharacter, -1
End If

strFirstWordInSentence = rgeFirstWordInSentence.Text

Selection.Fields.Add Range:=rgeFirstWordInSentence, _
Type:=wdFieldEmpty, Text:="GOTOBUTTON " _
& strBookMarkName & " " & strFirstWordInSentence, _
PreserveFormatting:=False

Selection.GoTo What:=wdGoToBookmark, Name:=strBookMarkName

Set rgeField = Selection.Fields(1).Code
rgeField.Text = Left(rgeField, Len(rgeField.Text) - 1)
Selection.Fields(1).Update

End Sub
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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