View Full Version : Solved: delete text with VBScript.RegExp
dschmitt
06-27-2010, 07:06 PM
I was looking for a script for PowerPoint that could delete strings that I have to delete on a regular basis. I attopted this script for Word. The below script deletes brackets and the content within. 
 
To adopt this script to delete any other string only the regexp.Patter property needs to be changed.
 
The link below is to a website that lists the characters used by RegExp.Pattern:
http://www.devguru.com/technologies/...p_pattern.html (http://www.devguru.com/technologies/vbscript/quickref/regexp_pattern.html) 
 
 
Sub DeleteBracketsAndContents()
    Set regexp = CreateObject("VBScript.RegExp")
    
    regexp.Global = True
    regexp.IgnoreCase = True
    regexp.Pattern = "\ \[(.*?)\]"
    With Selection
        .Text = regexp.Replace(.Text, "")
    End With
       
End Sub
fumei
06-28-2010, 10:36 AM
Sub RemoveBracketContent()
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
   .ClearFormatting
   .MatchWildcards = True
   .Text = "\[*\]"
   Do While .Execute(Forward:=True) = True
      r.Delete
   Loop
End With
End Sub
does the same thing without using RegEx.  This removes all instance in the entire document.  If you wanted just the Selection, it would be:
Sub RemoveBracketContent()
With Selection.Find
   .ClearFormatting
   .MatchWildcards = True
   .Text = "\[*\]"
   Do While .Execute(Forward:=True) = True
      Selection.Delete
   Loop
End With
End Sub
Paul_Hossler
06-28-2010, 12:21 PM
1. You WERE looking for a PowerPoint approach, even tho you posted in the Word Forum?
 
2. I'd try the PPT forum, just in case, but the code below seems to work in 2007 PP, at least in my test cases
 
3. This might not be the most effecient, but PP does not offer the text manipulation that word does.
 
Option Explicit
Sub drv()
    Dim oSlide As Slide
    Dim oShape As Shape
    Dim oRegExp As Object
    
    Set oRegExp = CreateObject("VBScript.RegExp")
    oRegExp.Global = True
    oRegExp.IgnoreCase = True
    
    For Each oSlide In ActivePresentation.Slides
        For Each oShape In oSlide.Shapes
            With oShape.TextFrame.TextRange
                oRegExp.Pattern = "\ \[(.*?)\]"
                .Text = oRegExp.Replace(.Text, vbNullString)
            End With
        Next
    Next
End Sub
 
Paul
dschmitt
06-28-2010, 04:19 PM
thanks fumei. Good to know. One question. You mean without RegEx? 
Paul, funny, my PP script looks very similar to yours. 
I needed a PP script and asked for help in the PP forum. After that I modified the script so that I can also use it in Word. I posted it as a contribution in this forum because I thought it is very useful in Word too.
Paul_Hossler
06-28-2010, 05:10 PM
I wasn't sure how to interpet your lead-in 
 
I was looking for a script for PowerPoint that could delete strings that I have to delete on a regular basis. I attopted this script for Word. The below script deletes brackets and the content within
 
PP doesn't have the Wildcard S&R power like Gerry's Word example shows.
 
So I was thinking RegEx = Powerpoint question
 
Since there's usually only one or two ways to do things in PP, I'm not surprised we had similar code
 
Paul
dschmitt
06-28-2010, 05:22 PM
I wasn't clear enough. Sorry about that. 
One good outcome of my post is that I learned about Wildcard S&R in Word. I didn't know. Thanks.
Paul_Hossler
06-29-2010, 05:40 AM
The Wildcard S&R is a one powerful feature.
 
I've seen some very complex (and powerful) operations done in the forum using it
 
I've only used the more basic features, but they've been big time savers
 
Paul
fumei
06-29-2010, 09:26 AM
edited my post to be "withOUT".
fumei
06-29-2010, 09:27 AM
Paul:  "This might not be the most effecient"
I can not think of a more efficient.  Seems to me to as good as it can get.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.