Controlling text wrapping around an image.

D

david_alex_smith

Hello

We generate word documents from content in a database (AuthorIT
application). I want to have an image on the front page of my document
_behind_ the text. When generated, the text is wrapped around the image. I
want to use a macro to set the image to display beneath the text.

I have come up with this macro:

Sub MoveImage()
Dim iShape, Shape
On Error Resume Next
For Each iShape In ActiveDocument.Sections.First.Range.InlineShapes
iShape.ConvertToShape
Next
For Each Shape In ActiveDocument.Sections.First.Range.ShapeRange
Shape.WrapFormat.Type = wdWrapThrough
Next
End Sub

This doesn't work when run as soon as the doc is generated, but curiously
does work if i manually select the image then run it.

Can anyone help me to develop a macro that will set the desired wrapping
without any manual action?

Thanks

Dave.
 
D

david_alex_smith

I should also mention that the image is output in my own-defined style. So if
it's possible to have images in a particular style sit beneath the text, this
would be a good solution.

Any ideas?
 
H

Helmut Weber

Hi David,

I don't know, but this works for me:

Dim oShp As Shape
For Each oShp In ActiveDocument.Range.ShapeRange
oShp.WrapFormat.Type = wdWrapNone ' 3 !
Next

And your variable definitions are far from perfect.
Also, "on error" seems to be useless here.

Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
H

Helmut Weber

....
same applies to
oShp.WrapFormat.Type = wdWrapThrough
of course.

Helmut Weber
 
D

david_alex_smith

Helmut,

Thanks, but I get:

"Requested object is not available" when i run this.

Dave.
 
D

david_alex_smith

It seems like the image is inserted as an inlineshape.

This works:

Sub MoveImage()
Dim oShp As Shape
Dim iShape As InlineShape
For Each iShape In ActiveDocument.Sections.First.Range.InlineShapes
iShape.ConvertToShape
Next
For Each oShp In ActiveDocument.Shapes
oShp.WrapFormat.Type = wdWrapThrough
Next
End Sub

BUT ONLY if the image is selected in the doc. Any ideas on how to make it
work regardless? Shall i attempt to select the image programatically first?
 
H

Helmut Weber

Hi David,

I simplified the code a bit, reduced it to the essentials.
And thought, you could adapt it without problems.

Well, a more explicit piece of code could look like this:

' ---
Dim oInlSh As InlineShape ' Object Inlineshape
Dim oShape As Shape ' Object Shape

For Each oInlSh In ActiveDocument.Range.InlineShapes
oInlSh.ConvertToShape
Next
For Each oShape In ActiveDocument.Range.ShapeRange
oShape.WrapFormat.Type = wdWrapThrough
Next
' ---
If You want to adress sections,
you have to add the appropriate code pieces again.

Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
H

Helmut Weber

Hi David,

hard to say anything more,

as the code I just pasted from your posting,
works perfectly here and now without any alterations.

Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
D

david_alex_smith

OK Helmut, thanks for your efforts anyway. (Sorry I'm only an amateur)

It's strange. The ConvertToShape method fails unless I first select the
image. I might try and find a totally different solution.
 

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