simple question add picture to document

J

Jed Harrison

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.
 
J

Jonathan West

Hi Jed,

There is sample code in the Word VBA help file

Depending on whether you want to insert the picture "floating" or "inline"
you use the Add method of the Shapes collection or the Add method of the
InlineShapes collection.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

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.
 
C

Chad DeMeyer

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.
 
C

Chad DeMeyer

The only potential problem I see so far is that you are calling the
OptimumSizeObject sub with an argument (theDoc) and the code I supplied
wasn't set up to receive arguments, so unless you have modified the code to
do so that could be part of the problem. If not, can you be more specific
about where your code falters and what error message you get?

Regards,
Chad DeMeyer


Jed Harrison said:
Chad, I tried using you code to size the image, but I'm afraid I must have
made some obvious newbie mistake in the selection. From reading the help, I
got the impression that the inlineImages collection is a 1 based count - eg.
There is only one image in my document so I should be able to use the code
below:
I am trying to select the image, then pass it to your sub to resize it.
I'm running from Access without opening a visible word window, so I removed
all code that refered to screen operations. The calling code looks like
this:
If theDoc.Bookmarks.Exists("insertImage") Then
Dim bmRange As Range
Set bmRange = theDoc.Bookmarks("insertImage").Range
theDoc.InlineShapes.AddPicture ("C:\Jed\Parks map
reports\testEMF\" & strParkName(i) & ".emf"), , , bmRange
theDoc.InlineShapes(1).Select
Call OptimumSizeObject(theDoc)
Else
bmRange.InsertAfter "No map found for this park - see the GIS
department for more information."
 
C

Chad DeMeyer

Jed,

Sorry so long to get back to you. Try setting a range variable to the
location where the object is inserted and using Range.Information instead of
Selection.Information. I think the Selection object can cause these types
of problems in a hidden instance of Word.

cjd


Jed Harrison said:
I did modify the code to receive theDoc as word.document. The error I get is on the first line

tmargin = Selection.Information(wdVerticalPositionRelativeToPage)

object variable or with block variable not set. I'm thinking I haven't
actually got a proper selection. If it would help I could post the rest of
the code as modified, but essentially, other than the added argument, I
simply removed all the references to application.screen* as it seemed
irrelevant for this purpose, I am not visibly opening word.
 

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