{"id":55713,"date":"2008-04-24T01:14:00","date_gmt":"2008-04-24T01:14:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/04\/24\/hey-scripting-guy-how-can-i-randomly-assign-a-font-to-characters-in-a-word-document\/"},"modified":"2008-04-24T01:14:00","modified_gmt":"2008-04-24T01:14:00","slug":"hey-scripting-guy-how-can-i-randomly-assign-a-font-to-characters-in-a-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-randomly-assign-a-font-to-characters-in-a-word-document\/","title":{"rendered":"Hey, Scripting Guy! How Can I Randomly Assign a Font to Characters in a Word Document?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! I\u2019ve 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 slightly-different 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 I\u2019m not sure how to go about doing that.<BR>&#8212; CW<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, CW. We have to admit that we\u2019re intrigued by your theory of using random font assignment to introduce variability, and thus more-realistically 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 <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games08.mspx\"><B>2008 Winter Scripting Games<\/B><\/A>. 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. <\/P>\n<TABLE class=\"dataTable\" id=\"EBD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. 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 \u2026.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>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. That\u2019s 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 <I>S<\/I>, for example, then no doubt the \u201csignatures\u201d you got on your certificate were unreadable smudges that looked as though they were signed by a monkey. <\/P>\n<P>Of course, as it turns out, it would be much better for you if your certificate <I>had<\/I> been signed by a monkey. After all, in June, 2005 three tempera paintings created by <A href=\"http:\/\/www.cbsnews.com\/stories\/2005\/06\/20\/entertainment\/main703057.shtml\" target=\"_blank\"><B>Congo the chimpanzee<\/B><\/A> sold at auction for $26,352. That\u2019s approximately $26,352 than you can expect to get at auction for anything created by the Scripting Guys.<\/P>\n<TABLE class=\"dataTable\" id=\"E3D\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Not to take anything away from Congo, mind you, but creating a series of abstract paintings doesn\u2019t seem anywhere near as impressive as the exploits of <A href=\"http:\/\/www.thrillingdetective.com\/eyes\/bobo.html\" target=\"_blank\"><B>Bobo, the Detective Chimp<\/B><\/A>. Incidentally, that has nothing to do with today\u2019s column; we just wanted to pay homage to Bobo, and to the immortal <A href=\"http:\/\/en.wikipedia.org\/wiki\/Lancelot_Link%2C_Secret_Chimp\" target=\"_blank\"><B>Lancelot Link, Secret Chimp<\/B><\/A>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>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:<\/P><PRE class=\"codeSample\">Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True<\/p>\n<p>Set objDoc = objWord.Documents.Open(&#8220;C:\\Scripts\\Test.doc&#8221;)<\/p>\n<p>Set objRandom = CreateObject(&#8220;System.Random&#8221;)<\/p>\n<p>intLow = 1\nintHigh = 5<\/p>\n<p>Set objRange = objDoc.Range()\nSet colCharacters = objRange.Characters<\/p>\n<p>For Each strCharacter in colCharacters\n    intRandom = objRandom.Next_2(intLow,intHigh)<\/p>\n<p>    Select Case intRandom\n        Case 1 strCharacter.Font.Name = &#8220;Arial&#8221;\n        Case 2 strCharacter.Font.Name = &#8220;Times New Roman&#8221;\n        Case 3 strCharacter.Font.Name = &#8220;Courier New&#8221;\n        Case 4 strCharacter.Font.Name = &#8220;Forte&#8221;\n    End Select\nNext\n<\/PRE>\n<P>We should note that one of the drawbacks to reprinting scripts created by chimpanzees is that chimps typically don\u2019t 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 <I>not<\/I> written by a monkey; it only <I>sounds<\/I> like it was written by a monkey.) At any rate, we\u2019ll 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, don\u2019t blame us; blame Congo the chimp.<\/P>\n<P>That\u2019s what we always do, especially now that Scripting Guy Peter Costantini is no longer on the team. <\/P>\n<P>As you can see, Congo starts his script off by creating an instance of the <B>Word.Application<\/B> object, then sets the <B>Visible<\/B> property to True; that gives him \u2013 and us \u2013 a running instance of Microsoft Word that we can see on screen. Once we have that instance we can then use the <B>Open<\/B> method to open the document C:\\Scripts\\Test.doc:<\/P><PRE class=\"codeSample\">Set objDoc = objWord.Documents.Open(&#8220;C:\\Scripts\\Test.doc&#8221;)\n<\/PRE>\n<P>That\u2019s a document that looks something like this:<\/P><IMG height=\"252\" alt=\"Microsoft Word\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/randomfont1.jpg\" width=\"450\" border=\"0\"> \n<P><BR>Because we\u2019re 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 <B>System.Random<\/B>, and by assigning our low number (1) to a variable named intLow, and our high number, plus 1, to a variable named intHigh. That\u2019s what we do in this block of code:<\/P><PRE class=\"codeSample\">Set objRandom = CreateObject(&#8220;System.Random&#8221;)<\/p>\n<p>intLow = 1\nintHigh = 5\n<\/PRE>\n<P>As for accessing each and every character in the document, well, these two lines of code give us the ability to do that:<\/P><PRE class=\"codeSample\">Set objRange = objDoc.Range()\nSet colCharacters = objRange.Characters\n<\/PRE>\n<P>In line 1 we\u2019re creating an instance of Word\u2019s <B>Range<\/B> object, an object that \u2013 in lieu of any other instructions \u2013 encompasses the entire document. In line 2, we\u2019re creating an object reference to the range object\u2019s <B>Characters<\/B> property; as the name implies, the Characters property contains a collection of all the characters found in the range.<\/P>\n<P>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:<\/P><PRE class=\"codeSample\">For Each strCharacter in colCharacters\n<\/PRE>\n<P>Inside that loop, the first thing we do is use the <B>Next_2<\/B> method to generate a random number between 1 and 4, inclusive:<\/P><PRE class=\"codeSample\">intRandom = objRandom.Next_2(intLow,intHigh)\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"EHG\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Did Congo come up with the name Next_2 for this method? Hey, come on: that\u2019s an insult to chimpanzees everywhere!<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>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:<\/P><PRE class=\"codeSample\">Select Case intRandom\n    Case 1 strCharacter.Font.Name = &#8220;Arial&#8221;\n    Case 2 strCharacter.Font.Name = &#8220;Times New Roman&#8221;\n    Case 3 strCharacter.Font.Name = &#8220;Courier New&#8221;\n    Case 4 strCharacter.Font.Name = &#8220;Forte&#8221;\nEnd Select\n<\/PRE>\n<P>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 we\u2019ll end up with a document that looks something like this:<\/P><IMG height=\"253\" alt=\"Microsoft Word\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/randomfont2.jpg\" width=\"450\" border=\"0\"> \n<P><BR>Granted, this particular example, using these fonts, might not look much like handwriting. But you get the idea.<\/P>\n<P>Considering the fact that he created over 300 paintings in his brief, three-year 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. Congo\u2019s second script takes a different approach from his first: this script takes a set of letters (in this case, <I>a<\/I>, <I>d<\/I>, <I>l<\/I>, and <I>s<\/I>), methodically searches the document for each of those characters, and then changes the font of each <I>a<\/I>, <I>d<\/I>, <I>l<\/I>, or <I>s<\/I> it encounters. We\u2019re 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. Here\u2019s the code for Congo\u2019s second script:<\/P><PRE class=\"codeSample\">Const wdReplaceAll = 2<\/p>\n<p>Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True<\/p>\n<p>Set objDoc = objWord.Documents.Open(&#8220;C:\\Scripts\\Test.doc&#8221;)\nSet objSelection = objWord.Selection<\/p>\n<p>arrLetters = Array(&#8220;a&#8221;, &#8220;d&#8221;, &#8220;l&#8221;, &#8220;s&#8221;)<\/p>\n<p>For Each strLetter in arrLetters\n    objSelection.Find.Text = strLetter\n    objSelection.Find.Forward = TRUE\n    objSelection.Find.MatchWholeWord = False<\/p>\n<p>    objSelection.Find.Replacement.Text = strLetter\n    objSelection.Find.Replacement.Font.Name = &#8220;Forte&#8221;<\/p>\n<p>    objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll\nNext\n<\/PRE>\n<P>That\u2019s all we have time for today, CW; as it its, it\u2019s 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 <I>really<\/I> signed by the Scripting Guys. If you\u2019re worried about that, take a careful look at the signatures on your certificate. Scripting Guy Greg Stemp\u2019s largely-illegible signature should look similar to this:<\/P><IMG height=\"139\" alt=\"Microsoft Word\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/randomfont3.jpg\" width=\"375\" border=\"0\"> \n<P><BR>Meanwhile, Scripting Guy Jean Ross\u2019 signature should look like this:<\/P><IMG height=\"104\" alt=\"Microsoft Word\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/randomfont4.jpg\" width=\"414\" border=\"0\"> \n<P><BR>And yes, we noticed that, too. But her handwriting was so nice we hated to say anything about her spelling. Maybe next year.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I\u2019ve 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 slightly-different fonts, taken from my actual handwriting, and then randomly assigning a font to each letter. I think that would imitate [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[84,49,3,5,395],"class_list":["post-55713","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-vbscript","tag-word-application"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I\u2019ve 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 slightly-different fonts, taken from my actual handwriting, and then randomly assigning a font to each letter. I think that would imitate [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=55713"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55713\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=55713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}