Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-21-2018, 01:20 PM
kilroy kilroy is offline Table column width wrong - unless macro paused!? Windows 10 Table column width wrong - unless macro paused!? Office 2016
Competent Performer
 
Join Date: Sep 2016
Location: Southern Ontario
Posts: 122
kilroy is on a distinguished road
Default

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
Reply With Quote
  #2  
Old 09-21-2018, 03:14 PM
macropod's Avatar
macropod macropod is offline Table column width wrong - unless macro paused!? Windows 7 64bit Table column width wrong - unless macro paused!? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,375
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by kilroy View Post
If you don't want to see the columns readjusting then you need to specify the column width.
Indeed, but when I wrote the code the OP hadn't said anything about the tables having more than one row; if I'd known that, I'd have written the code differently. For example:
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
Note the continued avoidance of Selection...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



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
Table column width wrong - unless macro paused!? Inserted AutoText table incorrectly formatted - unless macro paused! Smallweed Word VBA 4 01-16-2014 03:15 PM
Table column width wrong - unless macro paused!? Column Width keeps changing alsmith Word Tables 1 05-25-2013 02:09 AM
Table column width wrong - unless macro paused!? Word 2012: Change column width changes table width OpfinnarJocke Word Tables 1 09-22-2012 04:03 AM
Table column width wrong - unless macro paused!? Column width markg67 Word 2 06-07-2010 07:40 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:44 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft