PDA

View Full Version : Trouble Setting Column Widths



Mavila
09-20-2016, 08:40 AM
I have an rectangle shape that I create in the footer and inside the shape I put a table with 2 rows and 3 columns. All I'm trying to do is set the columns widths, but no matter how I try to do it only the first column changes width.

I convert text inside the shape into a table as follows:
Dim shpBox as Shape
With shpBox
.TextFrame.TextRange = strFooter
.TextFrame.TextRange.ConvertToTable Separator:=wdSeparateByTabs, numrows:=2, numcolumns:=3, _
AutoFitBehavior:=wdAutoFitFixed
End With

That works fine. Then I select a cell and insert the "Page" X "of" X page numbering fields.

Then I do the following:
Selection.Tables(1).Columns(1).Width = 220
Selection.Tables(1).Columns(2).Width = 18
Selection.Tables(1).Columns(3).Width = 220

For some reason, all it those lines do is change the width of the first column (to the correct width, I might add).

Any ideas what might be going on here? I thought this was a pretty easy bit of code. Thanks.

gmaxey
09-20-2016, 11:02 AM
Starting with a rectangular shape in a footer containing the following tab delimited text

Test A Test
Test B Test

I ran this code and all three columns were set:


Sub ScratchMacfro()
'A basic Word macro coded by Greg Maxey
Dim shpBox As Shape
Set shpBox = Selection.ShapeRange(1)
shpBox.TextFrame.TextRange.ConvertToTable Separator:=wdSeparateByTabs, numrows:=2, numcolumns:=3, _
AutoFitBehavior:=wdAutoFitFixed
With Selection.Tables(1)
.Columns(1).Width = 220
.Columns(2).Width = 18
.Columns(3).Width = 220
End With
lbl_Exit:
Exit Sub

End Sub

Mavila
09-20-2016, 02:04 PM
I'm still not having any luck. Those three lines of column.width code run but they only affect the first column. Here is the actual code from my project:


If Mid(FooterStyle, 5, 1) = 1 Or Mid(FooterStyle, 5, 1) = 2 Then 'Box or Shadowed Box
Set shpBox = ActiveDocument.Sections(ActiveDocument.Sections.Count).Footers(wdHeaderFoot erFirstPage).Shapes.AddShape(msoShapeRectangle, 72, 725, 468, 36)
With shpBox
.Fill.ForeColor.RGB = RGB(255, 255, 255) 'White fill for the box
.Line.ForeColor.RGB = RGB(0, 0, 0) 'Black perimeter line
If FooterStyle = 2 Then 'Configure box's shadow
.Shadow.Style = msoShadowStyleOuterShadow
.Shadow.ForeColor.RGB = RGB(0, 0, 0)
.Shadow.OffsetX = 5
.Shadow.OffsetY = 5
.Shadow.Transparency = 0.5
End If
.TextFrame.TextRange.Font.TextColor = RGB(0, 0, 0)
.TextFrame.TextRange.Font.Name = "Times New Roman"
.TextFrame.TextRange.Font.Bold = True
.TextFrame.TextRange.Font.Size = 10
.TextFrame.TextRange.ParagraphFormat.Alignment = wdAlignParagraphLeft 'Default of the shape is center, which places table cells in center
.TextFrame.TextRange = strFooter 'Constructed String for Data Display
.TextFrame.TextRange.ConvertToTable Separator:=wdSeparateByTabs, numrows:=2, numcolumns:=3, AutoFitBehavior:=wdAutoFitFixed
If Mid(FooterStyle, 4, 1) = 0 Then 'Left Alignment
.TextFrame.TextRange.Tables(1).Cell(2, 1).Range.Select
ElseIf Mid(FooterStyle, 4, 1) = 1 Then 'Right Alignment
.TextFrame.TextRange.Tables(1).Cell(2, 2).Range.Select
End If
Selection.TypeText Text:="Page "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage, preserveformatting:=True
Selection.TypeText Text:=" of "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldSectionPages, preserveformatting:=True
'Set Column Widths
With Selection.Tables(1)
.Columns(1).Width = 220
.Columns(2).Width = 18
.Columns(3).Width = 220
'Selection.Tables(1).Columns(3).Select
End With
End With
End If

Mavila
09-20-2016, 03:08 PM
So, I just decided to see if a simple table insert and column width change would work leaving out the footer, shape, etc., and it still doesn't work!



ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).Range.Select
ActiveDocument.Tables.Add Range:=Selection.Range, numrows:=2, numcolumns:=3, AutoFitBehavior:=wdAutoFitFixed
With Selection.Tables(1)
.Columns(1).Width = 220
.Columns(2).Width = 18
.Columns(3).Width = 220
End With


Frustrating! I've done this before.

UPDATE: OK, I screwed up with the above code and had all of the columns as #1 and I have changed that. I also took off the AutoFitBehavior:=wdAutoFitFixed and it works fine. So, apparently it has something to do with the AutoFitBehavior part of the code. Not sure why but at least I'm getting somewhere.

Mavila
09-20-2016, 05:35 PM
Apparently, it doesn't like you to convert text into a table inside a shape. Not sure why that would be, but that appears to be the case.

When I create the table first (not converting text into the table), then the column.width property works fine.

I only converted text to a table because I read an article in which the author said that your code will run faster if you convert text to a table, rather than try to fill the table. I'm guessing it's a marginal difference, but I was looking for the most efficient code possible as this is repeated dozens of times in my project.

Thanks for you interest.

I'm moving on now. :hi:

gmaxey
09-20-2016, 06:44 PM
I can't explain it, but the root cause seems to be selecting the cell. If you select the first column cell with your code then the three different column widths are applied to column 1. If you select the second column cell then he three different column widths are applied to column 2.

Here is a adaptation that doesn't select the cells and it works:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim shpBox As Shape
Dim strFooter As String
Dim oTbl As Table
Dim oRng As Range
strFooter = "Test" & vbTab & " " & vbTab & "Test" & vbCr & "Test" & vbTab & " " & vbTab & "Test"
Set shpBox = ActiveDocument.Sections(ActiveDocument.Sections.Count).Footers(wdHeaderFoot erFirstPage).Shapes.AddShape(msoShapeRectangle, 72, 725, 468, 36)
With shpBox
.Fill.ForeColor.RGB = RGB(255, 255, 255) 'White fill for the box
.Line.ForeColor.RGB = RGB(0, 0, 0) 'Black perimeter line
'If FooterStyle = 2 Then 'Configure box's shadow
.Shadow.Style = msoShadowStyleOuterShadow
.Shadow.ForeColor.RGB = RGB(0, 0, 0)
.Shadow.OffsetX = 5
.Shadow.OffsetY = 5
.Shadow.Transparency = 0.5
'End If
.TextFrame.TextRange.Font.TextColor = RGB(0, 0, 0)
.TextFrame.TextRange.Font.Name = "Times New Roman"
.TextFrame.TextRange.Font.Bold = True
.TextFrame.TextRange.Font.Size = 10
.TextFrame.TextRange.ParagraphFormat.Alignment = wdAlignParagraphLeft 'Default of the shape is center, which places table cells in center
.TextFrame.TextRange = strFooter 'Constructed String for Data Display
Set oTbl = .TextFrame.TextRange.ConvertToTable(Separator:=wdSeparateByTabs, numrows:=2, numcolumns:=3, AutoFitBehavior:=wdAutoFitFixed)
'If Mid(FooterStyle, 4, 1) = 0 Then 'Left Alignment
Set oRng = oTbl.Cell(2, 1).Range
'ElseIf Mid(FooterStyle, 4, 1) = 1 Then 'Right Alignment
'Set oRng = oTbl.Cell(2, 2).Range
'End If
With oRng
.End = .End - 1
.Text = "Page "
.Collapse wdCollapseEnd
.Fields.Add oRng, wdFieldPage
.MoveEnd wdCharacter, 1
.Collapse wdCollapseEnd
.Text = " of "
.Collapse wdCollapseEnd
.Fields.Add oRng, wdFieldSectionPages
End With
'Set Column Widths
With oTbl
.Columns(1).Width = 220
.Columns(2).Width = 18
.Columns(3).Width = 220
End With
End With
lbl_Exit:
Exit Sub
End Sub