#1
|
|||
|
|||
Table column width wrong - unless macro paused!?
Hey all!
I am investigating a VBA issue where Word 2016 behaves differently than Word 2010. However during Investigation I found out, even Word 2010 will behave differently depending if I set a breakpoint in VBA or not. Sounds weird? It is! I dumbed down my macro to the following. A table is created with three columns. Text is written to the table. The columns width is adjusted as well. Sounds easy, right? This width setting does not take effect on 2016 but does on 2010. When stepping through the macro one command at a time, it will work on 2016 as well. Only if the original macro is run "at full speed" (also as part of a much larger macro cascade), then the width is ignored. To simulate that on 2010, I put a content-less loop after the table creation. I also put a MsgBox for your comparison in screenshot. As you can see, when the Messagebox arrives, the width has not been set. Only after clicking "ok" and another pause, then the table will magically readjust its width. I assume this readjustment for some reason does not happen anymore for VBA 2016. Because it uses the exact same commands. See screenshot before clicking OK: https://imgur.com/lJmfNMs And adjusted with after clicking: https://imgur.com/OLOeHcn Following are the VBA commands used for the test. As you can see, the width should be adjusted long before the message box appears..! Code:
Sub Test() text_a = "A text" text_b = "A longer text to make the column need more width" text_c = "More text" ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:=3, _ DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=False With Selection.Tables(1) .ApplyStyleHeadingRows = True .ApplyStyleLastRow = True .ApplyStyleFirstColumn = True .ApplyStyleLastColumn = True .AllowAutoFit = False .PreferredWidthType = wdPreferredWidthPoints .PreferredWidth = CentimetersToPoints(17.5) .LeftPadding = 0 .RightPadding = 0 End With Selection.TypeText Text:=text_a Selection.Cells.PreferredWidth = CentimetersToPoints(3.2) Selection.MoveRight Unit:=wdCell Selection.TypeText Text:=text_b Selection.Cells.PreferredWidth = CentimetersToPoints(13.4) Selection.MoveRight Unit:=wdCell Selection.TypeText Text:=text_c For i = 1 To 100 DoEvents: DoEvents: DoEvents: DoEvents: DoEvents Next i For i = 1 To 10000000: Next i MsgBox ("OK") End Sub What am I doing wrong? How can I fix this to always see the correct width immediately and not run into those problems in VBA 2016? Can you recommend any other way to create a table of 3 columns with predefined width like this? Thanks a lot in advance! |
#2
|
||||
|
||||
Far more efficient:
Code:
Sub Test2() Application.ScreenUpdating = False Dim Tbl As Table Const text_a As String = "A text" Const text_b As String = "A longer text to make the column need more width" Const text_c As String = "More text" Set Tbl = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=1, NumColumns:=3, _ DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=False) With Tbl .ApplyStyleHeadingRows = True .ApplyStyleLastRow = True .ApplyStyleFirstColumn = True .ApplyStyleLastColumn = True .AllowAutoFit = False .PreferredWidthType = wdPreferredWidthPoints .PreferredWidth = CentimetersToPoints(17.5) .LeftPadding = 0 .RightPadding = 0 With .Cell(1, 1) .PreferredWidthType = wdPreferredWidthPoints .PreferredWidth = CentimetersToPoints(3.2) .Range.Text = text_a End With With .Cell(1, 2) .PreferredWidthType = wdPreferredWidthPoints .PreferredWidth = CentimetersToPoints(13.4) .Range.Text = text_b End With .Cell(1, 3).Range.Text = text_c End With Application.ScreenUpdating = True Application.ScreenRefresh MsgBox ("OK") End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thanks for the prompt reply, macropod!
When I try your solution, I do not find any difference in effect. See screenshots for comparison: Before, After However I will try and incorporate your suggestions in my main script to see if it will make any difference there. I like how you do not operate with Selection and Move at all, but instead name the absolute coordinates of the cells. Would you have any suggestion on how to handle multiple lines though? The script I showed will be used to add multiple lines to the same table, and it will not know if there exist lines before, or how many in total. In effect I have 3 scripts: One to start the table and add headlines, one to add a new line (the first and any additional), and another one to end the table and continue with normal text. They will all be called from external source and for some reason, when all is said and done, the table columns do still not have correct width and document is saved in wrong state. |
#4
|
||||
|
||||
With the code in post #2, the table must be refreshed before you see it; there's no before/after. Do note that I modified the code slightly after posting.
As for the coding for a table with variable rows, surely you can know when creating it how many rows you'll need? If not, since your table will undoubtedly have at least two rows, I'd start off with that, then add rows as necessary. As for saving the file and not seeing a correctly formatted table, that suggests a display driver or printer driver problem or a faulty Office installation.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
I tested your code from post #2 roughly 10 hours after you posted. The results you can see in my screenshots in post #3 (which I also edited in slightly afterwards)
Note how the width is not adjusted while the msgbox shows, no matter if I wait a minute before clicking OK, and is only updated after the script ends. Does it not do so when you try running the script in your installation? This is getting even weirder (not sure if that's possible) |
#6
|
||||
|
||||
As I already said, there is no before/after; there is simply a correctly-formatted table.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
If you don't want to see the columns readjusting then you need to specify the column width. This code is likely nowhere close to as refined as it could be, I'm still a beginner.
Code:
Sub Test() Dim oDoc As Document Dim oTbl As Table text_a = "A text" text_b = "A longer text to make the column need more width" text_c = "More text" Set oDoc = ActiveDocument Set oTbl = oDoc.Tables.Add(oDoc.Range, 1, 3) With oTbl .Columns(1).SetWidth ColumnWidth:=100, _ RulerStyle:=wdAdjustFirstColumn .Columns(2).SetWidth ColumnWidth:=300, _ RulerStyle:=wdAdjustFirstColumn .Columns(3).SetWidth ColumnWidth:=100, _ RulerStyle:=wdAdjustFirstColumn .ApplyStyleHeadingRows = True .ApplyStyleLastRow = True .ApplyStyleFirstColumn = True .ApplyStyleLastColumn = True .AllowAutoFit = False Selection.WholeStory With Selection.Borders(wdBorderTop) .LineStyle = Options.DefaultBorderLineStyle End With With Selection.Borders(wdBorderLeft) .LineStyle = Options.DefaultBorderLineStyle End With With Selection.Borders(wdBorderBottom) .LineStyle = Options.DefaultBorderLineStyle End With With Selection.Borders(wdBorderRight) .LineStyle = Options.DefaultBorderLineStyle End With With Selection.Borders(wdBorderHorizontal) .LineStyle = Options.DefaultBorderLineStyle End With With Selection.Borders(wdBorderVertical) .LineStyle = Options.DefaultBorderLineStyle End With .Cell(1, 1).Range.Text = text_a .Cell(1, 2).Range.Text = text_b .Cell(1, 3).Range.Text = text_c End With With Selection.Tables(1) For i = 1 To 1 .Rows(i).Select Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter Selection.Tables(1).Rows.Alignment = wdAlignRowCenter Selection.Cells.VerticalAlignment = wdCellAlignVerticalCenter Next End With Selection.WholeStory With Selection.ParagraphFormat .SpaceBefore = 1 .SpaceBeforeAuto = False .SpaceAfter = 1 .SpaceAfterAuto = False .LineSpacingRule = wdLineSpaceSingle .LineUnitBefore = 0 .LineUnitAfter = 0 End With Selection.Collapse End Sub |
#8
|
||||
|
||||
Quote:
Code:
Sub Test3() Application.ScreenUpdating = False Dim Tbl As Table Const text_a As String = "A text" Const text_b As String = "A longer text to make the column need more width" Const text_c As String = "More text" Set Tbl = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=4, NumColumns:=3) With Tbl .AllowAutoFit = False .Rows(1).HeadingFormat = True .PreferredWidthType = wdPreferredWidthPoints .PreferredWidth = CentimetersToPoints(17.5) .Columns(1).PreferredWidth = CentimetersToPoints(3.2) .Columns(2).PreferredWidth = CentimetersToPoints(13.4) .ApplyStyleHeadingRows = True .ApplyStyleLastRow = True .ApplyStyleFirstColumn = True .ApplyStyleLastColumn = True .LeftPadding = 0 .RightPadding = 0 .Borders.Enable = True .Cell(1, 1).Range.Text = text_a .Cell(1, 2).Range.Text = text_b .Cell(1, 3).Range.Text = text_c End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
create fields with multiple lines - fix column width in table | expert4knowledge | Word | 4 | 02-14-2014 01:06 PM |
Inserted AutoText table incorrectly formatted - unless macro paused! | Smallweed | Word VBA | 4 | 01-16-2014 03:15 PM |
Column Width keeps changing | alsmith | Word Tables | 1 | 05-25-2013 02:09 AM |
Word 2012: Change column width changes table width | OpfinnarJocke | Word Tables | 1 | 09-22-2012 04:03 AM |
Column width | markg67 | Word | 2 | 06-07-2010 07:40 PM |