Hi,
I'm trying to using VBA to take a description (the caption) from an
Excel document and place it under a JPEG image in Word. I originally
tried to do this by just using TypeText a paragraph under the image but
as it would always put it on the next page, I decided to use a Textbox.
My code is as follows:
Set wdApp = New Word.Application
With wdApp
.Visible = True
.WindowState = wdWindowStateMaximize
End With
Set myDoc = wdApp.Documents.Add
With wdApp.Selection
'Set page as landscape
.PageSetup.Orientation = wdOrientLandscape
'Align as Centre
.ParagraphFormat.Alignment =
Word.WdParagraphAlignment.wdAlignParagraphCenter
.TypeParagraph
' Insert as an InlineShape
Set objShape = myDoc.InlineShapes.AddPicture(imageAddress)
imagePosition =
objShape.Range.Information(wdVerticalPositionRelativeToPage)
' Resize the image
If objShape.Height > InchesToPoints(4) Then
objShape.Height = InchesToPoints(4)
objShape.ScaleWidth = objShape.ScaleHeight
End If
.TypeParagraph
Set txtBox =
myDoc.Shapes.AddTextbox(msoTextOrientationHorizontal, 235,
imagePosition, 330, 60)
txtBox.TextFrame.TextRange.Text = docDate & " " & docRef & ": "
& description
txtBox.ZOrder (msoBringToFront)
txtBox.Line.Visible = msoFalse
End With
I've tried resizing the image to a smaller size and using the image
position to place the text but whatever method I try it always places
the text box on a second page and never directly underneath.
How can I get the text to go directly under the photo and on the same
page. The images are generally all 640 x 480 pixels.
Thanks for any help,
Chris
Shauna Kelly - 13 Sep 2006 13:44 GMT
Hi Chris
I'm assuming that you do not need text to wrap around your images and
captions. That is, I'm assuming that what you need in your document is:
image
caption
image
caption
image
caption
etc.
If that's the case, then don't use a text box. Just put the caption in
ordinary text. Mark the paragraph into which you put the inline shape with
"Keep with next". Word will then keep the paragraph holding the image on the
same page as the next paragraph (ie on the same page as the caption).
There's more info on how to keep an image on the same page as its caption at
How to keep a figure on the same page as its caption
http://www.ShaunaKelly.com/word/figures/keepwithcaption.html
Hope this helps.
Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
> Hi,
>
[quoted text clipped - 53 lines]
> Thanks for any help,
> Chris
chris.w.news@googlemail.com - 13 Sep 2006 14:07 GMT
Hi Shauna
Thanks for the reply. I added
.Paragraphs(1).KeepWithNext = True
to the program and it sorted the 2-page problem. The text now gets
placed before the photo rather than on a line underneath it.
The new code is as follows:
With wdApp.Selection
'Set page as landscape
.PageSetup.Orientation = wdOrientLandscape
'Align as Centre
.ParagraphFormat.Alignment =
Word.WdParagraphAlignment.wdAlignParagraphCenter
' Insert as an InlineShape
.Paragraphs(1).KeepWithNext = True
Set objShape = myDoc.InlineShapes.AddPicture(imageAddress)
.TypeText vbCrLf & "Hello!"
How can I get the text to show under the photo.
Thanks for any help,
Chris
> Hi Chris
>
[quoted text clipped - 79 lines]
> > Thanks for any help,
> > Chris
Jean-Guy Marcil - 13 Sep 2006 15:36 GMT
chris.w.news@googlemail.com was telling us:
chris.w.news@googlemail.com nous racontait que :
> Hi Shauna
>
[quoted text clipped - 21 lines]
>
> How can I get the text to show under the photo.
Try not to use the Selection Object. It makes for more convoluted, less
reliable and slower code.
I see that you posted only the relevant code form a longer sub, so I tried
to recreate you code in a single code to give you an example of what you
could do with the range object instead of the selection object:
'_______________________________________
Private Sub InsertImage()
Dim wdApp As Word.Application
Dim rgeImage As Range
Dim objShape As InlineShape
Set wdApp = Application
'In case user has a range selected
wdApp.Selection.Collapse wdCollapseStart
Set rgeImage = wdApp.Selection.Range
With rgeImage
'Set page as landscape
.PageSetup.Orientation = wdOrientLandscape
'Align as Centre
.ParagraphFormat.Alignment = _
Word.WdParagraphAlignment.wdAlignParagraphCenter
' Insert as an InlineShape
.Paragraphs(1).KeepWithNext = True
Set objShape = .InlineShapes.AddPicture(ImagePath)
'move range end to the end of the inline picture,
'which is like a single character to Word
.MoveEnd wdCharacter, 1
.InsertAfter vbCrLf & "Hello!"
'Otherwise, the Caption Para will also be "Keep with next"
'This caption para is now the second para in the range
'Because of the vbCrLf
.Paragraphs(2).KeepWithNext = False
End With
End Sub
'_______________________________________

Signature
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org
chris.w.news@googlemail.com - 14 Sep 2006 11:00 GMT
Thanks everyone for your help.
Using the range object and "Keep with next" was just what I needed.
Kind regards,
Chris
> chris.w.news@googlemail.com was telling us:
> chris.w.news@googlemail.com nous racontait que :
[quoted text clipped - 73 lines]
> jmarcilREMOVE@CAPSsympatico.caTHISTOO
> Word MVP site: http://www.word.mvps.org
Cindy M. - 13 Sep 2006 13:54 GMT
The most reliable method will be to insert the JPEG formatted "in-line
with text" (as a member of the InlineShapes collection) into a Word
FRAME. Press right-arrow then ENTER to create a new line in the frame.
Then the caption can be inserted in the same frame - and you KNOW the two
things will always stay together.
An alternate method would be to use a one- or two-row table (again, the
JPEG should be in-line with the text).
Another possibility would be to format that paragraph in which the
InlineShape is with "KeepWithNext". Then it should stay on the same page
with the caption - ASSUMING there's enough room on the page, between the
margins.
There is simply no way to guarantee that a SHAPE will always stay
together with its caption, although you could probably guarantee they'd
at least be on the same page by inserting the caption's text into the
paragraph to which the Shape is anchored. Or making sure the TextBox is
anchored to the same paragraph as the Shape.
Looking at your code, I see the that you are inserting as an
InlineShape...
> I'm trying to using VBA to take a description (the caption) from an
> Excel document and place it under a JPEG image in Word. I originally
[quoted text clipped - 50 lines]
> How can I get the text to go directly under the photo and on the same
> page. The images are generally all 640 x 480 pixels.
Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org
This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :-)