How do I use VBA to add periods in tables? - vba

I have the below code which adds periods to the body of a PPT slide:
Sub TitlePeriod()
On Error Resume Next
Dim sld As Slide
Dim shp As Shape
Dim strTitle As String
For Each sld In ActivePresentation.Slides
If sld.Shapes.HasTitle = True Then 'check if there is a title
strTitle = sld.Shapes.Title.TextFrame.TextRange.Text
Else
strTitle = ""
End If
For Each shp In sld.Shapes
'add periods only if text of shape is not equal to title text.
If strTitle <> shp.TextFrame.TextRange.Text Then
shp.TextFrame.TextRange.AddPeriods
If shp.HasTable Then
shp.TextFrame.TextRange.AddPeriods
End If
End If
Next shp
Next sld
End Sub
I am trying to add bit to the code that will add periods to tables within a slide as well
If shp.HasTable Then
shp.TextFrame.TextRange.AddPeriods
When I run the code there are not errors, but there are no periods added within the table. Would love some advice or any tips on how to fix this.
Thanks in advance

First, I would like to offer some advice. When trying to figure out issues like this, it is best to try to examine the object in the locals window. This way, you can search through the properties of the object (in this case, the shape object, shp, which happens to be a Table) and figure out which properties you need to modify to achieve your desired results. No offense meant, but from your questions, it appears that you are new to VBA and found some of this code somewhere.
Also, the code was actually causing an error for me, as the Table shape did not have a textframe (although I only made a test table....perhaps yours actually had one). I added a check for the textFrame.
For your specific question, a shape object with a table, has a Table property that needs to be used to add things to the cells. The Table, in turn, has a Columns object which is a collection of columns. You need to loop through all of the columns. Each column is a collection of cells, so you need to loop through the cells. Each cell has the textframe and textrange objects you are looking for, so you need to run the .AddPeriods method on these objects.
Sub TitlePeriod()
On Error Resume Next
Dim sld As Slide
Dim shp As Shape
Dim strTitle As String
Dim myTable As Table
Dim myColumns As Columns
Dim col As Column
Dim myCell As Cell
For Each sld In ActivePresentation.Slides
If sld.Shapes.HasTitle = True Then 'check if there is a title
strTitle = sld.Shapes.Title.TextFrame.TextRange.Text
Else
strTitle = ""
End If
For Each shp In sld.Shapes
'add periods only if text of shape is not equal to title text.
If shp.TextFrame.HasText Then 'check to make sure there is text in the shape
If strTitle <> shp.TextFrame.TextRange.Text Then
shp.TextFrame.TextRange.AddPeriods
End If
End If
If shp.HasTable Then 'Check to see if shape is a table
Set myTable = shp.Table 'Get the table object of the shape
Set myColumns = myTable.Columns 'Get the columns of the table
For Each col In myColumns 'Loop through the columns
For Each myCell In col.Cells 'Loop through the cells in the column
myCell.Shape.TextFrame.TextRange.AddPeriods 'Add periods to the cell
Next myCell
Next col
End If
Next shp
Next sld
End Sub

Related

Powerpoint VBA Loop all slides, check title, if correct title, paste shapes to another presentation

Goal:
-Loop through presentation checking each slide for a certain title
-Once title is found
-Copy the shapes for the charts and footnote
-Then paste them into a separate presentation.
Notes:
-The slides in the presentations don't have titles but are located at Shapes(1)
-I receive a
run-time error '-2147024809 (80070057)': The specified value is out of
range.
-This error occurs on the line of the If statement
Sub library_update()
Dim NTppt As Presentation
Set NTppt = Application.Presentations("Z:\NTPath\NLibrary.pptx")
Dim ppt As Presentation
Set ppt = Application.Presentations("Z:\NTPath\Library.pptm")
Dim sld as slide
For Each sld In NTppt.Slides
If sld.Shapes(1).TextFrame.TextRange.Text = "Fixed Income - Yield Curves" Then
With NTppt
sld.Shapes.Range(Array(2, 3)).Copy
ppt.Slides(1).Shapes.Paste
End With
End If
Next sld
End Sub
The solution below worked. I am not sure why my code produced the original run time error but I assume it has something to do with not finding shapes(1) in some of my powerpoint slides.
To fix the problem, I searched for "Fixed Income - Yield Curves" in all shapes of all slides.
Sub library_update()
Dim NTppt As Presentation
Set NTppt = Application.Presentations("Z:\NTPath\NLibrary.pptx")
Dim ppt As Presentation
Set ppt = Application.Presentations("Z:\NTPath\Library.pptm")
Dim sld As Slide
Dim shp As Shape
For Each sld In NTppt.Slides
For Each shp In sld.Shapes
If shp.HasTextFrame Then
Set txt_range = shp.TextFrame.TextRange
'Confirm exact spelling and capitalization of the slides or an error will return
If txt_range = "Fixed Income – Yield Curves" Then
With NTppt
sld.Shapes.Range(Array(2, 3)).Copy
ppt.Slides(2).Shapes.Paste
End With
End If
End If
Next shp
Next sld
End Sub

Add period to entire PPT besides Titles using VBA

Is there a way to add periods to the whole PowerPoint presentation excluding the titles of each slide?
I currently am using the below code which puts a period after everything:
Sub AddPeriod()
Dim sld As Slide
Dim shp As Shape
For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
shp.TextFrame.TextRange.AddPeriods
Next shp
Next sld
End Sub
The easiest way I found to do this is:
Sub AddPeriod()
Dim sld As Slide
Dim shp As Shape
Dim strTitle As String
For Each sld In ActivePresentation.Slides
If sld.Shapes.HasTitle = True Then 'check if there is a title
strTitle = sld.Shapes.Title.TextFrame.TextRange.Text
Else
strTitle = ""
End If
For Each shp In sld.Shapes
'add periods only if text of shape is not equal to title text.
If strTitle <> shp.TextFrame.TextRange.Text Then
shp.TextFrame.TextRange.AddPeriods
End If
Next shp
Next sld
End Sub
This will check the text of the title vs. the text of your shape. If they are the same, it will not add the periods. Perhaps there is some sort of indicator on the shape that states whether the shape is the title, but I couldn't find it. You need to check to make sure the slide has a title, otherwise getting the string from the text of the title shape will cause an error.

VBA for capitalizing title slides

I want to use VBA to capitalize each word in the titles of all my PowerPoint slides.
So far this is the code I am using:
Sub Capitalize()
Dim sld As Slide
For Each sld In ActivePresentation.Slides
sld.Title.TextFrame.TextRange.ChangeCase ppCaseTitle
Next sld
End Sub
It's giving me an error by highlighting the "Title" and saying "Method or data member not found"
Any help would be greatly appreciated. Thanks!
The Title object is available on the Shapes object, which maps on to the placeholder title for the slide. I would also use the HasTitle property to check if the slide has a title or not.
Sub Capitalize()
Dim sld As Slide
For Each sld In ActivePresentation.Slides
If sld.Shapes.HasTitle Then
sld.Shapes.Title.TextFrame.TextRange.ChangeCase ppCaseTitle
End If
Next sld
End Sub
A Slide object doesn't have a Title property. You need to look for the Shape object that contains the title text.
Iterate the .Shapes collection and use its Name to know when you've found the one that contains your title (then you can exit the loop).
This assumes you've named the title shape "Title" or something.
Dim sld As Slide, shp As Shape
Dim found As Boolean
For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
If shp.Name = "Title" Then
found = True
shp.TextFrame.TextRange.ChangeCase ppCaseTitle
End If
If found Then Exit For
Next
If found Then Exit For
Next

Microsoft Word Macro to delete everything other than text

I'm trying to create a macro in word that deletes everything other than text.
So charts/tables/excel tables/images.
I've tried recording one and manipulating it but to no avail.
This is working for images & charts but not tables/excel tables.
Sub deleteimages()
Dim i As Integer
With ActiveDocument
For i = 1 To .InlineShapes.Count
.InlineShapes(i).ConvertToShape
Next i
Dim Shp As Shape
For Each Shp In ActiveDocument.Shapes
If Shp.Type = msoTextBox Then Shp.Delete
Next Shp
For Each Shp In ActiveDocument.Shapes
If Shp.Type = msoTable Then Shp.Delete
Next Shp
ActiveDocument.Shapes.SelectAll
Selection.Delete
End With
End Sub
For tables, use this:
Sub deletetables()
Dim i As Integer
With ActiveDocument
For i = .Tables.Count To 1 Step -1
.Tables(i).Delete
Next i
End With
End Sub
The same logic use for charts and other objects.
For further information, please see: Word Object Model Reference
By The Way: i suggest to delete objects starting from the last one, because of set of reasons. Another way is to use Do While... loop:
Do While ActiveDocument.Tables.Count>1
ActiveDocument.Tables(1).Delete
Loop
This macro deletes Charts, MS Tables, Excel copied tables & images.
Sub deleteNoise()
Dim objPic As InlineShape
For Each objPic In ActiveDocument.InlineShapes
objPic.Delete
Next objPic
Dim tbl As Table
For Each tbl In ActiveDocument.Tables
tbl.Delete
Next tbl
Dim shp As Shape
ActiveDocument.Shapes.SelectAll
Selection.Delete
End Sub

Building a macro for copying tables from Powerpoint to Excel

I'm "very" new at this. I am currently working with a ppt file containing ~120 slides, each containing a table with identical fields but different values. I want to copy these tables into an Excel file. There is no chart associated with the tables.
I don't care about the field names but just the value and I am looking for a VBA code to do automate this.
Is it even possible or is to too simple. I tried recording a PPT macro for copying the table (alt + t + m + r) but am not sure what to do after that, or how to start an Excel file to paste this macro etc.
Any help would be extremely appreciated.
I'm not all that good with Excel, but perhaps someone else who is can fill in the blanks here. This is more or less what you'd need to locate the first table on each slide in PowerPoint and do something with it. Perhaps someone who's good with Excel will fill in the missing bits.
Sub CopyTables()
Dim oSl As Slide
Dim oTbl As Table
Dim lCol As Long
Dim lRow As Long
For Each oSl In ActivePresentation.Slides
Set oTbl = GetFirstTable(oSl)
If oTbl Is Nothing Then
Exit For
End If
With oTbl
For lCol = 1 To .Columns.Count
For lRow = 1 To .Rows.Count
Debug.Print oTbl.Cell(lRow, lCol).Shape.TextFrame.TextRange.Text
Next
Next
End With
Next
End Sub
Function GetFirstTable(oSl As Slide) As Table
Dim oSh As Shape
For Each oSh In oSl.Shapes
If oSh.HasTable Then
Set GetFirstTable = oSh.Table
Exit Function
End If
Next
End Function