Re: MS Word 2004 basic questions
Re: MS Word 2004 basic questions
- Subject: Re: MS Word 2004 basic questions
- From: kai <email@hidden>
- Date: Wed, 7 Feb 2007 14:01:08 +0000
On 2 Feb 2007, at 22:49, email@hidden wrote:
*** read/find pointer
tell application "Microsoft Word"
-- Create a text range out of the entire document
set wholeStory to (create range active document start 0 end (count of
characters in active document))
-- Now search for a string
set selFind to find object of selection
set content of selFind to <string of interest>
execute find selFind
end tell
If I run this script, it finds and selects the first occurrence of
<string of interest>. If I then run it again, it finds the *second*
occurrence of <string of interest>, the next time it finds the third
occurrence.
Clearly, there is a read pointer that is not getting reset so the find
starts from where the last one stopped. How do I reset this pointer?
I tried setting the selection to the first word in the document and
then
collapsing the selection to the start, but that doesn't seem to do it.
Try something like this, Larry:
----------------
tell application "Microsoft Word"
set selection's selection end to 0
set selFind to find object of selection
set selFind's content to "text to find"
execute find selFind
end tell
----------------
*** testing characters: spaces don't count
[snip]
Further, when I try to set a range that includes spaces, the spaces
seem
to get ignored. What I need to do is test a few characters following a
selection. So far, I'm doing it by creating a new range defined by
start (end of content of selection +1)
end (end of content of selection +2)
to select the two characters after the selection. But if I then
examine
character 1, it ignores the space between words. Is this because it is
treating it as AppleScript's text item delimiter? Do I need to
redefine
the text item delimiter to do this operation?
I'd say it has more to do with the way a text range's start and end
points are defined. For example, to define a couple of characters
from a document as text ranges, we might say:
----------------
tell application "Microsoft Word"
set char1 to (create range active document start 0 end 1)
set char2 to (create range active document start 1 end 2)
(* note that each start point is 1 less than the actual character
position *)
{char1's content, char2's content}
end tell
----------------
So, when defining the start point of a text range following the
current selection, there's actually no need to add 1 to the
selection's selection end:
----------------
tell application "Microsoft Word"
set eos to selection's selection end
set next_range to (create range active document start eos end (eos +
2))
next_range's content
end tell
----------------
Is there a way I could get the whole word that the selection is
part of?
Something like this should do it:
----------------
set search_text to text returned of (display dialog "Enter text to
find:" default answer "")
tell application "Microsoft Word"
set selection's selection end to 0
set selFind to find object of selection
set selFind's content to search_text
set text_found to execute find selFind
if not text_found then return beep (* signal failure *)
tell (create range active document start 0 end selection's selection
end)
set word_count to count words
set word_start to end of content of content of words 1 thru
(word_count - 1)
set word_end to end of content of content of words 1 thru word_count
set found_word to set range it start word_start end word_end
end tell
content of found_word
end tell
----------------
---
kai
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/mailman//archives/applescript-users
This email sent to email@hidden