• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Setting text postion while building tables

Michael Romano

New Member
I have a few macros that take data in my excel spread sheet and build that data into tables in a Power Point Presenation. I have this code

Code:
 iTableColumn = 1
            For iColumn = 1 To MAX_NBR_COLUMNS
           
                ' Copy the cell text
                tbl.Cell(iTableRow, iTableColumn).Shape.TextFrame.TextRange.Text = rDataRange.Cells(iRow, iColumn).Text
               
                ' Set the font size
                tbl.Cell(iTableRow, iTableColumn).Shape.TextFrame.TextRange.Font.Size = 10
               
                ' Set the font
                tbl.Cell(iTableRow, iTableColumn).Shape.TextFrame.TextRange.Font.Name = "Verdana"

to set my font size and style. I am trying to figure out how I can also have the text centered both vertially and horizontaly.
 
I think you just need to set the alignment properties, perhaps something like this?
Code:
Sub SampleCode()
Dim iColumn As Integer
For iColumn = 1 To MAX_NBR_COLUMNS
    With tbl.Cell(iTableRow, iTableColumn)
        ' Copy the cell text
        .Shape.TextFrame.TextRange.Text = rDataRange.Cells(iRow, iColumn).Text
        
        ' Set the font size
        .Shape.TextFrame.TextRange.Font.Size = 10
        
        ' Set the font
        .Shape.TextFrame.TextRange.Font.Name = "Verdana"
        
        'Set alignment
        .VerticalAlignment = xlCenter
        .HorizontalAlignment = xlCenter
    End With
Next iColumn
End Sub
 
Could you post the beginning portion of your code? I'm guessing you are using a PowerPoint object or reference then, not just Excel's VB.
 
Here is the whole code (I think). THis was written by a co-worker but he is no longer allowed to work on this project due to budgeting.....

Code:
Function AddTableAsTable(ppPres As PowerPoint.Presentation, sWorksheetName As String, iSlideNumber As Integer, lTopLocation As Long, lLeftLocation As Long, sTableStyle As String) As Integer
 
    Dim ppSlide As PowerPoint.Slide
    Dim ppShapeRange As PowerPoint.ShapeRange
    Dim oRow As PowerPoint.Row
    Dim tbl As Table
    Dim ws As Worksheet
    Dim iLastColumn As Long
    Dim iLastRow As Long
    Dim iColumn As Long
    Dim iRow As Long
    Dim iTableRow As Long
    Dim iTableColumn As Long
    Dim rDataRange As Range
    Dim dHeight As Double
   
    On Error GoTo TableNotFound
 
    ' Get the worksheet to copy the data from.
    Set ws = ThisWorkbook.Worksheets(sWorksheetName)
   
    ws.Activate
   
    ' Find the last row and column
    iLastRow = FindLastRow(ws)
    iLastColumn = FindLastColumn(ws)
   
    ' As long as we find a row, select and copy all the data.
    If (iLastRow > 0) Then
   
        ' Set the range to copy from the upper left to the lower right cells
        Set rDataRange = Range(ws.Cells(1, 1), ws.Cells(iLastRow, iLastColumn))
 
        ' Move to the desired slide.
        ppPres.Windows(1).View.GotoSlide (iSlideNumber)
 
        ' Get the slide for the table.
        Set ppSlide = ppPres.Slides(iSlideNumber)
                     
        ' Create the table and add the header data
        Set tbl = CreateBaseTable(ppSlide, rDataRange, lTopLocation, lLeftLocation, sTableStyle)
           
        ' The data starts on second row, since the first has the header info
        iTableRow = 2
       
        ' Get the height of the header row
        dHeight = tbl.Rows(1).Height
       
        ' Loop through all the rows to copy
        For iRow = 2 To iLastRow
       
            ' If the table does not have enough rows and is still within the height, then add another row.
            If (iTableRow > tbl.Rows.Count) And (dHeight < MAX_TABLE_SIZE) Then
                tbl.Rows.Add
               
            ' If the table height is at the maximum, then create a new slide.
            ElseIf (dHeight >= MAX_TABLE_SIZE) Then
           
                ' Create a new slide using the same layout.
                Set ppSlide = ppPres.Slides.AddSlide(ppSlide.SlideIndex + 1, ppSlide.CustomLayout)
               
                ' Set the title of the new slide to be the same as the initial slide plus some indicating it is the next part.
                ppSlide.Shapes.Title.TextFrame.TextRange.Text = ppPres.Slides(iSlideNumber).Shapes.Title.TextFrame.TextRange.Text + " (Continued)"
                               
                ' Move to the desired slide.
                ppPres.Windows(1).View.GotoSlide (ppSlide.SlideNumber)
               
                ' Create the new table and add the header data.
                Set tbl = CreateBaseTable(ppSlide, rDataRange, lTopLocation, lLeftLocation, sTableStyle)
               
                ' Reset the index
                iTableRow = 2
               
                ' Reset the height
                dHeight = tbl.Rows(1).Height
               
                ' Add one row for data.
                tbl.Rows.Add
            End If
           
            ' Copy the columns
            iTableColumn = 1
            For iColumn = 1 To MAX_NBR_COLUMNS
           
                ' Copy the cell text
                tbl.Cell(iTableRow, iTableColumn).Shape.TextFrame.TextRange.Text = rDataRange.Cells(iRow, iColumn).Text
               
                ' Set the font size
                tbl.Cell(iTableRow, iTableColumn).Shape.TextFrame.TextRange.Font.Size = 10
               
                ' Set the font
                tbl.Cell(iTableRow, iTableColumn).Shape.TextFrame.TextRange.Font.Name = "Verdana"
               
                'tbl.Cell(iTableRow, iTableColumn).Shape.TextEffect.Alignment = msoTextEffectAlignmentCentered
                      'Set alignment
                'tbl.Cell(iTableRow, iTableColumn).VerticalAlignment = xlCenter
                'tbl.Cell(iTableRow, iTableColumn).HorizontalAlignment = xlCenter
 
               
                iTableColumn = iTableColumn + 1
            Next iColumn
           
            ' Add the height of the newest row.
            dHeight = dHeight + tbl.Rows(iTableRow).Height
           
            iTableRow = iTableRow + 1
        Next iRow
       
        ' Return the number of slides added
        AddTableAsTable = ppSlide.SlideIndex - iSlideNumber
       
        ' Clean up
        Set ppSlide = Nothing
   
    End If
 
    Exit Function
 
Ah, that helps. I think this switch will do it:
Code:
Sub SampleCode()
Dim iColumn As Integer
For iColumn = 1 To MAX_NBR_COLUMNS
    With tbl.Cell(iTableRow, iTableColumn).Shape.TextFrame
        ' Copy the cell text
        .TextRange.Text = rDataRange.Cells(iRow, iColumn).Text
        
        ' Set the font size
        .TextRange.Font.Size = 10
        
        ' Set the font
        .TextRange.Font.Name = "Verdana"
        
        'Set alignment
        .HorizontalAnchor = msoAnchorCenter
        .VerticalAnchor = msoAnchorMiddle
    End With
Next iColumn
End Sub
 
Hi Michael ,

I created a blank presentation , and inserted a slide , then a table , and ran Luke's procedure given below :
Code:
Sub SampleCode()
    Dim iColumn As Integer
    For iColumn = 1 To 4
        With ActivePresentation.Slides(1).Shapes(3).Table.Cell(1, iColumn).Shape.TextFrame
             ' Copy the cell text
           .TextRange.Text = "This is a trial sentence just to see how the alignment of text within a table cell works"
       
             ' Set the font size
           .TextRange.Font.Size = 12
       
             ' Set the font
           .TextRange.Font.Name = "Verdana"
       
             'Set alignment
           .VerticalAnchor = msoAnchorMiddle
           .TextRange.ParagraphFormat.Alignment = ppAlignCenter
        End With
    Next iColumn
End Sub

Can you try the same and see what happens ?

Narayan
 
Hi, Michael Romano!

HorizontalAlignment and VerticalAlignment are properties of Cell.Shape.TextFrame, so making a mix from Luke M's codes, why not giving a try to this:
Code:
Sub SampleCode()
Dim iColumn As Integer
For iColumn = 1 To MAX_NBR_COLUMNS
    With tbl.Cell(iTableRow, iTableColumn).Shape.TextFrame
        ' Copy the cell text
      .TextRange.Text = rDataRange.Cells(iRow, iColumn).Text
     
        ' Set the font size
      .TextRange.Font.Size = 10
     
        ' Set the font
      .TextRange.Font.Name = "Verdana"
     
        'Set alignment
       .VerticalAlignment = xlCenter
        .HorizontalAlignment = xlCenter
    End With
Next iColumn
End Sub

Regards!
 
Back
Top