Re: Making text plain or bold in Word
Re: Making text plain or bold in Word
- Subject: Re: Making text plain or bold in Word
- From: Paul Berkowitz <email@hidden>
- Date: Fri, 11 Apr 2003 23:31:27 -0700
On 4/11/03 10:37 PM, "David Wignall" <email@hidden> wrote:
>
on 4/12/03 4:56 AM, Frank W. Walker at email@hidden wrote:
>
>
> This is probably simple, but I am pulling my hair out over it.
>
>
>
> All I want to do is change all text in an opened Word document to "plain",
>
> then change the first two words of text to "bold".
>
>
Which on 4/12/03 7:32 AM, prompted Paul Berkowitz at email@hidden to
>
reply, regarding another reply from "Oz.Springs" <email@hidden>:
>
>
> You should not be including the Sub/End Sub when using in 'do Visual Basic'.
>
> And just as in regular AppleScript, you should not use Selection when it's
>
> not necessary - it's clunky and slower. 'Words' in VB can consist just of
>
> tabs or other white space, and should be excluded.
>
>
Why do you feel the need to excluded spaces in this case Paul? The 'Word'
>
object is the equivalent of what you get by double clicking on a word in the
>
interface and AFAIK a space on its own will never be considered as a word.
It was treating tabs as words but converting tabs to multiple spaces in
Immediate Window when I tested. I thought I'd better play it safe just in
case it also considered multiple spaces on their own to be words. case
unproven either way so far.
>
>
> It's possible that Normal style will not be plain text although it is,
>
> virtually always true enough, with the user's default font (usually Times)
>
> which is plain enough, I'm sure. Then you can just set the style to Normal
>
> and
>
> - most important - Reset the Font to remove manually applied formatting.
>
>
People do terrible things with Normal and it should not be relied on. In any
>
case, I took the original question to mean that they wanted to remove all
>
typeface styles but not perhaps change the typeface itself. So, I now offer
>
>
tell application "Microsoft Word"
>
do Visual Basic "
>
Dim rngWord As Range
>
Dim i As Integer
>
With ActiveDocument
>
With .Range
I missed .Range first time around because - in the annoying way of VBA,
Document does not have a Range property so it wasn't in the list of
properties. However, it does have a Range _Method_ which I discovered about
5 minutes after I wrote to the list. I've already changed the code to get
ActiveDocument.Range rather than loop through the StoryRanges. Still, it was
interesting learning about StoryRanges...
>
With .Font
>
.Bold = False
>
.Italic = False
>
.Underline = wdUnderlineNone ' and so forth
Not nearly enough. There are about 20 properties of Font property that have
to be set to False or equivalent, including several properties of .Shading,
setting a size, etc. etc. etc. I was trying to find a shorter way. There is
actually a foolproof way: Make a new style:
Dim oSty As Style
Set oSty = ActiveDocument.Styles.Add(Name:="aaaTest",
Type:=wdStyleTypeParagraph)
oSty.BaseStyle = wdStylePlainText ' or ""
With ActiveDocument.Range
.Style = "aaaTest"
.Font.Reset
End With
>
End With
>
End With
>
For Each rngWord In .Words
>
With rngWord
>
If .Text <> vbTab Then
>
If .Text <> vbNewLine Then
As long as you're certain that those are the only type of white space to be
considered words, sure. (Have you tried option-spaces?)
>
rngWord.Font.Bold = True
>
i = i + 1
>
End If
>
End If
>
End With
>
If i = 2 Then Exit For
>
Next rngWord
>
End With
>
"
>
end tell
>
>
although I like Shane Stanley's solution more :)
If it never crashes, sure.
>
>
> I haven't been able to discover any generic "white space" constant in VBA,
>
> so I check to make sure that a 'word' has at least one character with an
>
> ASCII number higher than 32 (i.. a visible character). When I first tested
>
> this without that check it found all the tabs in my first line as "words" so
>
> the real words never got bolded.
>
>
Nothing for a space but you can use vbTab for the tabs and vbNewline for the
>
paragraph marker. You need to watch out for the cell mark when tables are
>
involved as well.
Right,
>
>
> This script is super fast, with no clunky selecting going on:
>
>
I would like to offer a couple of points here, if you don't mind. Um,
>
otherwise stop reading, I guess :)
>
>
> tell application "Microsoft Word"
>
>
>
> do Visual Basic "
>
> Dim myStory As Variant
>
> Dim myWord As Variant, char As Variant
>
> Dim i As Integer, c As Integer, w As Integer
>
>
>
> For Each myStory In ActiveDocument.StoryRanges
>
> myStory.Style = wdStyleNormal
>
> myStory.Font.Reset
>
> Next myStory
>
>
StoryRanges is a collection so you wouldn't ordinarily use the For Each
>
construction but rather 'For i = 1 to 11.' to enumerate each member. This is
>
why the VBE has insisted you dim myStory as a variant
It worked, though, unless I was just lucky (since in fact there was only one
story in the document - mainStory). Yes, I'd noticed there's no StoryRange
object.
>
>
> i = 0
>
> w = 0
>
>
Did you need to do this in testing? Typically i and w would be set to zero
>
when they were declared.
Playing safe again.
>
>
> c = ActiveDocument.Words.Count
>
>
Maybe Long for c's data type, rather than an integer, as it doesn't take
>
much to get past 37700-odd words.
They'd have to be 37700 tabs and newlines then, since otherwise it's going
to exit the loop as soon as it hits the second real word. I though Integer
was pretty safe under the circumstances. Otherwise, under other
circumstances, sure.
>
>
> For Each myWord In ActiveDocument.Words
>
>
Technically, 'myWord' should have been declared as a Range or an Object,
>
rather than a Variant. The same applies to 'char'
I missed that: VBA allows only variants and object variables as the loop
variable in a For Each loop. I had already hit a compilation error declaring
myWord as a Word (since there's no such thing as a Word object - the
compiler thought I meant the application 'Word'!). Yes, Range or Object is
correct, thanks.
--
Paul Berkowitz
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.