Getting a carriage return or something before a pic in a table cell so that I can show the border??

M

Mojo

Hi All

Some of you have kindly assisted me in getting 'x' number of pictures into
'x' number of table cells in a Word document using VBA, but I seem to have
hit a brick wall when it comes to getting the top border line to appear on
my inserted pics.

Now as soon as I manually add a carriage return above the pictures my top
border appears perfectly, but my code won't let me add this carriage return
before the pic. When the pic gets inserted it automatically obliterates
anything that was already in the cell and I get a pic with no top border.

I've tried giving a top margin to each cell so that it forces the issue, but
it doesn't work. It looks like Word needs that carriage return prior to
inserting the pic.

My code is currently as follows:

For y = 0 To UBound(arrData, 2)
If Len(arrData(2, y)) > 0 Then ' check that a picture name exists
Set oPic =
oNewDoc.Tables(intTable).Rows.Add.Cells(2).Range.InlineShapes.AddPicture(arrData(2,
y), False, True)
With oPic
.Width = 200
.Height = 150
.Borders.OutsideLineStyle = wdLineStyleSingle
.Borders.OutsideLineWidth = wdLineWidth075pt
.Borders.OutsideColor = wdColorAutomatic
End With
End If

strText = arrData(1, y) & vbCrLf ' desc
If Len(arrData(3, y)) > 0 Then strText = strText & arrData(3, y) & vbCrLf
' comment
oNewDoc.Tables(intTable).Rows.Add.Cells(2).Range.Text = strText
Next

The above works fine I just need to get a carriage return (or anything!!!)
that gets my top border to appear. I've got the left, right and bottom
borders no problem :0)

Any ideas?
 
J

Jay Freedman

The simplest solution is to add some Space Before (maybe 6 pt) to the
paragraphs in the table. If the table is predefined in a template used
to create oNewDoc, you can just add the space to the table's text in
the template. If the table is built by code in your macro, then you
can use a line like this:

oNewDoc.Tables(intTable).Range.Paragraphs.SpaceBefore = 6

It might be a good idea to add some Space After as well.

If you really want a paragraph before the picture (strongly NOT
recommended), the problem with what you're doing now is this: The
range you're using to insert the picture is Cells(2).Range, and that
includes the paragraph mark you already put in the cell. The
AddPicture replaces the whole range with the picture. The solution for
that is to declare a Range object, set it to Cells(2).Range, collapse
it to its start, and move one character to the right. Use that as the
range (a single point) to insert the picture:

Dim oRg As Range

....

Set oRg = oNewDoc.Tables(intTable).Rows.Add.Cells(2).Range
oRg.Collapse wdCollapseStart
oRg.Move wdCharacter, 1
Set oPic = oRg.InlineShapes.AddPicture(arrData(2, y), False, True)

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

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