Re: Using VBA to replace text



In article <3be051l6t3h1viufptivns16uof6mu6h6n@xxxxxxx>, Jeff Jones wrote:
> I'm having an opportuniy meeting an objective. I have a character
> string that I need to replace in many presesnations. I, frankly,
> don't want to do this manually. It would be easy if PowerPoint
> actually allowed me to record the key strokes to do a "replace all"
> but it doesn't. I've created the following subroutine. The problem
> is complicated by the fact that the text is on the Slidemaster. The
> code below selects the first slide. The code doesn't get a hit on the
> search string because it's on the slidemaster.
>
> I can activate the slidemaster by including ActiveWindow.ViewType =
> ppViewTitleMaster but haven't been able to figure out how to set the
> oSld variable to the master. I get a type mismatch. Can anyone help
> me to set the slide variable so I can spin through the shapes until
> I've gotten to the one with the text I want to change. Any
> alternative would do. Once I get this code working I can wrap in the
> code to process all the slides in a selected folder.
>

You can get around this by using:

Dim oSld as Object ' instead of Slide

You could then:

' Do the slides
For each oSld in ActivePresentation.Slides
call FixUp(oSld)
Next ' slide

' Do the Slide and Title masters
Set oSld = ActivePresentation.SlideMaster
call Fixup(oSld)

If ActivePresentation.HasTitleMaster Then
Set oSld = ActivePresentation.TitleMaster
call FixUp(oSld)
End if

Sub FixUp (oSld as Object)
' here's where you pop in the code you're already using for the
' search/replace
Dim oSh as Shape
For each oSh in oSld.Shapes
' do yer stuff
Next
End Sub
As David's mentioned, it gets a bit more complex if you have multiple masters,
but this is a start.



> Sub Change_Text()
>
> Dim oSld As Slide
> Dim oShp As Shape
> Dim oTxtRng As TextRange
> Dim oTmpRng As TextRange
>
> Set oSld = Application.ActivePresentation.Slides(1)
> On Error Resume Next
>
> For Each oShp In oSld.Shapes
> Set oTxtRng = oShp.TextFrame.TextRange
> Set oTmpRng = oTxtRng.Replace(FindWhat:="original text.", _
> Replacewhat:="replacement text", WholeWords:=True)
> Do While Not oTmpRng Is Nothing
> Set oTxtRng = oTxtRng.Characters(oTmpRng.Start +
> oTmpRng.Length, _
> oTxtRng.Length)
> Set oTmpRng = oTxtRng.Replace(FindWhat:="original text", _
> Replacewhat:="replacement text", WholeWords:=True)
> Loop
> Next oShp
>
> End Sub
>
> I'd sure appreciate any suggestions that anyone may have.
>
> Thank you,
> Jeff
>

-----------------------------------------
Steve Rindsberg, PPT MVP
PPT FAQ: www.pptfaq.com
PPTools: www.pptools.com
================================================


.



Relevant Pages

  • Re: title text auto resize issue
    ... Dim osld as Slide ... Custom vba coding and PPT Makeovers ...
    (microsoft.public.powerpoint)
  • Re: run-time error in powerpoint
    ... >> different shapes on the same slide. ... Then when the macro is run, ... Dim oSlides As Slides ... Dim oSld As Slide ...
    (microsoft.public.powerpoint)
  • Re: run-time error in powerpoint
    ... but PowerPoint will sometimes create them just the same. ... Sometimes copy/pasting the shape or slide sorts things out, ... > Dim oSlides As Slides ... > Dim oSld As Slide ...
    (microsoft.public.powerpoint)
  • RE: title text auto resize issue
    ... Dim osld As Slide ... Dim oshp As Shape ... For Each osld In ActivePresentation.Slides ...
    (microsoft.public.powerpoint)
  • Re: Extracting data from Powerpoint datasheet/MSgraph to Excel
    ... Dim s As Shape 'gr As Graph.Chart ... Dim sl As Slide ... You need to automate Excel to open your ... which chart and or slide the data is extracted from. ...
    (microsoft.public.powerpoint)

Loading