Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help adding pictures in Word using VBA 1

Status
Not open for further replies.

tlogan

Programmer
Jun 26, 2001
33
US
Hello All -

I've searched around and haven't found what I'm looking for. I'm working on a project to create an input form for a rather complicated Word template containing many table cells. One of things I'd like to be able to do is to add a picture as a watermark over a group of cells.

I can do the following by hand, but am not able to figure out how to so it using VBA.

INSERT -> Picture -> From File - (select picture) - double click on picture to get the Format Picture Dialog -> Picture Tab - select "washout" - Layout Tab - select "behind text" - close dialog - drag picture to correct location and size.

What I've tried so far....

Code:
ActiveDocument.Shapes.AddShape(msoShapeRectangle, 12, 141, 120, 200).Fill _
.UserPicture "C:\Documents and Settings\xxx.jpg"

This is the ONLY way I've been able to insert a picture at the correct location and size. BUT, I can't manipulate it at all. I've tried to convert to an inline shape, but that doesn't seem to help. I've tried every combination of things I can find in the VBA help files/examples, but I'm not having any luck.

Anyone know the secret?

Any help is GREATLY appreciated.
Tom
 
Have you tried using the macro recorder wile you do it by hand then editing the code the macro recorder generates?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Thanks TTKP -

No, not yet. My past experience is that that generates a LOT of extraneous code. I thought that perhaps there is more elegant and simple way to code it. That is really my last resort.

Any one else?

Tom
 
That is really my last resort
Hey, that is really the FIRST thing I try.
Then I go thru the object browser (f2 key) and play with the help (f1 key) in order to make the code more simple and elegant ...
 


Your Macro recorder is your friend.

Start with it and then customize.

Try setting pic to something OTHER THAN in-line.

Set the Pix -- Top, Left, Height, Width -- properties in accordance with the location and size of the area you want the watermark to be in.

Skip,
[sub]
[glasses] [red]Sign above the facsimile apparatus at the music publisher:[/red]
If it ain't baroque...
Don't FAX it![tongue][/sub]
 
OK...

I just tried recording....

I am able to insert the picture, but while recordding I cannot double click it to continue the rest of the process and editing.

I'll try some of the other suggetions.

Tom
 
By the way ...

The macro I recorded to insert the picture sets it as an InlineShape.


Code:
Selection.InlineShapes.AddPicture FileName:= _
    "C:\Documents and Settings\tlogan4\My Documents\racingstripes_releaseposter.jpg" _
    , LinkToFile:=False, SaveWithDocument:=True

Tom
 


Word has some limitation in the macro editor that baffle me too.

Once you have gone as far as you can using the macro recorder. Turn it off, post your code and describe what you need to do.

Check out: How to use the Watch Window as a Power Programming Tool faq707-4594

Skip,
[sub]
[glasses] [red]Sign above the facsimile apparatus at the music publisher:[/red]
If it ain't baroque...
Don't FAX it![tongue][/sub]
 
The code above (in my last post) IS as far as I can go. While recording I cannot double click to continue the format process as described earlier in the thread.

I want to

Insert a picture, place it, size it over a range of cells, watermark it.

so far I've got this, which inserts the picture and watermarks it, but can't figure out how to move and size it where I want it.

Code:
Selection.InlineShapes.AddPicture FileName:= _
    "C:\Documents and Settings\xxx.jpg" _
    , LinkToFile:=False, SaveWithDocument:=False

ActiveDocument.InlineShapes(1).ConvertToShape
ActiveDocument.Shapes(1).PictureFormat.ColorType = msoPictureWatermark

Seems pretty simple, but you know how that goes.

Tom
 


Take PHV's suggestions. The Object Browser & Help will help you find the properties and methods you will need.

In the Document, when you manually FORMAT the pic, the SAME properties are the ones you'll need to manipulate. LOOK for them in OB & Help.

Skip,
[sub]
[glasses] [red]Sign above the facsimile apparatus at the music publisher:[/red]
If it ain't baroque...
Don't FAX it![tongue][/sub]
 
Quite a number of properties for InlineShapes are not exposed to the macro recorder.

However, while you may not be able to double click, try (when using the macro recorder) using Shift-F10. This is the keyboard equivalent to a right click. This allows a number of further commands to get into the macro recorder. This is handy not just for this instance, but all instances where the macro recorder does not allow right clicks.

That being said, while you CAN get at washout using the macro recorder (it actually sets the values of Contrast and Brightness), you can NOT get at anything in Layout. NO values under Layout are available (at all) during macro recording.

Further, the location gets very weird to handle by code.

You CAN convert to Shapes, as you have done. Once converted to Shape, there are a few things that are now exposed. You can get at them using ShapeRange. Left and Right can move the Shape, and ZOrder exposes the layout properties.
Code:
ActiveDocument.InlineShapes(1).Select
With Selection.InlineShapes(1)
   .ConvertToShape
   With .ShapeRange
      .Left = InchesToPoints(3)
      .Top = InchesToPoints(5)
      .ZOrder msoSendBehindText
   End With
End With

To the gentlemen who have encouraged the use of the macro recorder, I totally agree that it is our friend. However, both Shapes and InlineShapes have some very very weird things in the object model, and not many of them are available, AND trying to find things with the Object Browser is extremely difficult. Unless you really study the model it is almost impossible to even stumble on things.

Good luck with this. Programming with standard graphic objects in Word is an ugly ugly business....at best.

You may want to consider looking at using an ActiveX Image control instead. They are, in fact, much better behaved. At least in some ways.

Gerry
 
fumei -

Thanks for the point in the right direction. I guess I just needed a little push. The following seems to be working. Now I just need to play around until I come with correct values for the exact placement I want. But this semes to be what I'm after.

Thanks!
Tom

Code:
Selection.InlineShapes.AddPicture FileName:= _
    "C:\Documents and Settings\XXX.jpg" _
    , LinkToFile:=False, SaveWithDocument:=True
    
ActiveDocument.InlineShapes(1).ConvertToShape
With ActiveDocument.Shapes(1)
    .PictureFormat.ColorType = msoPictureWatermark
    .ZOrder msoSendBehindText
    .RelativeHorizontalPosition = _
        wdRelativeHorizontalPositionPage
    .Top = InchesToPoints(2)
    .Left = InchesToPoints(-0.5)
    .Width = InchesToPoints(1.7)
    .Height = InchesToPoints(2.5)
End With
 
That is in fact the direction you need to go. As I stated, trying to find this with Object Browser and/or macro recorder would fail miserably. However, poke around Help a lot; also use Intellisense to get various properties and methods of things.

One annoying thing though is:

ActiveDocument.InlineShapes(1).OLEFormat.Object

There ARE further properties and methods, but putting the dot after Object ("Object.") does NOT bring up Intellisense. I don't know why.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top