PDA

View Full Version : Select all text boxes and shapes with texts on current slide



wizarddani
12-07-2023, 12:24 PM
Hello!

Let me start by saying I'm not a programmer, I have absolutely no idea about how all this coding works, it's all sorcery to me. x)

I've been using ChatGPT just fine until now when it comes to creating simple working macros for Word, Powerpoint, etc. This simple task, however, seems impossible for ChatGPT. I've tried, literally, over 30 different codes generated by ChatGPT and none of them work correctly.

So basically:



Version of the program: Powerpoint 365 (European Spanish version if that helps)
What you want it to do: I want a simple macro that, when ran, selects all of the text boxes and any other type of shape or object that contains text within, and have them all selected simultaneously. This is because I need to copy/paste them in another Powerpoint file, maintaining the same position, font size, format, etc.
Cell references, bookmark names, column letters, row numbers, worksheets, styles, whatever pertains to the information at hand: N/A
Error messages if any: Oh dear Lord, all of them. The most common one can be loosely translated as "-2147467263 (80004001): the specified collection index is out of limits".
If not the entire code, then at least some of it:

This one generates the aforementioned error:


Sub SeleccionarCuadrosDeTexto()
Dim slide As slide
Dim shape As shape
Dim selectedShapes() As shape
Dim i As Integer

' Obtiene la referencia a la diapositiva actual
Set slide = ActiveWindow.View.slide

' Inicializa el contador de formas seleccionadas
i = 0

' Recorre todos los objetos en la diapositiva
For Each shape In slide.Shapes
' Verifica si el objeto es un cuadro de texto
If shape.Type = msoTextBox Then
' Aumenta el tamaņo de la matriz y agrega la forma a la matriz
ReDim Preserve selectedShapes(1 To i + 1)
Set selectedShapes(i + 1) = shape
i = i + 1
End If
Next shape

' Deselecciona cualquier objeto seleccionado actualmente
slide.Shapes.Range(Array()).Select

' Selecciona todas las formas de la matriz
For i = LBound(selectedShapes) To UBound(selectedShapes)
selectedShapes(i).Select (msoTrue)
Next i
End Sub


Now the following does work... partially, cause it only selects ONE textbox, not all of them, and does not select other shapes or objects that contain text:


Sub SeleccionarCuadrosDeTexto()
Dim slide As slide
Dim shape As shape

' Obtiene la referencia a la diapositiva actual
Set slide = ActiveWindow.View.slide

' Deselecciona cualquier objeto seleccionado actualmente
On Error Resume Next
slide.Shapes.Range(Array()).Select
On Error GoTo 0

' Recorre todos los objetos en la diapositiva
For Each shape In slide.Shapes
' Verifica si el objeto es un cuadro de texto
If shape.HasTextFrame Then
' Selecciona el cuadro de texto
shape.Select (msoTrue)
End If
Next shape
End Sub

I cannot fathom why such a simple task is so complicated to automate on Powerpoint.

Any help would be very much appreciated!

John Wilson
12-08-2023, 09:22 AM
In this line

shape.Select (msoTrue)

msoTrue means DEselect any previously selected shapes

Change that line to

Sub SeleccionarCuadrosDeTexto() Dim slide As slide
Dim shape As shape

' Obtiene la referencia a la diapositiva actual
Set slide = ActiveWindow.View.slide



' Recorre todos los objetos en la diapositiva
For Each shape In slide.Shapes
' Verifica si el objeto es un cuadro de texto
If shape.HasTextFrame Then
' Selecciona el cuadro de texto
shape.Select (msoFalse)
End If
Next shape
End Sub

Aussiebear
12-08-2023, 04:21 PM
Welcome to VBAX wizarddani. If there's an issue with ChatGPT it is more often than not, that you need to ask very specific questions, to receive a correct answer. John has (as usual) found the issue for you.