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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word 2003 Insert image using macro

Status
Not open for further replies.

ProfessorGrimes

Technical User
Nov 7, 2005
4
US
How can I insert an image into a *specific place* in a Word 2003 document *once* regardless of how many times the macro is run.

Key words there are "Specific Place" and "Once".

I've tried using:
ActiveDocument.Bookmarks("ThisBookmark").Range.InlineShapes.AddPicture FileName:="C:\ThisImage.jpg"

This will place the image every time I run the macro. I only want one image in the document... not one for every time I run the macro.

If I don't use a bookmark then the image does not end up in the correct place in the document.

I'd like the image to act kind of like a document variable in that each time I run the macro it retrieves the image file and places it in the location of the "docvariable".
 


hi,

Code:
For Each oInLin in ActiveDocument.InlineShapes
  if oInLin.Name = "TheName" then Exit Sub
Next
Set oPic = ActiveDocument.Bookmarks("ThisBookmark").Range.InlineShapes.AddPicture FileName:="C:\ThisImage.jpg"
oPic.Name = "TheName"
Code:
Sub AddOnceMacro



Skip,

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

I've been trying different things and found something that works. In the document I define a DocVariable within an INCLUDEPICTURE like this:

{INCLUDEPICTURE {DOCVARIABLE ThisVariable \* MERGEFORMAT} \* MERGEFORMAT}

And then include this line in a Macro:

.Variables("ThisVariable").Value = "C:\ThisImage.jpg"

This works except sometimes the image is too big for the page so I'm trying to find a way to size it properly.

Is there an advantage to using the previously mentioned method vs. the method mentioned above?
 
Uh, the two methods have (either of them) nothing at all to do with your sizing issue.

Of the two, for what it is worth, I prefer Skip's method.

Back to your sizing issue.

Sizing - after the fact, and by code - an InlineShape can be tricky. Can be done though, but it is, unfortunately, not possible to get the code by using the macro recorder. Parts are exposed to the recorder, but not all.

Please state exactly what you want to happen.

Gerry
 
> Please state exactly what you want to happen.

Primary need:
I want to place a picture in a specific location of a document.

Special note:
The picture file name will be the same each time, but the directory path will change every time.

Secondary needs:
1. Place the picture in the document ONCE regardless of how many times the macro is run. The new picture should replace the old picture when the macro is run.
2. Size: The picture should automatically resize to fit on the page. This is kind of an understood thing... but not necessarily according to Word. As a bonus, complete height & width control is preferred.

----------------

As an update to what I did earlier, after running the macro and updating the fields, this:

{INCLUDEPICTURE {DOCVARIABLE ThisVariable \* MERGEFORMAT} \* MERGEFORMAT}

turns to this:

{INCLUDEPICTURE "C:/ThisImage.jpg" \* MERGEFORMAT}

If the file is saved like this, the DOCVARIABLE disappears and the "C:/ThisImage.jpg" is hard coded into the file. In reality, the ThisImage.jpg will exist in different directories.
 
Couple of ways.

1. ActiveX Image control. The default name is Image1. The advantage here is that you have full control of height and width, PLUS the code loads the file directly. Any existing value (image) in the control is removed/replaced. You would just need to have the path to the file as a string variable - rather than hard coded in the example below.
Code:
With Image1
    .AutoSize = False
    .PictureSizeMode = fmPictureSizeModeStretch
    .Picture = LoadPicture("c:\test\coot1.jpg")
    .Height = 300
    .Width = 250
End With

The ActiveX control can be placed where ever you like.

2. Use a bookmark, in this example it is named "PictureHere". You do not have easy access to sizing.
Code:
With Selection
 .GoTo what:=wdGoToBookmark, Name:="PictureHere"
 .InlineShapes.AddPicture FileName:="C:\test\coot1.jpg", _
     LinkToFile:=False, SaveWithDocument:=True
End With

Gerry
 
The Active X control looks like it will do exactly what I need... but I've never done this before so I need a little more guidance please.

I opened up the Control Toolbox, clicked on 'Image' control and placed a square in the document.

I double-click the square and it brings up the code:

Code:
Private Sub Image1_Click()

End Sub

I enter your code from above into the function and... nothing happens. :(

How/Where do I assign the string to be used in LoadPicture()?
How do I get the square to display the image, in other words how do I execute the code?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top