View Full Version : Solved: how can I remove bracketed text from text box
dschmitt
06-21-2010, 11:23 PM
the script below replaces a carriage return with a space.
Can this script be modified so that it replaces everything/anything within two brackets including the brackets with a space?
e.g. "[2, 3, 4]" to " "
.Text = Replace(.Text, vbCr, " ")
dschmitt
06-22-2010, 09:16 PM
I attempted to solve the problem by using VBScript.RegExp (see sub below). I am getting an Object required error message for the replace statement. Does anybody know how to code this correctly.
Sub DeleteBracketsAndContents()
Dim oshp As Shape
Dim otxt As TextRange
Set RegExp = CreateObject("VBScript.RegExp")
RegExp.IgnoreCase = True
RegExp.Pattern = "[[]\number.*[]]"
'check for type of selection
'replaces all text in shape unless text is highlighted
If ActiveWindow.Selection.Type = ppSelectionNone _
Or ActiveWindow.Selection.Type = ppSelectionSlides Then Exit Sub
If ActiveWindow.Selection.Type = ppSelectionShapes Then
Set oshp = ActiveWindow.Selection.ShapeRange(1)
Set otxt = oshp.TextFrame.TextRange
End If
If ActiveWindow.Selection.Type = ppSelectionText Then
Set otxt = ActiveWindow.Selection.TextRange
If Len(otxt) < 1 Then
Set otxt = otxt.Parent.Parent.TextFrame.TextRange
End If
End If
'Replace text
With otxt
.Text = Replace(.Text, Reg.Pattern, "")
End With
Set RegExp = Nothing
End Sub
John Wilson
06-23-2010, 01:45 AM
See if this helps
Sub DeleteBracketsAndContents()
Dim oshp As Shape
Dim otxt As TextRange
Dim RegEx As Object
Dim RegEx_match As Object
Set RegExp = CreateObject("vbscript.regexp")
RegExp.Global = True
RegExp.IgnoreCase = True
'\[ square [
'(.* = anything
'? = not greedy without it would match the longest string between any two square brackets
'[abc] and [123]" would match ALL not just bracketted text
'\} closing square]
RegExp.Pattern = "\[(.*?)\]"
'check for type of selection
'replaces all text in shape unless text is highlighted
If ActiveWindow.Selection.Type = ppSelectionNone _
Or ActiveWindow.Selection.Type = ppSelectionSlides Then Exit Sub
If ActiveWindow.Selection.Type = ppSelectionShapes Then
Set oshp = ActiveWindow.Selection.ShapeRange(1)
Set otxt = oshp.TextFrame.TextRange
End If
If ActiveWindow.Selection.Type = ppSelectionText Then
Set otxt = ActiveWindow.Selection.TextRange
If Len(otxt) < 1 Then
Set otxt = otxt.Parent.Parent.TextFrame.TextRange
End If
End If
Set RegEx_match = RegExp.Execute(otxt)
If RegEx_match.Count > 0 Then
' 'Replace text
For i = 0 To RegEx_match.Count - 1
otxt.Replace RegEx_match(i), ""
Next i
End If
Set RegEx_match = Nothing
Set RegExp = Nothing
End Sub
dschmitt
06-23-2010, 05:37 PM
Yes, this is working perfect. Thank you, John.
I found that the below script works equally well. Is there a fundamental difference between your code and the one below?
In other words, where is having the For i = 0 To RegEx_match.Count - 1
loop an advantage?
With CreateObject("vbscript.regexp")
.Global = True
.Pattern = "\[(.*?)\]"
otxt.Text = .Replace(otxt.Text, "")
End With
dschmitt
06-23-2010, 06:24 PM
John, I have an additional question.
How does the Pattern Property look like if I want to remove the following?
12, 23, 17
or
5, 12-15, 27
For 12, 23, 17 I tried the below code but that doesn't work.
.Pattern = "\number\,"
dschmitt
06-24-2010, 06:17 PM
I found the solution to my question
.Pattern = "\d+\W*"
John, thanks again for your help, and thanks for not responding to my last question. It was a padagocially wise move. Only by playing around with the available characters can one learn how to use them.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.