Textbox positioning under an image problem

C

chris.w.news

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
 
S

Shauna Kelly

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
 
C

Cindy M.

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

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 :)
 
C

chris.w.news

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
 
J

Jean-Guy Marcil

(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
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.

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
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

chris.w.news

Thanks everyone for your help.

Using the range object and "Keep with next" was just what I needed.

Kind regards,
Chris
 
Top