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 / February 2005

Tip: Looking for answers? Try searching our database.

Operations on text which includes field codes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tony Gravagno - 12 Feb 2005 04:12 GMT
Word 2003 SP1
If I have Selection.Start = x and Selection.End = y, the
Selection.Text doesn't properly operate on data if that Range includes
fieldcodes.

Please try the following:
In a new document put the letters "abcd"
Now after 'b', insert a Date fieldcode.
Now add and run this macro:

Private Declare Sub Sleep Lib "kernel32" _
    (ByVal dwMilliseconds As Long)
Private Sub try()
   Dim doc As Word.Document
   Dim pos As Integer
   Set doc = Application.ActiveDocument
   Debug.Print Len(doc.Range.Text)
   For pos = 0 To Len(doc.Range.Text)
       doc.Range(pos, pos + 1).Select
       Selection.Font.Color = wdColorRed
       Sleep 500
   Next pos
End Sub

Notice:
1) 'a' goes red on 0, 'b' on 1, the entire date goes red on '2'.
2) 'c' is never reached, even if the For loop is set to Len+2 or
doc.Characters.Count, etc.

Try toggling the field code to get this text:
    ab{ DATE   \* MERGEFORMAT }cd
Then re-run (change color to Blue to see the diff between runs).
Note that the selection goes through each character of the field code
but stops at the "}".  By adding more characters to the end of the
string ('cdexxxxxxxxxxxxxxxxxz') and setting the Range end from pos+1
to pos+20, somehow the routine above eventually runs through the whole
string.

My application: I have a Selection of text and I know that at the end
of the selection there should be some specific character, but when I
look at the end of the selection I may or may not see these characters
if the selection happens to include a field.  I don't want to code a
cludge around this - any ideas?

Thanks.
Tony
Klaus Linke - 12 Feb 2005 04:33 GMT
Hi Tony,

Most fields have both code and a result, and you have to add both.

a{ date }bcd?
a12.02.2005bcd?

Set doc = Application.ActiveDocument
Debug.Print Len(doc.Range.Text)
15
Debug.Print doc.Range.End - doc.Range.Start
24
Debug.Print doc.Fields(1).Code.Start, doc.Fields(1).Code.End
2             8
Debug.Print doc.Fields(1).Result.Start, , doc.Fields(1).Result.End
9            19

Regards,
Klaus

> Word 2003 SP1
> If I have Selection.Start = x and Selection.End = y, the
[quoted text clipped - 42 lines]
> Thanks.
> Tony
 
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.