word vba, table.column.width returns 999999

jackni

Board Regular
Joined
Feb 10, 2011
Messages
73
Hi, All

In my word VBA, the following code returns 999999, what does the result 999999 means?
It's not points, not centimeters, not percent, not any of the length unit used in WORD.

Code:
activedocument.tables(1).columns.width

I'm looking for a code to read the table's width in centimeters, is someone here can help me, Thank you very much!!!
 
Since you know which table it is, there is no need to bookmark it, and even that is insufficient on its own: for a table than doesn't span a page break, all you need normally do is to get the vertical position of the last character before, then deduct the: font size & 'space after' setting, then deduct the vertical position of the first character after the table and deduct its 'space before' setting.

If fail to see how the table height would be used to calculate column widths.
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi,Paul Edstein
Thank you for reply again! Really appreciated!!!
As you told, when I pasted table to WORD, I calculate the last charecter(wdVerticalPositionRelativeToPage) in table (i.e. T1), then the program changes table width, I read the last charecter(wdVerticalPositionRelativeToPage) again(i.e. T2). when T1<T2, I know some cells in table span in more than one line.
But the same problem exist as when I use bookmark, that is T2 is not changed IMMEDIATELY when the program run byitself, when T2 finnally changed, the columnwidth is too much small.
Any advise about this? Really Thanks!!!
 
Upvote 0
One would ordinarily format the range in Excel so it has the desired 'appearance' there, so it doesn't have to be changed again in Word...

Depending on how the table is formatted, neither it's first character nor its last character might define the highest or lowest character, respectively. And, even if they do, they do not tell you what the table's height is. Bookmarking the table, which is quite unnecessary, won't help tell you that, either.

Regardless, if all you're after is to minimise or optimise the table's dimensions, all you need is code like:
Code:
Sub demo()
    Dim WdObj As Object, WdDoc As Object
    Set WdObj = CreateObject("word.application")
    With WdObj
        .Visible = True
        Set WdDoc = .Documents.Add
        ThisWorkbook.Sheets(1).UsedRange.Copy
        With WdDoc
            .PasteExcelTable False, False, False
            With .Tables(1)
              .PreferredWidthType = wdPreferredWidthAuto
              .Columns.PreferredWidthType = wdPreferredWidthAuto
              .AutoFitBehavior wdAutoFitContent
            End With
        End With
    End With
End Sub
No messing around with bookmarks or character positions in sight!
 
Last edited:
Upvote 0
Paul Edstein,
Sorry for cannot get on to internet these days.
I think I'll use the code you pointed out and try it in my program.
Really appreciated for your patience and reply!!!
Thank you very much!!!
 
Upvote 0

Forum statistics

Threads
1,223,750
Messages
6,174,291
Members
452,554
Latest member
Louis1225

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top