{"id":66173,"date":"2006-10-26T16:56:00","date_gmt":"2006-10-26T16:56:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/10\/26\/how-can-i-center-align-a-picture-in-a-word-document\/"},"modified":"2006-10-26T16:56:00","modified_gmt":"2006-10-26T16:56:00","slug":"how-can-i-center-align-a-picture-in-a-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-center-align-a-picture-in-a-word-document\/","title":{"rendered":"How Can I Center-Align a Picture in a Word Document?"},"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>Hey, Scripting Guy! How can I center-align a picture in a Word document?<BR><BR>&#8212; CD<\/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, CD. In light of the controversy surrounding World Series game 2 (was Detroit Tigers pitcher Kenny Rogers throwing \u201cgoopballs\u201d or wasn\u2019t he?) the Scripting Guys felt it was important to note that no illegal substances were used in the writing of today\u2019s <I>Hey, Scripting Guy!<\/I> column: no pine tar, no Vaseline, not even any peanut butter. Nothing.<\/P>\n<P>OK, true, there <I>are<\/I> cookie crumbs wedged between some of the keys on the keyboard. But those don\u2019t count.<\/P>\n<TABLE id=\"EED\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. If you\u2019re not a fan of America\u2019s national pastime we should make it clear that the sport has very strict rules against \u201cdoctoring\u201d baseballs. Why do major league umpires throw out any baseball that has so much as a blemish on it? Because major league pitchers can take a baseball that has a scuff mark, a dab of Vaseline, or a little pine tar on it and then throw a pitch that is essentially unhittable: typically the ball will come in straight and then, swiftly and unexpectedly, dive straight into the ground. (According to the old clich\u00e9 \u201clike it rolled off a table.\u201d) Because these pitches are well-nigh impossible to hit, the so-called \u201cspitball\u201d has been illegal for decades.<\/P>\n<P>And yes, up until 1920 the spitball was legal; in fact, Scripting Guy Peter Costantini used to throw the spitter as a boy in New Jersey.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Of course, at the risk of tarnishing the spotless reputation of the Scripting Guys, the Scripting Guy who writes this column would have happily used an illegal substance when writing today\u2019s column; he just couldn\u2019t figure out how a dab of Vaseline would help him center-align a picture in Microsoft Word. As it turns out, the process isn\u2019t all that difficult, but it\u2019s not exactly intuitive, either, as you\u2019re about to see.<\/P>\n<TABLE id=\"ERD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Ethical question<\/B>. So if the Scripting Guy who writes this column would have happily used an illegal substance in writing this column does that mean he would have used an illegal substance when pitching a baseball? Heavens no! After all, that would have been against the rules.<\/P>\n<P>Besides, he could never really control the spin of the ball any time he tried.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>At any rate, CD, here\u2019s a perfectly natural, perfectly legal script that center-aligns a picture in Microsoft Word:<\/P><PRE class=\"codeSample\">Const wdShapeCenter = -999995 \nConst wdWrapSquare = 0<\/p>\n<p>Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True\nSet objDoc = objWord.Documents.Add()<\/p>\n<p>Set objShape = objDoc.Shapes\nobjShape.AddPicture(&#8220;C:\\Scripts\\Welder-small.jpg&#8221;)<\/p>\n<p>Set objShapeRange = objDoc.Shapes.Range(1)\nobjShapeRange.Left = wdShapeCenter<\/p>\n<p>objShapeRange.WrapFormat.Type = wdWrapSquare\n<\/PRE>\n<P>No promises here, but we\u2019ll try to explain this script the best we can. As you can see, we start out by defining a pair of constants, wdShapeCenter (and yes, -999995 <I>is<\/I> the real value we need to assign to the constant) and wdWrapSquare. We\u2019ll use these two constants to center the picture (wdShapeCenter) and then to configure the picture word wrap (wdWrapSquare).<\/P>\n<P>After defining the constants we create an instance of the <B>Word.Application<\/B> object and set the <B>Visible<\/B> property to True; that gives us a running instance of Microsoft Word that we can see on screen. We then use this line of code to create a new document we can work with:<\/P><PRE class=\"codeSample\">Set objDoc = objWord.Documents.Add()\n<\/PRE>\n<P>Our next step is to insert a picture into the document. To do that we create an instance of the document\u2019s <B>Shapes<\/B> collection, then use the <B>AddPicture<\/B> method to insert the picture C:\\Scripts\\Welder-small.jpg. That\u2019s what these two lines of code are for:<\/P><PRE class=\"codeSample\">Set objShape = objDoc.Shapes\nobjShape.AddPicture(&#8220;C:\\Scripts\\Welder-small.jpg&#8221;)\n<\/PRE>\n<P>OK; now it gets a tad bit convoluted. <\/P>\n<P>As you doubtless know, CD, by default Word left-aligns any pictures you insert programmatically. To center the picture we use these two lines of code:<\/P><PRE class=\"codeSample\">Set objShapeRange = objDoc.Shapes.Range(1)\nobjShapeRange.Left = wdShapeCenter\n<\/PRE>\n<P>In the first line we create an instance of the document\u2019s <B>ShapeRange<\/B> object. To do that we reference the Shapes collection and the <B>Range<\/B> property; in particular, we reference the first Range which, for our purposes, is just the first picture in the collection. That\u2019s what the <B>Range(1)<\/B> is for. What if we had two pictures in our document and we wanted to center-align picture 2? Then we\u2019d use code like this:<\/P><PRE class=\"codeSample\">Set objShapeRange = objDoc.Shapes.Range(2)\n<\/PRE>\n<P>After we have a ShapeRange object that references our picture we can then align the graphic by setting the value of the <B>Left<\/B> property to the constant wdShapeCenter. Alternatively, we could right-align the graphic by using the constant wdShapeRight and the value -999996. Or we could set the Left property to wdShapeLeft (with the value -999998); that, in turn, left-aligns the picture.<\/P>\n<P>It\u2019s weird, but that\u2019s the way it works.<\/P>\n<TABLE id=\"ELF\" 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>. In one of those astonishing coincidences that link baseball, scripting, and real life, -999998 is also the batting average compiled by the Scripting Editor during this past softball season. To tell you the truth, we\u2019re not totally sure how she could even <I>have<\/I> a negative batting average. But statistics don\u2019t lie.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>The final line of code in the script is optional, but you might find it useful. By default any picture you programmatically insert into a Word document gets plastered on top of the text. In other words, if you start adding text to your document you\u2019ll end up with something that looks like this:<\/P><IMG border=\"0\" alt=\"Microsoft Word\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/wraptext1.jpg\" width=\"400\" height=\"340\"> \n<P><BR>It might be a little hard to see in this screenshot, but the picture covers up some of the text. Admittedly, with a <I>Hey, Scripting Guy!<\/I> article, covering up the text isn\u2019t necessarily a bad thing. But we\u2019re guessing you\u2019d prefer to be able to still read the document you just added your graphic to.<\/P>\n<P>To address that problem we use this line of code to set the wrap <B>Type<\/B> to square (a task which requires us to reference the <B>WrapFormat<\/B> object, which is a child object of the ShapeRange object):<\/P><PRE class=\"codeSample\">objShapeRange.WrapFormat.Type = wdWrapSquare\n<\/PRE>\n<P>Now when we add text to the document that text will wrap nicely around the graphic:<\/P><IMG border=\"0\" alt=\"Microsoft Word\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/wraptext2.jpg\" width=\"400\" height=\"352\"> \n<P><BR>Much better.<\/P>\n<P>In case you\u2019re interested, here\u2019s a list of other wrap Type constants and values:<\/P>\n<TABLE id=\"EUG\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Constant<\/B><\/P><\/TD>\n<TD>\n<P class=\"lastInCell\"><B>Value<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">wdWrapInline<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">7<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">wdWrapNone<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">3<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">wdWrapSquare<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">0<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">wdWrapThrough<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">2<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">wdWrapTight<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">1<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">wdWrapTopBottom<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">4<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Hope that helps, CD.<\/P>\n<P>Although we hate to moralize, the Scripting Guys would like to close by stating we think it\u2019s a shame that, in this day and age, anyone who does something really well is immediately accused of cheating, whether that\u2019s by doctoring a baseball, taking steroids, or engaging in some other illicit practice. Sadly, that\u2019s the price you pay for success these days.<\/P>\n<P>Interestingly enough, the Scripting Guys have never <I>once<\/I> been accused of cheating. <\/P>\n<P>Hmmm \u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I center-align a picture in a Word document?&#8212; CD Hey, CD. In light of the controversy surrounding World Series game 2 (was Detroit Tigers pitcher Kenny Rogers throwing \u201cgoopballs\u201d or wasn\u2019t he?) the Scripting Guys felt it was important to note that no illegal substances were used in the writing [&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],"class_list":["post-66173","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I center-align a picture in a Word document?&#8212; CD Hey, CD. In light of the controversy surrounding World Series game 2 (was Detroit Tigers pitcher Kenny Rogers throwing \u201cgoopballs\u201d or wasn\u2019t he?) the Scripting Guys felt it was important to note that no illegal substances were used in the writing [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66173","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=66173"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66173\/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=66173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}