set variable to get back to the place I started in the document

F

frogman

The user selects a piece of text and clicks my macro button the code:

Sub TextToFieldClear()
Dim strSelection As String
Dim intStringLenth As Integer
Dim intWordCount As Integer
Dim myRng As Range
Set myRng = Selection.Range
ActiveWindow.View.ShowFieldCodes = True
strSelection = Selection.Text
***
intWordCount = ActiveDocument.Range(0,
Selection.Paragraphs(1).Range.End).Words.count

'takes the last space of the word
intStringLenth = Len(strSelection)
If Right(strSelection, 1) = " " Then
strSelection = Left(strSelection, intStringLenth - 1)
End If

'builds the double field code
With myRng
.Delete
.Collapse wdCollapseEnd
.Fields.Add Range:=myRng, Type:=wdFieldMacroButton,
PreserveFormatting:=False
.Move unit:=wdWord, count:=2
.InsertAfter "NoMacro "
.Move unit:=wdWord, count:=1
.Fields.Add Range:=myRng, Type:=wdFieldQuote,
PreserveFormatting:=False
.Move unit:=wdWord, count:=2
.InsertAfter ("""" & strSelection & """ \* CharFormat")
End With

'finds and changes the Q to red and updates the fields
With Selection
.find.ClearFormatting
.find.Forward = False
.find.MatchCase = True
.find.Text = "Q"
.find.Execute
.Font.Color = wdColorRed
.Fields.Update

End With

ActiveWindow.View.ShowFieldCodes = False

THIS IS WHERE I NEED THE HELP
I WANT TO FIND THE FIELD I JUST CREATED AND PUT A SPACE AFTER IT.
I DON'T WANT THE SPACE TO BE IN THE FIELD SO I WOULD LIKE TO KNOW IF
THERE IS A WAY TO SET A VARIABLE (***) RANGE OR COUNT TO FIND AT THIS
POINT TO ADD THE SPACE.

'add space after the new field
Selection.HomeKey unit:=wdStory
ActiveDocument.Words(intWordCount).Select
ActiveDocument.Range.Move unit:=wdCharacter, count:=1
Selection.TypeText Text:=" "

End Sub
 
G

Greg

Why not just add it when you build the nested field code:

..InsertAfter ("""" & strSelection & """ \*CharFormat")
..Move unit:=wdWord, Count:=2
..InsertAfter " "
 

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