vb设置word中的图片

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值