Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / September 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
frogman - 08 Sep 2005 19:30 GMT
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
Greg - 08 Sep 2005 19:59 GMT
Why not just add it when you build the nested field code:

.InsertAfter ("""" & strSelection & """ \*CharFormat")
.Move unit:=wdWord, Count:=2
.InsertAfter " "
frogman - 08 Sep 2005 20:52 GMT
that was too easy
Thanks
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.