0% found this document useful (0 votes)
51 views4 pages

Hey, Scripting Guy! How Can I Randomly Assign A Font To Characters in A Word Document - Hey, Scripting Guy! Blog - Site Home - TechNet Blogs

Hey, Scripting Guy! How Can I Randomly Assign a Font to Characters in a Word Document_ - Hey, Scripting Guy! Blog - Site Home - TechNet Blogs

Uploaded by

genshaox
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views4 pages

Hey, Scripting Guy! How Can I Randomly Assign A Font To Characters in A Word Document - Hey, Scripting Guy! Blog - Site Home - TechNet Blogs

Hey, Scripting Guy! How Can I Randomly Assign a Font to Characters in a Word Document_ - Hey, Scripting Guy! Blog - Site Home - TechNet Blogs

Uploaded by

genshaox
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

6/18/2015

Hey,ScriptingGuy!HowCanIRandomlyAssignaFonttoCharactersinaWordDocument?Hey,ScriptingGuy!BlogSiteHomeTechNetBlogs

All About
Windows Server

Cloud Platform
Blogs

Datacenter
Management

Client
Management

Virtualization,
VDI & Remote
Desktop

File & Storage &


High Availability

Windows Server
Management

Identity & Access

Hey, Scripting Guy! Blog


Learn about Windows PowerShell

Hey, Scripting Guy! How Can I Randomly Assign a Font to


Characters in a Word Document?
ScriptingGuy1

24 Apr 2008 2:14 AM

Hey, Scripting Guy! Ive come up with a way to imitate handwriting in a document, but I need some help. Because
real handwriting is characterized by variability, I had the idea of using four slightlydifferent fonts, taken from my
actual handwriting, and then randomly assigning a font to each letter. I think that would imitate handwriting a little better,
but Im not sure how to go about doing that.
CW

Hey, CW. We have to admit that were intrigued by your theory of using random font
assignment to introduce variability, and thus morerealistically imitate actual handwriting. The
Scripting Guys actually debated this issue a couple weeks ago, when we were faced with the daunting
task of signing hundreds and hundreds and hundreds! of Certificates of Excellence for the 2008
Winter Scripting Games. For a little while we toyed with the notion of having their signatures printed
onto the certificates, but we had the same concern you do: that these printed signatures using some
font designed to look like handwriting would still look phony.
Note. Were we also concerned about the ethics involved in promising everyone a certificate signed
by the Scripting Guys, and then not actually signing those certificates? Um, sure, sure we were .
In the end, we went ahead and signed all the certificates by hand, a process that required several
hours to finish. Admittedly, this might have gone a little faster, but at one point the Scripting Guy
who writes this column literally could not remember how to write his name. Thats what signing your
name several hundred times in succession will do to you. On top of that, this was also a process that
really deteriorated towards the end: if you have a last name beginning with the letter S, for example, then no doubt the
signatures you got on your certificate were unreadable smudges that looked as though they were signed by a monkey.
Of course, as it turns out, it would be much better for you if your certificate had been signed by a monkey. After all, in
June, 2005 three tempera paintings created by Congo the chimpanzee sold at auction for $26,352. Thats approximately
$26,352 than you can expect to get at auction for anything created by the Scripting Guys.
Note. Not to take anything away from Congo, mind you, but creating a series of abstract paintings doesnt seem
anywhere near as impressive as the exploits of Bobo, the Detective Chimp. Incidentally, that has nothing to do with
todays column; we just wanted to pay homage to Bobo, and to the immortal Lancelot Link, Secret Chimp.
As it turns out, Congo the chimpanzee who died many years ago only painted for three years, at which time he became
bored with art and no longer felt the need to put his feelings down on canvas. Fortunately for us, however, Congo next
turned his attention to system administration scripting; in fact, one of the last scripts Congo ever wrote was a script that
can randomly select a font for the individual characters in a Microsoft Word document. You know, a script just like this
one:
SetobjWord=CreateObject("Word.Application")
objWord.Visible=True
SetobjDoc=objWord.Documents.Open("C:\Scripts\Test.doc")
SetobjRandom=CreateObject("System.Random")
intLow=1
intHigh=5
SetobjRange=objDoc.Range()
SetcolCharacters=objRange.Characters
ForEachstrCharacterincolCharacters
intRandom=objRandom.Next_2(intLow,intHigh)

SelectCaseintRandom
Case1strCharacter.Font.Name="Arial"
Case2strCharacter.Font.Name="TimesNewRoman"

http://blogs.technet.com/b/heyscriptingguy/archive/2008/04/23/howcanirandomlyassignafonttocharactersinaworddocument.aspx

1/4

6/18/2015

Hey,ScriptingGuy!HowCanIRandomlyAssignaFonttoCharactersinaWordDocument?Hey,ScriptingGuy!BlogSiteHomeTechNetBlogs

Case3strCharacter.Font.Name="CourierNew"
Case4strCharacter.Font.Name="Forte"
EndSelect
Next
We should note that one of the drawbacks to reprinting scripts created by chimpanzees is that chimps typically dont
bother to explain how their scripts work. Which should finally put to rest the rumor that this column is written by a
monkey. This column is not written by a monkey; it only sounds like it was written by a monkey. At any rate, well see what
we can do to explain how this script does its thing. However, if that explanation turns out to be unclear or misleading, well,
dont blame us; blame Congo the chimp.
Thats what we always do, especially now that Scripting Guy Peter Costantini is no longer on the team.
As you can see, Congo starts his script off by creating an instance of the Word.Application object, then sets the Visible
property to True; that gives him and us a running instance of Microsoft Word that we can see on screen. Once we have
that instance we can then use the Open method to open the document C:\Scripts\Test.doc:
SetobjDoc=objWord.Documents.Open("C:\Scripts\Test.doc")
Thats a document that looks something like this:

Because were going to randomly assign fonts to each character we need two things: we need a method for generating
random numbers, and we need a way to access each of the characters in the document. We can generate random
numbers between 1 and 4, inclusive, by using the .NET Framework class System.Random, and by assigning our low
number 1 to a variable named intLow, and our high number, plus 1, to a variable named intHigh. Thats what we do in
this block of code:
SetobjRandom=CreateObject("System.Random")
intLow=1
intHigh=5
As for accessing each and every character in the document, well, these two lines of code give us the ability to do that:
SetobjRange=objDoc.Range()
SetcolCharacters=objRange.Characters
In line 1 were creating an instance of Words Range object, an object that in lieu of any other instructions
encompasses the entire document. In line 2, were creating an object reference to the range objects Characters property;
as the name implies, the Characters property contains a collection of all the characters found in the range.
So what comes next? Well, after a quick break to eat a banana or two, we proceed to set up a For Each loop to walk us
through each character in the collection colCharacters:
ForEachstrCharacterincolCharacters
Inside that loop, the first thing we do is use the Next_2 method to generate a random number between 1 and 4, inclusive:
intRandom=objRandom.Next_2(intLow,intHigh)
Note. Did Congo come up with the name Next_2 for this method? Hey, come on: thats an insult to chimpanzees
everywhere!
As soon as we have a number between 1 and 4 we can then use the following Select Case statement to change the font
for that particular character:
SelectCaseintRandom
Case1strCharacter.Font.Name="Arial"
Case2strCharacter.Font.Name="TimesNewRoman"
Case3strCharacter.Font.Name="CourierNew"
Case4strCharacter.Font.Name="Forte"
EndSelect

http://blogs.technet.com/b/heyscriptingguy/archive/2008/04/23/howcanirandomlyassignafonttocharactersinaworddocument.aspx

2/4

6/18/2015

Hey,ScriptingGuy!HowCanIRandomlyAssignaFonttoCharactersinaWordDocument?Hey,ScriptingGuy!BlogSiteHomeTechNetBlogs

And then from there we then go back to the top of the loop, where we generate another random number and change the
font for the next character in the collection. When all is said and done well end up with a document that looks something
like this:

Granted, this particular example, using these fonts, might not look much like handwriting. But you get the idea.
Considering the fact that he created over 300 paintings in his brief, threeyear career, it probably comes as no surprise that
the prolific Congo also created a second script that can change the font of various letters throughout a Word document.
Congos second script takes a different approach from his first: this script takes a set of letters in this case, a, d, l, and s,
methodically searches the document for each of those characters, and then changes the font of each a, d, l, or s it
encounters. Were not sure if that does you any good, but we have no doubt that Congo would have wanted us to give
you the option of using this alternate approach instead. Heres the code for Congos second script:
ConstwdReplaceAll=2
SetobjWord=CreateObject("Word.Application")
objWord.Visible=True
SetobjDoc=objWord.Documents.Open("C:\Scripts\Test.doc")
SetobjSelection=objWord.Selection
arrLetters=Array("a","d","l","s")
ForEachstrLetterinarrLetters
objSelection.Find.Text=strLetter
objSelection.Find.Forward=TRUE
objSelection.Find.MatchWholeWord=False
objSelection.Find.Replacement.Text=strLetter
objSelection.Find.Replacement.Font.Name="Forte"
objSelection.Find.Execute,,,,,,,,,,wdReplaceAll
Next
Thats all we have time for today, CW; as it its, its almost time for the cleaning crew to come clean out our cages. In the
meantime, some of you might be concerned about whether your Certificate of Excellence was really signed by the
Scripting Guys. If youre worried about that, take a careful look at the signatures on your certificate. Scripting Guy Greg
Stemps largelyillegible signature should look similar to this:

Meanwhile, Scripting Guy Jean Ross signature should look like this:

And yes, we noticed that, too. But her handwriting was so nice we hated to say anything about her spelling. Maybe next
year.

http://blogs.technet.com/b/heyscriptingguy/archive/2008/04/23/howcanirandomlyassignafonttocharactersinaworddocument.aspx

3/4

6/18/2015

Hey,ScriptingGuy!HowCanIRandomlyAssignaFonttoCharactersinaWordDocument?Hey,ScriptingGuy!BlogSiteHomeTechNetBlogs

Tweet

Like

Share

Save this on Delicious

Comments
2 Dec 2011 12:06 AM
Journey

I am new to all of this. What kind of script is this and how do go about creating this script for my own use?
26 Jun 2014 5:55 PM
chris

how can i get this to work with word 2007 It would really help my direct mail business. Thanks.

hassan sayed issa20014

6 Aug 2014 2:59 PM

thanks

http://blogs.technet.com/b/heyscriptingguy/archive/2008/04/23/howcanirandomlyassignafonttocharactersinaworddocument.aspx

4/4

You might also like