Log in

View Full Version : Convert tables to Images - bottom rows missing



arangogs
07-18-2024, 05:26 AM
Hi All,

I am attempting to convert all tables in a document to images, the code seems to work fine when the tables\pages are in portrait orientation. The issue of bottom rows being missing presents itself when landscape orientation has been applied.

There is a prepping process that unmerges cells and splits tables per individual page, where appropriate.

I have attempted many fix options, all to no avail. Code used below.


Sub ConvertTablesToImages(ByVal currentDoc As Word.Document)


Dim tbl As Table, rng As Range, i As Integer
Dim tWidth As Long, tHeight As Long

For i = currentDoc.Tables.Count To 1 Step -1
Set tbl = currentDoc.Tables(i)
tbl.Select
Set rng = tbl.Range
'Get table width prior to resetting
'tWidth = ReturnTableWidth(tbl)
'tHeight = ReturnTableHeight(tbl)
tbl.PreferredWidth = 0
tbl.Range.CopyAsPicture

rng.Collapse Direction:=wdCollapseStart
'rng.Collapse Direction:=wdCollapseEnd

tbl.Delete
rng.PasteSpecial DataType:=wdPasteEnhancedMetafile

'rng.ShapeRange(1).LockAspectRatio = msoFalse
rng.ShapeRange(1).ConvertToInlineShape


Next
End Sub


any help appreciated, tearing my hair out with this one.

Thanks

Gordon

Dave
07-18-2024, 10:37 AM
Hi Gordon. Not sure if this will help, but here is some code that Macropod provided me to keep tables on the same page... it sounds like this may be an issue for you. HTH. Dave

'prevent tables from splitting page
For Each Otbl In ActiveDocument.Tables
Otbl.Range.Paragraphs.keepwithnext = True
For Each Ocel In Otbl.Rows.last.Range.Cells
Ocel.Range.Paragraphs.last.keepwithnext = False
Next Ocel
Next Otbl

arangogs
07-19-2024, 01:53 AM
Many thanks for the response Dave, much appreciated.

Unfortunately, the tables sometimes span many pages, thus this would not work. As i said, it is a the orientation that appears to be the issue, further head scratching and testing yesterday. I had originally had an issue with the image only pasting half the columns, resolved by adding "tbl.PreferredWidth = 0"

Removing this line yesterday, led to all rows being displayed, but back to only half of the columns again :(

more trawling of internet today, i feel, for the nugget of info i require.

Thanks or your help once again

Dave
07-22-2024, 01:01 PM
Hi again Gordon. You can set your table width with the following code. You can also trial removing the commented code to see what happens. HTH. Dave

With CurrentDoc.PageSetup
TblWdth = .PageWidth - .LeftMargin - .RightMargin - .Gutter
End With
'format table
With CurrentDoc.Tables(1)
'.AutoFormat Format:=16, applyborders:=True
'.AutoFitBehavior (0)
.Columns.Width = TblWdth / .Columns.Count
End With

edit: this will set all of your columns to the same width

arangogs
07-23-2024, 01:23 AM
Hi Dave,

Many thanks for the response once again. I have been toiling with many scenario's since my last post and finally managed to get the code working, bizarrely I have to perform effectively the same actions in varying ways dependant on the orientation of the page. resetting the width on landscape.

I am trying to iron out a couple of niggly bits at the moment, but once i am done, I will post the code here.


Thanks again for your help.

Gordon