How Can I Change the Font Name and Size for an Entire Word Document?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I change the font name and size for an entire Word document?

— CD

SpacerHey, Scripting Guy! AnswerScript Center

Hey, CD. You know, this question reminded us of how old we Scripting Guys are getting – well, all of us except for Jean, of course, who continues to get younger and more beautiful every day.

Note. No, that isn’t really true. However, because Jean’s the editor she’ll just edit this column in a way that makes it sound like she’s getting younger and more beautiful every day. With that in mind we decided to save her the trouble of having to rewrite the preceding paragraph. Don’t worry about rewriting everything to make yourself sound good, Jean: it’s already taken care of! (Editor’s Note: Thank you. And yes, the preceding paragraph really is true. Greg’s just jealous – he wishes he could look younger and more beautiful every day.)

At any rate, at least some of the Scripting Guys are old enough to remember the days when you had no choice but to use the same font and font size (typically Courier 10-point) throughout a document; back in those dark days of computing you didn’t have TrueType, ClearType, OpenType, PostScript or anything even remotely similar. Way back when, Frutiger Linotype and TW Cen MT Condensed Extra Bold were just a dream. Sigh ….

In other words, back then you couldn’t change the font name and size for an entire Word document. In today’s Golden Age of computing, however, all you need to do is run a script like this:

Set objWord = CreateObject(“Word.Application”)
objWord.Visible = True
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection

objSelection.TypeText “This is the first paragraph.” objSelection.TypeParagraph() objSelection.TypeText “This is the second paragraph.” objSelection.TypeParagraph() objSelection.TypeText “This is the third paragraph.” objSelection.TypeParagraph()

Set objRange = objDoc.Range() objRange.Font.Name = “Arial” objRange.Font.Size = 10

Yes, this is a very simple little script and, believe it or not, it’s even simpler than it might first appear; after all, about half the code is there just to type some text into our document. We start off by creating an instance of the Word.Application object, then setting the Visible property to True; that gives us a running instance of Microsoft Word that we can actually see on screen. We use the Add method to create a new document, then use this line of code to create a Selection object at the very beginning of the document:

Set objSelection = objWord.Selection

We should point out that we don’t have to create the Selection object in order to change the font name and size; we do it here simply because we’re creating a new document on-the-fly. If we were opening an existing document we could skip this line of code; in that case, we could also skip this block of code, which just adds some text to the document:

objSelection.TypeText “This is the first paragraph.”
objSelection.TypeParagraph()
objSelection.TypeText “This is the second paragraph.”
objSelection.TypeParagraph()
objSelection.TypeText “This is the third paragraph.”
objSelection.TypeParagraph()

The fun really begins as soon as we have a Word document that includes some text (either because we created the document ourselves, as we did here, or because we opened an existing document). Probably the easiest way to change the font name and size for all the text in a document is to simply select the entire document and then make those changes to the selection. How hard is that going to be? Not hard at all; for example, selecting the entire document is as easy as calling the Range method:

Set objRange = objDoc.Range()

Note that we don’t pass any parameters to the Range method; doing so would actually limit our selection. (For example, we could tell the Range method to select only characters 1 through 100 in the document.) Calling Range without any parameters puts the entire document (and thus all of our text) into a Range object we called objRange. At that point we can then specify a new font name and font size using code like this:

objRange.Font.Name = “Arial”
objRange.Font.Size = 10

And there you have it; with any luck all the text in your document will now be 10-point Arial. Not bad for a bunch of old geezers, huh?

Oops; sorry, Jean. Not bad for three old geezers and one young and vibrant editor, huh?

0 comments

Discussion is closed.

Feedback usabilla icon