Please Write Vba Script
Please Write Vba Script
Here's a VBA script that will take the text from a selected text box, split it into
individual words, and then create a new text box for each word.
Important Considerations Before Running:
● Select a Text Box First: You must have a text box selected on your PowerPoint slide
before running this script. If nothing is selected or if you select a shape that isn't a text
box, the script will show an error.
● Word Definition: This script defines a "word" as any sequence of characters separated
by a space. Punctuation attached to words (e.g., "word." or "word!") will remain part of the
"word" in its text box.
● Positioning: The script will initially place the new word text boxes in a line, starting near
where the original text box was. You'll likely need to manually adjust their positions and
formatting afterward.
● Performance: For very long pieces of text with hundreds or thousands of words, this
script might take a moment to run, and the resulting slide will have many objects, which
could impact performance if you have too many.
● Undo: There is no automatic "undo" for VBA actions. It's always a good idea to save your
presentation before running new scripts, or at least run it on a duplicate slide first.
How to Use the VBA Script:
1. Open PowerPoint.
2. Open the VBA Editor:
○ Press Alt + F11 on your keyboard. This will open the Microsoft Visual Basic for
Applications window.
3. Insert a New Module:
○ In the VBA editor, in the left-hand "Project Explorer" pane, find your presentation
(e.g., "VBAProject (YourPresentationName.pptx)").
○ Right-click on your presentation name.
○ Go to Insert > Module. A new, blank module window will appear.
4. Paste the Code:
○ Copy the VBA code provided below.
○ Paste it into the new module window.
5. Run the Script:
○ Go back to your PowerPoint slide.
○ Select the text box containing the text you want to split.
○ Go back to the VBA editor (Alt + F11).
○ Click anywhere inside the pasted code (within the Sub and End Sub).
○ Press F5 to run the script, or click the green "Run Sub/UserForm" triangle icon in
the toolbar.
○ Alternatively, you can go to Developer tab on PowerPoint ribbon (if not visible,
enable it via File > Options > Customize Ribbon) and click Macros. Select
SplitTextIntoWordBoxes and click Run.
Sub SplitTextIntoWordBoxes()
Dim oSh As Shape
Dim oSlide As Slide
Dim sText As String
Dim vWords As Variant
Dim i As Long
Dim leftPos As Single
Dim topPos As Single
Dim newTextBox As Shape
Dim originalTextFrame As TextFrame2 ' For modern text boxes
(PowerPoint 2007+)
' --- 1. Check if a shape is selected ---
If ActiveWindow.Selection.ShapeRange.Count = 0 Then
MsgBox "Please select a text box on the slide before running
this script.", vbExclamation, "No Shape Selected"
Exit Sub
End If
Set oSh = ActiveWindow.Selection.ShapeRange(1) ' Get the first
selected shape
' --- 2. Check if the selected shape is a text box ---
' Using HasTextFrame for older text boxes, and Type = msoTextBox
for certainty
If Not (oSh.HasTextFrame Then
MsgBox "The selected shape does not contain text. Please
select a text box.", vbExclamation, "Not a Text Box"
Exit Sub
ElseIf oSh.TextFrame2.TextRange.Text = "" Then
MsgBox "The selected text box is empty. Please enter some
text.", vbExclamation, "Empty Text Box"
Exit Sub
End If
Set oSlide = oSh.Parent ' Get the slide containing the selected
shape
' Get the text from the selected shape
sText = oSh.TextFrame2.TextRange.Text
' Get the initial position for placing new text boxes
leftPos = oSh.Left
topPos = oSh.Top
' Split the text by spaces
vWords = Split(sText, " ")
' --- 3. Create new text boxes for each word ---
For i = LBound(vWords) To UBound(vWords)
Dim word As String
word = Trim(vWords(i)) ' Trim any extra spaces
If word <> "" Then ' Only create a box if the word is not
empty
Set newTextBox =
oSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, leftPos,
topPos, 100, 20) ' Adjust width/height as needed
With newTextBox.TextFrame2.TextRange
.Text = word
' Optional: Copy some formatting from the original
text box
.Font.Name = oSh.TextFrame2.TextRange.Font.Name
.Font.Size = oSh.TextFrame2.TextRange.Font.Size
.Font.Fill.ForeColor.RGB =
oSh.TextFrame2.TextRange.Font.Fill.ForeColor.RGB
End With
' Adjust position for the next word (simple horizontal
arrangement)
' You might need more sophisticated positioning for
complex layouts
leftPos = leftPos + newTextBox.Width + 5 ' 5 is a small
gap between words
If leftPos + newTextBox.Width > oSlide.CustomLayout.Width
- 20 Then ' Wrap to next line if close to right edge
leftPos = oSh.Left ' Reset to original starting left
position
topPos = topPos + newTextBox.Height + 5 ' Move down a
line
End If
End If
Next i
' Optional: Delete the original text box after splitting
(uncomment if desired)
' oSh.Delete
MsgBox "Text successfully split into individual word boxes!",
vbInformation, "Done"
End Sub