How Can I Center-Align a Picture in a Word Document?

How Can I Center-Align a Picture in a Word Document?

  • Comments 4
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I center-align a picture in a Word document?

-- CD

SpacerHey, Scripting Guy! AnswerScript Center

Hey, CD. In light of the controversy surrounding World Series game 2 (was Detroit Tigers pitcher Kenny Rogers throwing “goopballs” or wasn’t he?) the Scripting Guys felt it was important to note that no illegal substances were used in the writing of today’s Hey, Scripting Guy! column: no pine tar, no Vaseline, not even any peanut butter. Nothing.

OK, true, there are cookie crumbs wedged between some of the keys on the keyboard. But those don’t count.

Note. If you’re not a fan of America’s national pastime we should make it clear that the sport has very strict rules against “doctoring” 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é “like it rolled off a table.”) Because these pitches are well-nigh impossible to hit, the so-called “spitball” has been illegal for decades.

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.

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’s column; he just couldn’t figure out how a dab of Vaseline would help him center-align a picture in Microsoft Word. As it turns out, the process isn’t all that difficult, but it’s not exactly intuitive, either, as you’re about to see.

Ethical question. 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.

Besides, he could never really control the spin of the ball any time he tried.

At any rate, CD, here’s a perfectly natural, perfectly legal script that center-aligns a picture in Microsoft Word:

Const wdShapeCenter = -999995 
Const wdWrapSquare = 0

Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add()

Set objShape = objDoc.Shapes
objShape.AddPicture("C:\Scripts\Welder-small.jpg")

Set objShapeRange = objDoc.Shapes.Range(1)
objShapeRange.Left = wdShapeCenter

objShapeRange.WrapFormat.Type = wdWrapSquare

No promises here, but we’ll 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 is the real value we need to assign to the constant) and wdWrapSquare. We’ll use these two constants to center the picture (wdShapeCenter) and then to configure the picture word wrap (wdWrapSquare).

After defining the constants we create an instance of the Word.Application object and set the Visible 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:

Set objDoc = objWord.Documents.Add()

Our next step is to insert a picture into the document. To do that we create an instance of the document’s Shapes collection, then use the AddPicture method to insert the picture C:\Scripts\Welder-small.jpg. That’s what these two lines of code are for:

Set objShape = objDoc.Shapes
objShape.AddPicture("C:\Scripts\Welder-small.jpg")

OK; now it gets a tad bit convoluted.

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:

Set objShapeRange = objDoc.Shapes.Range(1)
objShapeRange.Left = wdShapeCenter

In the first line we create an instance of the document’s ShapeRange object. To do that we reference the Shapes collection and the Range property; in particular, we reference the first Range which, for our purposes, is just the first picture in the collection. That’s what the Range(1) is for. What if we had two pictures in our document and we wanted to center-align picture 2? Then we’d use code like this:

Set objShapeRange = objDoc.Shapes.Range(2)

After we have a ShapeRange object that references our picture we can then align the graphic by setting the value of the Left 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.

It’s weird, but that’s the way it works.

Note. 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’re not totally sure how she could even have a negative batting average. But statistics don’t lie.

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’ll end up with something that looks like this:

Microsoft Word


It might be a little hard to see in this screenshot, but the picture covers up some of the text. Admittedly, with a Hey, Scripting Guy! article, covering up the text isn’t necessarily a bad thing. But we’re guessing you’d prefer to be able to still read the document you just added your graphic to.

To address that problem we use this line of code to set the wrap Type to square (a task which requires us to reference the WrapFormat object, which is a child object of the ShapeRange object):

objShapeRange.WrapFormat.Type = wdWrapSquare

Now when we add text to the document that text will wrap nicely around the graphic:

Microsoft Word


Much better.

In case you’re interested, here’s a list of other wrap Type constants and values:

Constant

Value

wdWrapInline

7

wdWrapNone

3

wdWrapSquare

0

wdWrapThrough

2

wdWrapTight

1

wdWrapTopBottom

4

Hope that helps, CD.

Although we hate to moralize, the Scripting Guys would like to close by stating we think it’s a shame that, in this day and age, anyone who does something really well is immediately accused of cheating, whether that’s by doctoring a baseball, taking steroids, or engaging in some other illicit practice. Sadly, that’s the price you pay for success these days.

Interestingly enough, the Scripting Guys have never once been accused of cheating.

Hmmm ….

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • This was a waste of my time to read this. I am trying to work, not to  program or read about baseball (although I love baseball). The previous versions of MS Office offered real help, not a bunch of gobbedlygook. Why don't you people put something simple like centering a picture into the program correctly the first time?

  • @Ken I am sorry you felt this blog post was a waste of your time (It was written five years ago, before I became the Scripting Guy). As a matter of fact, I do not even like baseball, so I do not write about it). However, the purpose of the Hey Scripting Guy blog, is, and has always been, about scripting -- which is a form of light weight programing. Many times, however, our readers will simply take the script and use it as is, without attempting to understand programing that goes into the script. At other times, our readers read just becaue they like the pictures I post (I am into scuba diving and travel) or because they happen to like baseball. I can understand your frustation. The Microsoft Office team is very responsive to customer feedback -- the best way to provide feedback to them would be via either their techcenter / blog / or user forum. They pay attention to feedback, and use it to help them develop new features. I hope this helps.

  • Hi Scripting guy, I've stumbled across this page.

    I was excellent in showing me how to right align an image in my outlook signature script, but I also need to move the image down the page ( the way it works now, its right aligned at the top of the email footer, I'm trying to get to right align about 10 point above the footers last line ).

    Ive tried

    objShapeRange.Top = wdShapeTop

    ( where wdShapeTop has been defined previously as -50 )

    but it doesn't seem to do anything.

    Its what I'm trying to do even possible ?

  • thanks