Re: Count of specific string in Word (was re: Count of Tab characters)
Re: Count of specific string in Word (was re: Count of Tab characters)
- Subject: Re: Count of specific string in Word (was re: Count of Tab characters)
- From: Bill Planey <email@hidden>
- Date: Tue, 13 Feb 2001 18:52:29 -0600
Actually, I solved the problem, and rather elegantly, I think. And
this little routine can be used for any string; here I used it for
counting tabs.
Basically, you count the words in the document, use visual basic
to search for the desired character or string, and Replace With
the original search string (I use the Find What symbol "^&") and
a short placeholder string flanked on each side by a single white
space. I use a handler to call the replace functions of Visual Basic
(otherwise the script code would be many miles long). "noCReplace"
is a handler from the script object "TextSubObject", below. I left
the display dialog lines active here, but in use, I comment them
out because it needs to run unattended. The handler makes calls to
Word and the front document, that's why it doesn't "tell Microsoft
Word" on those steps.
set TextSubObject to load script file (TextSubFile)
tell application "Microsoft Word"
set WordCount1 to count each word of front document
display dialog "There are " & WordCount1 & " words in this document."
end tell
tell TextSubObject
noCReplace("^t", "^& fff ") -- adds a single string " fff "
-- for each tab there is in the document
end tell
tell application "Microsoft Word"
set WordCount2 to count each word of front document
display dialog "There are now " & WordCount2 & " words in this
document."
end tell
set TabCount to (WordCount2 - WordCount1) -- determines how many times
-- the action will take place
tell TextSubObject
noCReplace("^t fff ", "^t") -- removes " fff " strings
display dialog "There are " & TabCount & " tabs in this document."
end tell
Thanks for the feedback Jed and all others who commented on my
previous question!
Bill Planey
++++++++++++++++++++++++++++++++
>
Hi Bill,
>
>
This should delete any paragraph with zwx in it (though I haven't tried it
>
with exceptionally large documents):
>
>
---begin script
>
property badString : "zwx" --this is the string to look for
>
>
tell application "Microsoft Word"
>
set docBody to (get paragraphs of text of document 1) --working with
>
first open document
>
set newBody to my RidBadPars(docBody)
>
set text of document 1 to newBody
>
end tell
>
>
to RidBadPars(docBody)
>
set newDocBod to {}
>
set parCount to count items of docBody
>
repeat with i from 1 to parCount
>
if not (item i of docBody contains badString) then
>
set end of newDocBod to item i of docBody
>
end if
>
end repeat
>
set oldDelims to AppleScript's text item delimiters
>
set AppleScript's text item delimiters to return
>
set newDocBod to newDocBod as text
>
set AppleScript's text item delimiters to oldDelims
>
newDocBod
>
end RidBadPars
>
---end script
>
>
HTH,
>
Jed
>
>
On 2/12/01 8:20 PM, Bill Planey uttered these immortal words:
>
>
> I desperately need a way to count the number of occurrences of a
>
> specified string in MS Word. I have a script which puts a placeholder
>
> in certain paragraphs that have met my criteria. This placeholder is
>
> "zwx".
>
>
>
> I want the script to either:
>
>
>
> delete any paragraph which is found to contain the string by
>
> using a broad statement to do so, something like:
>
>
>
> tell MS Word
>
> for any paragraph which contains string "zwx"
>
> delete entire paragraph
>
> end tell
>
>
>
> or:
>
>
>
> tell MS Word
>
> set count of paragraphs which contain string "zwx" to ParaCount
>
> repeat ParaCount times
>
> find string "zwx"
>
> extend selection to entire paragraph
>
> delete selection
>
> end repeat
>
> end tell
>
>
>
> Now, I know my "code" above is wrong, I just wanted to simply express
>
> the concept. Anyone out there with some experience with this kind of
>
> thing?
>
>
>
> Thanks!!!
>
>
>
> Bill Planey
>
> _______________________________________________
>
> applescript-users mailing list
>
> email@hidden
>
> http://www.lists.apple.com/mailman/listinfo/applescript-users
>
>
>
>
>
~)~)~)~)~)~)~)~)~)~)~)~)~)
>
Jed Verity
>
_______________________________________________
>
applescript-users mailing list
>
email@hidden
>
http://www.lists.apple.com/mailman/listinfo/applescript-users