Hi, Paul Edstein
Thank you again, really appreciated!
I used what Yongle told me, to add a bookmark at the foot of the table, in order to get the table's height.
And here comes the problem, which is bookmark location cannot updated IMMEDIATELY when columnwidth changed.
Here is the background information:
I'm trying to copy a lot of tables from EXCEL to WORD using vba/vb, in order to make the tables look nice in WORD, I have to adjust column width of each table.
As you know, tables varying in lots of shapes, which means there may exist mergecells or some cells may occupy more than one line/row.
For example,
one 3 * 5 table, in cell(3,2), the contents is much longer than any cells in columns(2), the columns(2)'s perfect width is 5 cm, that means cell(3,2) occupies 2 lines,and other cells in columns(2) occpies one line, which can make the table looks nice. When pasted to WORD, columns(2)'s width is 6 cm, so I use do while... loop to reduce columns(2)'s width by 0.1cm each time, and at the same time, read the bookmark's location(
wdVerticalPositionRelativeToPage), if bookmark's vertical location doesnot change,means columns(2)'s width can be reduced, if changed, that means each cell in columns(2) occupy more than one line,and I can know that the table looks not so good. When I run the loop by manual(F8), the program goes well, cause the table and the bookmark can change at once,but when the program run by itself (F5), the table's width and bookmark's location cannot change AT ONCE, so columns(2)'s width is reduced to a very much small value,maybe 1cm. how to solve this problem?
See some part of the code(code saved in Excel vbe) below:
Code:
Sub demo()
Dim bm_old As Single
Dim myrange As Word.Range
Set wordobj = CreateObject("word.application")
wordobj.Visible = 1
wordobj.documents.Add
Set doc = wordobj.ActiveDocument
Set myrange = doc.Range(doc.Content.End - 1, doc.Content.End - 1)
ThisWorkbook.Sheets(1).UsedRange.Copy
myrange.PasteExcelTable False, False, False
With wordobj.Selection.Tables(1) 'add bookmark at the foot of the table
myrange.SetRange Start:=.Range.End - 1, End:=.Range.End - 1
doc.bookmarks.Add Range:=myrange, Name:="temp"
End With
bm_old = WorksheetFunction.Round(doc.bookmarks("temp").Range.Information(wdVerticalPositionRelativeToPage) / CentimetersToPoints(1), 2)
*******
Call columnwidth_adjust(bm_old, 2)
*******
End Sub
Sub columnwidth_adjust(bm_old As Single, i As Integer)
Dim bm_new As Single
Dim myrange As Word.Range
bm_new = bm_old
wordobj.Selection.Tables(1).Cell(1, 2).Select 'cell(1,2).for example
With wordobj.Selection
.Tables(1).AutoFitBehavior (wdAutoFitFixed)
.SelectColumn
Do While bm_old = bm_new
bm_new = 0
.Columns.PreferredWidth = CentimetersToPoints(thisworkbook.Sheets(2).Cells(1, i) - 0.1) 'this is the place where the columnwidth(in cm) saved
tiquwb.Sheets("Check").Cells(1, i) = thisworkbook.Sheets(2).Cells(1, i) - 0.1
bm_new = WorksheetFunction.Round(doc.bookmarks("temp").Range.Information(wdVerticalPositionRelativeToPage) / CentimetersToPoints(1), 2) 'bm_new cannot updated immediately!!!
Loop
wordobj.Selection.Tables(1).Cell(i_cellrow, i).Select
With wordobj.Selection
.Columns.PreferredWidth = CentimetersToPoints(thisworkbook.Sheets(2).Cells(1, i) + 0.2)
End With
End With
End Sub
Paul Edstein, how to solve bookmark's location cannot updated immediately? Thank you!!!