First, I'll say that there are several ways to accomplish the look you want,
and the Image control is only one of them -- and probably the least
tractable from a VBA point of view. The problem is that an Image control,
like all the items in the Control Toolbox, is an ActiveX control that has
relatively poor support in VBA. General information about these controls,
but not specifically about the Image control, is at
http://msdn2.microsoft.com/en-us/library/aa140269(office.10).aspx. Pay close
attention to the section "Appropriateness for the Task", which details a
number of possible problems.
After showing you the answer to your specific question, I'll point to a
better alternative.
Assuming that your Image control is inserted in-line with text, you first
address it as a member of the ActiveDocument.InlineShapes collection. If its
text wrapping is anything else, you get to it through the
ActiveDocument.Shapes collection. Then you should verify that the
InlineShape (or Shape) is of the expected type, and get its
..OLEFormat.Object property which represents the Image control itself. You
can set its .Picture property by calling LoadPicture with the name of the
desired picture file, adjust its sizing properties, and finally force it to
update:
Sub ChangeImgPicture()
Dim PicName As String
Dim ipic As InlineShape
Dim oOle As OLEFormat
Dim img As Image
PicName = "C:\somefolder\somepicture.jpg" ' set as needed
Set ipic = ActiveDocument.InlineShapes(1)
If ipic.Type = wdInlineShapeOLEControlObject Then
If ipic.OLEFormat.ClassType = "Forms.Image.1" Then
Set img = ipic.OLEFormat.Object
If LCase(img.Name) = "logo" Then
With img
.Picture = LoadPicture(PicName)
.AutoSize = False
.PictureSizeMode = fmPictureSizeModeZoom
End With
' force update of picture
ActiveWindow.View = wdPrintPreview
ActiveWindow.View = wdPrintView
End If
End If
End If
End Sub
I'm not sure (and I don't have any way to test right now) whether the image
data is stored in the document file -- so that the picture will be visible
if you send the document to another computer that doesn't have access to the
original picture file -- or whether it will display some sort of error
message.
~~~~~~~~~
Instead of using an Image control, you can use a one-cell borderless table,
a text box, a bookmark, or several other items that are native to Word to
indicate where the picture should be inserted. A one-cell table has the
advantage that, with a little setup, it can automatically size whatever
picture you choose to insert.
In the template for your report document, insert the table (because you're
putting in a logo, I expect it will be the first table in the document,
which VBA addresses as ActiveDocument.Tables(1); if not, choose the correct
index number). With the table selected, click Table > AutoFit > Fixed Column
Width. In Table > Properties > Rows, set the minimum height you want. In the
cell, press Ctrl+Alt+U to turn off the border lines (or you can do this in
the Format > Borders dialog).
Then the macro code will look like this:
Sub InsertLogo()
Dim PicName As String
Dim oRg As Range
PicName = "C:\somefolder\somepicture.jpg" ' set as needed
Set oRg = ActiveDocument.Tables(1).Cell(1, 1).Range
' remove any existing picture in the table
While oRg.InlineShapes.Count
oRg.InlineShapes(1).Delete
Wend
ActiveDocument.InlineShapes.AddPicture _
FileName:=PicName, LinkToFile:=False, _
SaveWithDocument:=True, Range:=oRg
End Sub
There's no need to force an update here. Also, because of the LinkToFile and
SaveWithDocument settings, you're guaranteed that the image will travel with
the document file.