Word 2K, AddPicture, Shape.Left, Shape.Top

M

Michael Conroy

Hi all -

I've been developing in Word 2003 and have a VB.NET automation routine that
plops an image into a Word doc both locating and sizing the newly inserted
image based on a blank picture. The whole point is to allow the doc creator
to locate and size the image exactly how he wants it. (He has a utility that
allows him to name the shape so I can find it later).

In Word 2K, this whole method breaks down. It seems as though Shape.Left
and Shape.Top don't really work the same way. When I insert the picture in
Word 2K, it simply puts it wherever and ignores the location information. It
looks like it's sizing the image right, but if I can't control where it
lands, the whole algorithm goes out the window and I have to start again.

So again - design goals:

1. Word user should be able to create the landing zone for the picture by
inserting the new picture object, sizing it and locating it as appropriate.
2. Word user names the shape using a utility.
3. VB.NET automation app grabs the document and shape, inserts the new
picture into the document using the original picture object as a template for
both size and location.

Works great in 2003. Bombs in 2000.

HELP!!!!!

Thanks,
Mike
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?TWljaGFlbCBDb25yb3k=?=,
I've been developing in Word 2003 and have a VB.NET automation routine that
plops an image into a Word doc both locating and sizing the newly inserted
image based on a blank picture. The whole point is to allow the doc creator
to locate and size the image exactly how he wants it. (He has a utility that
allows him to name the shape so I can find it later).
The .Top and .Left properties don't work in Word 2000 ("it's broken") when you
use them in the .AddPicture method. But in my experience, they do function
correctly if you apply them AFTER the shape has been inserted. So, roughly,

Dim shp as Word.Shape
Set shp = doc.Shapes.AddPicture(Filename, etc)
shp.Left = 13.6
shp.Top = 12.8

I can't recall ever having found a case where they don't work like this, as long
as you're not doing something that would delete/recreate the picture in the
document. And with the caveat that odd things COULD happen if these are linked.
In Word 2K, this whole method breaks down. It seems as though Shape.Left
and Shape.Top don't really work the same way. When I insert the picture in
Word 2K, it simply puts it wherever and ignores the location information. It
looks like it's sizing the image right, but if I can't control where it
lands, the whole algorithm goes out the window and I have to start again.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
M

Michael Conroy

Ha! The great Cindy! I've been reading your various pearls of wisdom for a
year or two now. How cool to actually have you answer my post!

Thanks a bunch - however, the problem isn't so much in positioning the
graphic as it is determining the position to use as the template. In your
example, you write:
Dim shp as Word.Shape
Set shp = doc.Shapes.AddPicture(Filename, etc)
shp.Left = 13.6
shp.Top = 12.8

Problem is - 13.6 and 12.8 should be derived from another picture object.
So, what I'm doing is...

Dim oTemplate as Word.Shape
Set oTemplate=doc.Shapes("templateShape")
Dim shp as Word.Shape
Set shp = doc.Shapes.AddPicture(Filename, etc)
shp.Left = oTemplate.Left
shp.Top = oTemplate.Top

In this case, oTemplate.Left and oTemplate.Top read incorrectly. To get
around this, "templateShape" is no longer a MS Word Picture object - it's a
text box. For some reason, TextBox.Left and TextBox.Top work just fine.
Ideally, I'd love to just replace the empty image in the Picture object with
a different image, but I see no way to do that, so I just insert a new image
with AddPicture directly on top of the template image. (I should probably
nuke the template image after this and rename the newly inserted shape so I
can go back and apply the same algorithm to swap out images if I need to...
but anyway, that I can figure out.)

Again - it's a pleasure... You've been a HUGE help many times when I've had
a problem with Word automation.

Best,
Mike
 
C

Cindy M -WordMVP-

Hi Mike,

Hmmm. Have you discovered the RelativeHorizontalPosition and
RelativeVerticalPosition properties, yet? These will tell you if Top and Left
are relative to the anchor position, or to the page. If you're reading one type
of value, but applying the other type, I could imagine things get pretty
chaotic :)

As to the question about replacing an existing graphic with another, no, in my
experience you do have to start over from scratch again, saving and reapplying
all the properties. Unless, of course, the image is linked. Then,
theoretically, you could change the SourceFullName. But this will crash Word
2000, as I recall.

And, finally, thanks for the positive feedback :) We tend to forget how many
people silently lurk out there, and profit from the information exchange in the
NGs. But this is the reason for NGs (as opposed to emails and such), and it's
great to be reminded that the theory actually works said:
Dim oTemplate as Word.Shape
Set oTemplate=doc.Shapes("templateShape")
Dim shp as Word.Shape
Set shp = doc.Shapes.AddPicture(Filename, etc)
shp.Left = oTemplate.Left
shp.Top = oTemplate.Top

In this case, oTemplate.Left and oTemplate.Top read incorrectly. To get
around this, "templateShape" is no longer a MS Word Picture object - it's a
text box. For some reason, TextBox.Left and TextBox.Top work just fine.
Ideally, I'd love to just replace the empty image in the Picture object with
a different image, but I see no way to do that, so I just insert a new image
with AddPicture directly on top of the template image. (I should probably
nuke the template image after this and rename the newly inserted shape so I
can go back and apply the same

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top