Insert Image; Set Layout & Size

G

Gordon Bentley-Mix

I've got code for inserting an image into a document (using a bookmark to
specify the location), and I've also got separate code for converting all of
the Shapes in a document into Inline Shapes and resizing them to a
particular standard. What I'd like to do is sort of combine the two
operations and insert an image as an Inline Shape and resize it all in one
go. Unfortunately, I've not had a lot of experience in working with images
through VBA - most of my current code is recorded stuff that I've tweaked -
so I'm not really sure where to even start. Can someone point me in the
general direction, and I'll take it from there?
 
J

Jay Freedman

I've got code for inserting an image into a document (using a bookmark to
specify the location), and I've also got separate code for converting all of
the Shapes in a document into Inline Shapes and resizing them to a
particular standard. What I'd like to do is sort of combine the two
operations and insert an image as an Inline Shape and resize it all in one
go. Unfortunately, I've not had a lot of experience in working with images
through VBA - most of my current code is recorded stuff that I've tweaked -
so I'm not really sure where to even start. Can someone point me in the
general direction, and I'll take it from there?
--
Cheers!

Gordon Bentley-Mix
Word MVP

Hi Gordon,

This is extracted from my form_picture.dot template, which you can
download from http://jay-freedman.info.

' change these numbers to the maximum width and height
' (in inches) to make the inserted pictures
Const PicWidth = 1.9
Const PicHeight = 2.25

Public Sub FormInsertPicture()
' ...
Dim PicRg As Range
Dim Photo As InlineShape
Dim RatioW As Single, RatioH As Single, RatioUse As Single

' ...

' insert the picture
Set Photo = .InlineShapes.AddPicture(FileName:=FName, _
LinkToFile:=False, SaveWithDocument:=True, _
Range:=PicRg)
With Photo
RatioW = CSng(InchesToPoints(PicWidth)) / .Width
RatioH = CSng(InchesToPoints(PicHeight)) / .Height

' choose the smaller ratio
If RatioW < RatioH Then
RatioUse = RatioW
Else
RatioUse = RatioH
End If

' size the picture to fit the cell
.Height = .Height * RatioUse
.Width = .Width * RatioUse
End With

' re-add the bookmark so it can be reused
.Bookmarks.Add Name:=LocName, Range:=Photo.Range
 
D

DaveLett

Hi Gordon,
I think this will get you started.

Dim oShp As InlineShape
Dim oRng As Range

Set oRng = Selection.Range '''really the range of your bookmark
Set oShp = ActiveDocument.InlineShapes.AddPicture _
(FileName:="C:\Documents and Settings\dlett\Desktop\Ben Photos\Ben April
'09 004.jpg", _
Range:=oRng)
With oShp
.Height = .Height * 2
.Width = .Width / 2
End With

HTH,
Dave
 
G

Gordon Bentley-Mix on news.microsoft.com

Thanks Dave. After I posted last night I got thinking about this and came up
with pretty much the same approach - but it's good to have confirmation all
the same.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 

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