{"id":63873,"date":"2007-10-04T23:29:00","date_gmt":"2007-10-04T23:29:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/10\/04\/hey-scripting-guy-how-can-i-convert-an-extension-number-to-an-actual-phone-number\/"},"modified":"2007-10-04T23:29:00","modified_gmt":"2007-10-04T23:29:00","slug":"hey-scripting-guy-how-can-i-convert-an-extension-number-to-an-actual-phone-number","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-convert-an-extension-number-to-an-actual-phone-number\/","title":{"rendered":"Hey, Scripting Guy! How Can I Convert an Extension Number to an Actual Phone Number?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Our phone numbers are stored in Active Directory as extension numbers; however, we have a set of rules for converting those extensions to actual phone numbers. I\u2019m writing a script to create an Outlook signature, but in that script I need to be able to convert an extension to a phone number. How do I do that?<BR><BR>&#8212; SM<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, SM. You know, you are looking at one tired Scripting Guy today; in fact, you\u2019re looking at one totally <I>exhausted<\/I> Scripting Guy. The Scripting Guys have been busily preparing for their forthcoming trip to Barcelona (for the <A href=\"http:\/\/www.mseventseurope.com\/teched\/07\/itforum\/Pages\/Default.aspx\" target=\"_blank\"><B>TechEd IT Forum<\/B><\/A>), and the Scripting Guy who writes this column has been working pretty much round the clock the past few days to ensure that he made all his deadlines. He did, although it wasn\u2019t easy, and the past few nights \u2013 when he\u2019s averaged maybe 4 hours of sleep \u2013 have finally caught up to him.<\/P>\n<TABLE id=\"EID\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Didn\u2019t this Scripting Guy know well in advance that he was going to Barcelona? Sure. And hasn\u2019t he had over a month to complete his assignments, meaning he could have done this a long time ago without having to lose a minute of sleep? Yes, of course he did. But what\u2019s your point?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>At any rate, the Scripting Guy who writes this column is tired. Too tired to write a new column, even one that shows you how you can convert an extension number to an actual phone number? Yes, <I>way<\/I> too tired. Come back tomorrow and we\u2019ll see if he feels like writing a new column then.<\/P>\n<P>Nah, just kidding. The Scripting Guy who writes this column always has room for Jell-O, and he always has enough get-up-and-go to write a column that shows you how you can convert an extension number to an actual phone number (hey, who doesn\u2019t?). Here\u2019s a portion of the script; we\u2019ll show you the rest in due time:<\/P><PRE class=\"codeSample\"><P class=\"MsoNormal\"><SPAN>strNumber = &#8220;7596&#8221;<\/SPAN><\/P><P class=\"MsoNormal\"><SPAN>&nbsp;<\/SPAN><\/P><P class=\"MsoNormal\"><SPAN>If Left(strNumber, 2) = &#8220;74&#8221; Then<\/SPAN><\/P><P class=\"MsoNormal\"><SPAN><SPAN>&nbsp;&nbsp;&nbsp; <\/SPAN>strNumber = &#8220;3112-74&#8221; &amp; Right(strNumber, 2)<\/SPAN><\/P><P class=\"MsoNormal\"><SPAN>End If<\/SPAN><\/P><P class=\"MsoNormal\"><SPAN>&nbsp;<\/SPAN><\/P><P class=\"MsoNormal\"><SPAN>If Left(strNumber, 2) = &#8220;75&#8221; Then<SPAN>&nbsp;&nbsp;&nbsp; <\/SPAN><\/SPAN><\/P><P class=\"MsoNormal\"><SPAN>    strNumber = &#8220;3115-85&#8221; &amp; Right(strNumber, 2)<\/SPAN><\/P><P class=\"MsoNormal\"><SPAN>End If<\/SPAN><\/P><P class=\"MsoNormal\"><SPAN>&nbsp;<\/SPAN><\/P><P class=\"MsoNormal\"><SPAN>Wscript.Echo strNumber<\/SPAN><\/P><\/PRE>\n<P>Gee, now that we did all that typing we don\u2019t seem to have near enough energy to explain how this script works. Guess we\u2019ll just have to go home then. See you all tomorr \u2013<\/P>\n<P>Never mind; the Scripting Editor just gave us \u201cThe Look.\u201d<\/P>\n<TABLE id=\"E3D\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. What\u2019s The Look? Remember in those old science fiction movies when someone would get hit with a blast from a ray gun and would literally <I>explode<\/I>? Those guys got off easy compared to anyone who\u2019s ever gotten The Look.<\/P>\n<P>Is that our spleen lying over there or has someone else gotten The Look recently?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Anyway, as we were saying, let\u2019s take some time to explain how this script works. In order to keep things simple, all we\u2019re showing you at the moment is code that converts an extension to an actual phone number; at the end of this column, we\u2019ll show you a more complete script, one that can grab the extension from Active Directory, convert the extension to a phone number, and then create the Outlook signature. But all that comes at the <I>end<\/I> of the column. Here, in the <I>middle<\/I> of the column, we\u2019re going to limit ourselves to discussing the process by which we convert an extension to a phone number.<\/P>\n<P>Before we do that, however, let\u2019s briefly explain SM\u2019s rules for converting an extension to a phone number. In SM\u2019s organization, extensions have one of two prefixes: <B>74<\/B> or <B>75<\/B>. How do you convert an extension like 74 to an actual phone number? Well, suppose we have the extension 7439. In this case, we first convert the <B>74<\/B> (the first two digits in the extension) to <B>3112-74<\/B>. We then grab the last two digits of the extension (<B>39<\/B>) and tack those onto the <B>3112-74<\/B>. What does <B>3112-74<\/B> plus <B>39<\/B> give us? That\u2019s right: a valid phone number.<\/P>\n<TABLE id=\"ENF\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Yes, it would be better if we weren\u2019t acting coy and simply <I>showed<\/I> you the resulting phone number. For better or worse, however, there are a whole bunch of rules regarding phone numbers and whether a number can or cannot be displayed on the Web. We\u2019re already tired enough as it is, so we decided to bypass the issue altogether. Originally we were going to convert each extension to 555-; that way we could show you a phone number like 555-7439. But it seemed weird to convert the 74 extensions to 555- <I>and<\/I> to convert the 75 extensions to 555-. So we decided to instead write a column about phone numbers that never once displays a phone number. Another Scripting Guys first!<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As for extensions beginning with <B>75<\/B>, we do the same sort of thing, in that case combining <B>3115-75<\/B> and the last two digits of the extension.<\/P>\n<P>Which, interestingly enough, is the same process the Scripting Guy who writes this column used to pick out the name of his son. (Why else would we always refer to him as \u201cThe Scripting Son\u201d rather than by name?)<\/P>\n<P>As for the script (or script snippet), we start off by assigning an extension (in this case 7596) to a variable named strNumber. We then use the <B>Left<\/B> function to determine whether or not the first two digits in the extension are equal to 74. If they <I>are<\/I>, we then construct a new phone number, combining the standard prefix <B>3112-74<\/B> with the last two digits of the extension. And how do we know what the last two digits of the extension are? That\u2019s easy: we simply use the <B>Right<\/B> function to grab the last two characters in the variable strNumber.<\/P>\n<P>That, by the way, is what this block of code is for:<\/P><PRE class=\"codeSample\"><P class=\"MsoNormal\"><SPAN>If Left(strNumber, 2) = &#8220;74&#8221; Then<\/SPAN><\/P><P class=\"MsoNormal\"><SPAN><SPAN>&nbsp;&nbsp;&nbsp; <\/SPAN>strNumber = &#8220;3112-74&#8221; &amp; Right(strNumber, 2)<\/SPAN><\/P><P class=\"MsoNormal\"><SPAN>End If<\/SPAN><\/P><\/PRE>\n<P>What if the first two digits in the extension <I>aren\u2019t<\/I> 74? No problem; in that case we just drop down to the next If-Then statement, one that checks to see if the first two digits are equal to 75 and, if so, takes the appropriate action:<\/P><PRE class=\"codeSample\"><P class=\"MsoNormal\"><SPAN>If Left(strNumber, 2) = &#8220;75&#8221; Then<SPAN>&nbsp;&nbsp;&nbsp; <\/SPAN><\/SPAN><\/P><P class=\"MsoNormal\"><SPAN>    strNumber = &#8220;3115-85&#8221; &amp; Right(strNumber, 2)<\/SPAN><\/P><P class=\"MsoNormal\"><SPAN>End If<\/SPAN><\/P><\/PRE>\n<P>And, of course, if we had more extension types (say, 76 and 77) we could continue to add additional If-Then blocks as needed.<\/P>\n<P>That\u2019s really all there is to it; when we\u2019re all done we echo back the value of strNumber. To see that value, just close your eyes and imagine a phone number being echoed back to the command window.<\/P>\n<TABLE id=\"E6G\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. By a remarkable coincidence, imaging a phone number being echoed back to the command window is the exact same dream that the Scripting Editor has almost every night. Amazing but true!<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Now, how would we use this code snippet in a real, live script? Well, here\u2019s one way:<\/P>\n<P class=\"MsoNormal\"><SPAN>Set objWord = CreateObject(&#8220;Word.Application&#8221;)<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>&nbsp;<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>Set objDoc = objWord.Documents.Add()<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>Set objSelection = objWord.Selection<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>&nbsp;<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>Set objSysInfo = CreateObject(&#8220;ADSystemInfo&#8221;)<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>&nbsp;<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>strUser = objSysInfo.UserName<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>Set objUser = GetObject(&#8220;LDAP:\/\/&#8221; &amp; strUser)<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>&nbsp;<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>strName = objUser.FullName<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>strTitle = objUser.Title<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>strDepartment = objUser.Department<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>strCompany = objUser.Company<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>strNumber = objUser.telephoneNumber<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>&nbsp;<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>If Left(strNumber, 2) = &#8220;74&#8221; Then<SPAN>&nbsp;&nbsp;&nbsp; <\/SPAN><\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN><SPAN>&nbsp;&nbsp;&nbsp; <\/SPAN>strNumber = &#8220;3112-74&#8221; &amp; Right(strNumber, 2)<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>End If<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>&nbsp;<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>If Left(strNumber, 2) = &#8220;75&#8221; Then<SPAN>&nbsp;&nbsp;&nbsp; <\/SPAN><\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN><SPAN>&nbsp;&nbsp;&nbsp; <\/SPAN>strNumber = &#8220;3115-85&#8221; &amp; Right(strNumber, 2)<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>End If<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>&nbsp;<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>Set objEmailOptions = objWord.EmailOptions<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>Set objSignatureObject = objEmailOptions.EmailSignature<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>&nbsp;<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>Set objSignatureEntries = objSignatureObject.EmailSignatureEntries<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>&nbsp;<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>objSelection.TypeText strName &amp; &#8220;, &#8221; &amp; strTitle<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>objSelection.TypeParagraph()<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>objSelection.TypeText strDepartment<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>objSelection.TypeParagraph()<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>objSelection.TypeText strCompany<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>objSelection.TypeParagraph()<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>objSelection.TypeText strNumberSet <\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>&nbsp;<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>objSelection = objDoc.Range()<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>&nbsp;<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>objSignatureEntries.Add &#8220;AD Signature&#8221;, objSelection<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>&nbsp;<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>objDoc.Saved = True<\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN>objWord.Quit<\/SPAN><\/P>\n<P>We aren\u2019t going to explain how this script works; we have an entire <A href=\"http:\/\/www.microsoft.com\/technet\/technetmag\/issues\/2006\/10\/HeyScriptingGuy\/default.aspx\" target=\"_blank\"><B>TechNet Magazine article<\/B><\/A> devoted to that subject. Suffice to say that this script connects to Active Directory and, for the logged-on user, retrieves information such as full name, title, department, company, and, of course, phone number. After converting the extension number to an actual phone number (look for our code snippet \u2026 hey, there it is!), the script then proceeds to create a new email signature named AD Signature.<\/P>\n<P>Incidentally, this all happens without anything appearing on screen. If you prefer to watch the magic unfold, make this the second line of code (and comment out the very last line, which causes Word to terminate):<\/P><PRE class=\"codeSample\"><P class=\"MsoNormal\"><SPAN>objWord.Visible = True<\/SPAN><\/P><\/PRE>\n<P>So now that all the work for Barcelona is done (well, mostly done) what\u2019s next for the Scripting Guys? That would probably be the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/default.mspx\"><B>2008 Winter Scripting Games<\/B><\/A>, scheduled for February 15, 2008 through March 3, 2008. This time around the Games are going to be way bigger and way better than ever. (For one thing, we\u2019ve added a new language to the mix: Perl.) And yes, that <I>is<\/I> going to be a lot of work.<\/P>\n<P>But hey, we have plenty of time between now and then, so \u2026.<\/P>\n<P>&nbsp;<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Our phone numbers are stored in Active Directory as extension numbers; however, we have a set of rules for converting those extensions to actual phone numbers. I\u2019m writing a script to create an Outlook signature, but in that script I need to be able to convert an extension to a phone number. How do I [&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,4,21,5],"class_list":["post-63873","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-scripting-techniques","tag-string-manipulation","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Our phone numbers are stored in Active Directory as extension numbers; however, we have a set of rules for converting those extensions to actual phone numbers. I\u2019m writing a script to create an Outlook signature, but in that script I need to be able to convert an extension to a phone number. How do I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63873","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=63873"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63873\/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=63873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}