# power point VBA , tables



## ziad alsayed (Jan 22, 2013)

dear all
i have a power presentation with seven slides, each slide has a title and table, i need to select each table in each slide and put its measurments (width and Height), of course the tables dont have the same measurments
hope you can assist me with a code that select each table in each slide and i will add the measurments.
thanks in advance.


----------



## ziad alsayed (Jan 23, 2013)

dear all

any help please.


----------



## Worf (Jan 26, 2013)

Hello
I'm not sure if I understood exactly what you want to do, where are the measurements supposed to go?
Anyway, here's some code:


```
' this is a PowerPoint macro


Sub TableData()
Dim tb As Table, shp As Shape, tg$, i%


For i = 1 To ActivePresentation.Slides.Count
    tg = ""
    For Each shp In ActivePresentation.Slides(i).Shapes
        If shp.HasTable Then
            tg = shp.Name      ' finds first table
            Exit For
        End If
    Next
    Select Case tg
        Case Is = ""
            
            MsgBox "Slide " & i & " has no tables", vbCritical
            
        Case Else       ' show table info
            
            MsgBox "Width : " & shp.Width & vbNewLine & "Height : " & shp.Height & vbNewLine & _
            "Number of rows : " & shp.Table.Rows.Count & vbNewLine & "Number of columns : " & _
            shp.Table.Columns.Count, vbInformation, "Table at Slide " & i & " = " & tg
            
    End Select
Next
            
End Sub
```


----------



## ziad alsayed (Jan 26, 2013)

Dear Worf 

thanks for your relpy. this is not what it need.i normaly run my  macro from excel to get my power point presentation.now in my power presentation i have seven slides, and each slide has one table ( tables dont have the same number of rows and columns), but all the tables in presentation has the same height and the same width (Width is 710 and height is 476).
for exampe the table in first slide has (Width is 710 and height is 476) but is should be (Width is 595 and height is 375), the second should have different width and height.

so i need a code that will select the first table and make the width 595 and 476 , then select the second table and make the width 400 and height 250 for example and  so on.

i hope i am explain well.
note : if possible i can send you file ( before and after ).

thanks in advance.


----------



## Worf (Jan 26, 2013)

> note : if possible i can send you file ( before and after ).



This forum does not support annexes, but you can upload files to any hosting site and paste the link here.

Concerning your other thread with the merged cells question, I'll answer soon...


----------



## Worf (Jan 29, 2013)

Hello
See if this does it.
Now I´ll work on the merged cells thread...


```
Option Explicit
Option Base 1
' this code goes at a PowerPoint module
Sub SizeTables()
Dim TabWid, TabHei, shp As Shape, i%
TabWid = Array(400, 300, 200)   ' desired table widths
TabHei = Array(350, 250, 150)   ' desired table heights
If UBound(TabWid) <> UBound(TabHei) Then Exit Sub
For i = 1 To UBound(TabWid)     ' this example works with 3 slides
    For Each shp In ActivePresentation.Slides(i).Shapes
    
        If shp.HasTable Then Exit For   ' finds first table
        
    Next
    shp.Width = TabWid(i)
    shp.Height = TabHei(i)
    
Next
End Sub
```


----------



## ziad alsayed (Jan 29, 2013)

Dear Worf

sorry for the delay i was not feeling good .

this is the idea i need , but instead of looping through the slides and select tables, i want to select slide by slide and then the table in the slide and put the parameters.

so if i have 3 slides as per your example i will have to add 3 times the parameters ( of course the parameters ar not the same).

so the code will be something like that
select Slide (1), table, parameters
select slide (2), table, parameters
select slide(3), table, parameters


----------



## Worf (Jan 29, 2013)

Hello Ziad
My code already does what you described. I added message boxes, if you run the example it will become clearer.


```
Option Explicit
Option Base 1
' this code goes at a PowerPoint module
Sub SizeTables()
Dim TabWid, TabHei, shp As Shape, i%
TabWid = Array(400, 300, 200)   ' desired table widths
TabHei = Array(350, 250, 150)   ' desired table heights
If UBound(TabWid) <> UBound(TabHei) Then Exit Sub
For i = 1 To UBound(TabWid)     ' this example works with 3 slides
    MsgBox "Working with slide #" & i, 64
    For Each shp In ActivePresentation.Slides(i).Shapes
    
        If shp.HasTable Then
            MsgBox "Found table at slide #" & i, 64
            Exit For   ' finds first table
        End If
        
    Next
    shp.Width = TabWid(i)
    shp.Height = TabHei(i)
    MsgBox "Table resized at slide #" & i, vbInformation
    
Next
End Sub
```


----------



## ziad alsayed (Jan 29, 2013)

Dear Worf

sorry but this is not what i need.

did you check the link i sent to you?

anyway thanks for your time and effort you put to in solving this issue.


please can you send me a code that will collect the first slide and the first table and make the Width 595 and height is 375.

i willl replicate the code by changing the slide number  , table number , height and width.


----------



## ziad alsayed (Jan 29, 2013)

dear Worf
please ignore my previous email, i just understood the code 
this is working perfectly.

i just need now to center the table in the middle of the slide.


a very big thanks for you  .


----------



## ziad alsayed (Jan 22, 2013)

dear all
i have a power presentation with seven slides, each slide has a title and table, i need to select each table in each slide and put its measurments (width and Height), of course the tables dont have the same measurments
hope you can assist me with a code that select each table in each slide and i will add the measurments.
thanks in advance.


----------



## Worf (Jan 30, 2013)

> a very big thanks for you



You are welcome. This new version will center tables:


```
Option Explicit
Option Base 1
' this code goes at a PowerPoint module


Sub SizeTables()
Dim TabWid, TabHei, shp As Shape, i%, pres As Presentation


TabWid = Array(400, 300, 200)   ' desired table widths
TabHei = Array(350, 250, 150)   ' desired table heights
Set pres = ActivePresentation
If UBound(TabWid) <> UBound(TabHei) Then Exit Sub


For i = 1 To UBound(TabWid)     ' this example works with 3 slides


    For Each shp In pres.Slides(i).Shapes
    
        If shp.HasTable Then Exit For   ' finds first table
        
    Next
    With shp
        .Width = TabWid(i)
        .Height = TabHei(i)
        .Top = (pres.PageSetup.SlideHeight - .Height) / 2
        .Left = (pres.PageSetup.SlideWidth - .Width) / 2
    End With
Next
End Sub
```


----------



## ziad alsayed (Jan 30, 2013)

Dear Worf

this Working perfectly 

thanks .


----------



## Worf (Feb 6, 2013)

You are welcome and thanks for the feedback.


----------

