How to change font size for a table

  • Thread starter Thread starter Fred
  • Start date Start date
F

Fred

Hi everybody,

I counldn't find an answer to my question: how to change the font
attributes (e.g. size) for a whole table in an original powerpoint
table.

Once I have created the table through
ActiveWindow.Selection.SlideRange.Shapes.AddTable(4, 4).Select

I store the object ID (e.g. "Group 3020")
ReDim Preserve tblName(1 To 1)
tblName(1) = ActiveWindow.Selection.ShapeRange.Name
Set oShape = ActiveWindow.Selection.SlideRange.Shapes(tblName(1))

But when I try to address the objects' font attribute through
ActiveWindow.Selection.SlideRange.Shapes(oShape.Name).Select
With ActiveWindow.Selection.ShapeRange.TextFrame.TextRange
.Font.Name = "Courier"
.Font.Size = 12
.Font.Bold = msoTrue
End With
It gives the following error message: "TextFrame(unknown member): This
type of shape cannot have a textrange"

Since I can (on an slide) simply select a powerpoint table and change
the font attributes for the whole table at once, I wonder how to perfom
this in vba (should be possible - shouldn't it?)

Best regards to good ideas

Fred
 
Fred,

Try the following:

Sub SetTableFont()
Dim oShp As Shape
Dim oTbl As Table
Dim I As Long
Dim J As Long

Set oShp = ActiveWindow.Selection.ShapeRange(1)
Set oTbl = oShp.Table

For I = 1 To oTbl.Columns.Count
For J = 1 To oTbl.Rows.Count
oTbl.Cell(I, J).Shape.TextFrame.TextRange.Font.Name = "Verdana"
Next
Next
End Sub


--
Regards,
Shyam Pillai

Animation Carbon
http://www.AnimationCarbon.com
 
Hi everybody,

I counldn't find an answer to my question: how to change the font
attributes (e.g. size) for a whole table in an original powerpoint
table.

To begin with, you can shortcut this and speed up your code a bit thus:
Once I have created the table through

' ActiveWindow.Selection.SlideRange.Shapes.AddTable(4, 4).Select
Set oShape = ActiveWindow.Selection.SlideRange.Shapes.AddTable(4, 4)

then ...


' ActiveWindow.Selection.SlideRange.Shapes(oShape.Name).Select
' never select anything if you can avoid it

With oShape.Table

then have a look here to see how to iterate through the cells in the table:

Working with PowerPoint tables
http://www.pptfaq.com/FAQ00790.htm
 
Hi guys,

thanks for the input. Since no one came up with a faster method than
scrolling through each and every cell, I used Shyam's technique to set
the font. However, I've encountered a mysterious thing anyways: the
code does not affect the size attribute. Even when I address only the
size with

Sub SetTableFont()
Dim oShp As Shape
Dim oTbl As Table
Dim I As Long
Dim J As Long


Set oShp = ActiveWindow.Selection.ShapeRange(1)
Set oTbl = oShp.Table


For I = 1 To oTbl.Columns.Count
For J = 1 To oTbl.Rows.Count
oTbl.Cell(I, J).Shape.TextFrame.TextRange.Font.Size = 12
Next
Next
End Sub

... then it leaves the table with font size 10, which is really akward.
Would anyone have an idea on this one?

Thanks in advance.
Fred
 
Hi Fred.

I just copy/pasted your code below into the VBA editor in a new presentation,
added a table to a slide and ran the code while the table was selected. Works
fine here (PPT 2003, but I've used essentially the same code in older PPT
versions back to 2000). So ... what's different?

What version of PPT do you have, and have you made sure to select the table
before running the code?.
 
Hi Steve,

thanks for you feedback. However, it for a reason doesn't do what it's
supposed to do. (I work on ppt 2003). I have selected the shape (I
couldn't find a .select for the table). Still doesn't work. Still does
only change the font.type.
By the way: you have mentioned to avoid selecting things when possible.
Why is that?

Best regards
Fred
 
thanks for you feedback. However, it for a reason doesn't do what it's
supposed to do. (I work on ppt 2003). I have selected the shape (I
couldn't find a .select for the table).

Manually or via code? Try manually to begin with. Click anywhere in the table
then run Shyam's code.
Still doesn't work. Still does
only change the font.type.

Try setting a break point and watching what happens as it runs. Does it skip any
code you think it should be executing?
By the way: you have mentioned to avoid selecting things when possible.
Why is that?

Code that relies on selection:

- runs considerably slower

- makes the screen flash as you do things

- can't run against any but the currently visible slide (whereas code that
doesn't can process a whole file full of slides/shapes w/o having to change the
view at all)

- can't run unless PPT is visible (sometimes you want to hide PPT behind the
scenes or leave PPT visible but open another presentation invisibly)

- probably has other drawbacks that don't come to mind right now ;-)
 
Steve,

thanks for your answer on the selection.
Thanks for your input on the font.size. I somehow tried the
..insertafter.font.size . This works, but only if I insert real text
(e.g. blank or a letter; not with ""). What is wrong in my setting?

Thanks
Fred
 
Steve,

thanks for your answer on the selection.
Thanks for your input on the font.size. I somehow tried the
..insertafter.font.size . This works, but only if I insert real text
(e.g. blank or a letter; not with ""). What is wrong in my setting?

Ah. I was only looking at the results with tables that had text in them.
PowerPoint has some peculiar ways with text objects.
Basically you can set some properties any way you like but unless there's text to
apply it to, PPT ignores you.

So you'd need to modify the thing something like this:
Warning: AIR CODE!

For I = 1 To oTbl.Columns.Count
For J = 1 To oTbl.Rows.Count
with oTbl.Cell(I,J).Shape
' tack on some bogus text
.TextFrame.TextRange.Text = .TextFrame.TextRange.Text & "!#@"
oTbl.Cell(I, J).Shape.TextFrame.TextRange.Font.Size = 12
' remove the bogus text
.TextFrame.TextRange.Text = Replace(.TextFrame.TextRange.Text,"!@#","")
End With
Next
Next
 
Steve,
thanks, I knew ppt has some odities in it. But I didn't think I need to
get around it.

Regards,
Fred
 
Back
Top