PDA

View Full Version : Solved: set specific column width in table



TrippyTom
12-03-2008, 05:11 PM
Hi everyone,

I wanted to write a macro to let the user specify a width for a column in a PowerPoint table (not an imbedded Word table). Would this be too involved?

TrippyTom
12-03-2008, 07:59 PM
Let me rephrase.

I found this in the help:
With ActiveWindow.Selection.ShapeRange
If .HasTable = True Then
.Table.Columns(1).Width = 72
End If
End With

This will set the width of column 1 to 1 inch (72 pixels) if the selection is a table. So now all I need to know is how to get the current column number where the cursor is. I'm assuming the user has clicked in a cell before running the macro.

TrippyTom
12-03-2008, 08:48 PM
Well, I figured it out. This is what I ended up doing. Nothing fancy, but it gets the job done for what I wanted.
Sub Tbl_ColWidth()
On Error Resume Next
'Select some cells in a table first (NOT FOR USE WITH COMPLEX TABLES)
Dim oTable As Table
Dim i As Integer, J As Integer
Dim Message, Title, Default, MyValue
With ActiveWindow.Selection.ShapeRange
If .HasTable = True Then
Set oTable = ActiveWindow.Selection.ShapeRange.Table
End If
End With
Message = "How wide do you want the selected cells?" & vbCr & "(units in inches)"
Title = "InputBox Demo" ' Set title.
Default = "1" ' Set default.
MyValue = InputBox(Message, Title, Default)
'Set Column Widths of selection
With oTable
For i = 1 To .Rows.Count
For J = 1 To .Columns.Count
If .Cell(i, J).Selected Then
.Columns(J).Width = MyValue * 72
End If
Next J
Next i
End With
End Sub

Cosmo
12-04-2008, 12:42 PM
You need to do something if the selection doesn't have a table, or you'll get an error when you get to the part of the code where you use oTable

If .HasTable Then
Set oTable = ActiveWindow.Selection.ShapeRange.Table
else
' you might want to put an alert here
exit sub
End If

TrippyTom
12-04-2008, 02:10 PM
Thanks cosmo.