Jed,
Refer to Jonathan's reply for help with the insertion part. If you insert
as an inline shape, then select that shape, the following code will
automatically size it to fit within the margins without altering the aspect
ratio, using the insertion point in place of the top margin (so that if
there were text on the page before the shape, the shape would fill the
remaining space rather than bumping to a new page), but as long as you
insert on a blank page it will size to fit the whole page.
Public Sub OptimumSizeObject()
Dim PHeight As Single, PWidth As Single, LMargin As Single, RMargin As
Single, _
BMargin As Single, OHeight As Single, OWidth As Single, tmargin As
Single
Dim CHeight As Single, CWidth As Single, NHeight As Single, NWidth As
Single
'Get the dimensions of available space
tmargin = Selection.Information(wdVerticalPositionRelativeToPage)
Application.ScreenUpdating = False
PHeight = Selection.Sections(1).PageSetup.PageHeight
PWidth = Selection.Sections(1).PageSetup.PageWidth
LMargin = Selection.Sections(1).PageSetup.LeftMargin +
Selection.ParagraphFormat.LeftIndent
RMargin = Selection.Sections(1).PageSetup.RightMargin
BMargin = Selection.Sections(1).PageSetup.BottomMargin
'Compute Optimum Height and Width for Object
OHeight = PHeight - (tmargin + BMargin + 24)
OWidth = PWidth - (LMargin + RMargin + 9)
'Trap errors for shapes that are floating
On Error GoTo NotAnInLineShape
'Get current height and width of shape
CHeight = Selection.InlineShapes(1).Height
CWidth = Selection.InlineShapes(1).Width
'Set NHeight to optimum height and NWidth proportionally
'for change in height
NHeight = OHeight
NWidth = NHeight * CWidth / CHeight
'If NWidth is larger than optimum width, then reverse so that
'NWidth is optimum width and NHeight is set proportionally
'for change in width.
If NWidth > OWidth Then
NWidth = OWidth
NHeight = CHeight * NWidth / CWidth
End If
On Error GoTo 0
'Set the height and width of the object to the optimums
'computed above
With Selection.InlineShapes(1)
.LockAspectRatio = msoTrue
.Height = NHeight
.Width = NWidth
End With
'Reset options and clear variable used by macro
Application.ScreenRefresh
Application.ScreenUpdating = True
Exit Sub
'Error Handler
NotAnInLineShape: MsgBox "There is no InLineShape selected!"
Application.ScreenUpdating = True
End Sub
Regards,
Chad DeMeyer
Jed Harrison said:
I am creating documents through code using VBA, and I need to insert a
picture. Either at a given position, or at a bookmark, whichever is
easiest. Does anyone have any relatively simple sample code that does this,
or can point me to some?
Extra points for sizing the picture after the insert. I am inserting an
emf file, and I want to make sure to take up the whole page.if what I ask seems obvious.