PPT2000: Help Example for Replace Method, is Incorrect

ID: Q226601


The information in this article applies to:


SYMPTOMS

If you implement the code example from the Replace Method topic in the Microsoft Visual Basic Help, you will find that it will only replaces one occurrence of the search string.

Below is the incorrect code sample from the Help file example:


For Each sld In Application.ActivePresentation.Slides
    For Each shp In sld.Shapes
        shp.TextFrame.TextRange.Replace FindWhat:="CompanyX", _
            Replacewhat:="CompanyY", WholeWords:=True
    Next shp
Next sld 


CAUSE

The code, given in the help article on the Replace Method in the Microsoft Visual Basic Help, only replaces the first occurrence of the search string being looked for. It does not continue searching the text frame until it can no longer find the search string.


RESOLUTION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft Support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://support.microsoft.com/support/
The following code sample correctly implements the Replace Method example for PowerPoint 2000:

Sub test()
   Dim oSld As Slide
   Dim oShp As Shape
   Dim oTxtRng As TextRange
   Dim oTmpRng As TextRange
    
   Set oSld = Application.ActivePresentation.Slides(1)
   For Each oShp In oSld.Shapes
      Set oTxtRng = oShp.TextFrame.TextRange 
      Set oTmpRng = oTxtRng.Replace(FindWhat:="like", _ 
        Replacewhat:="NOT LIKE", WholeWords:=True)
      Do While Not oTmpRng Is Nothing
         Set oTxtRng = oTxtRng.Characters(oTmpRng.Start + oTmpRng.Length, _ 
            oTxtRng.Length)
         Set oTmpRng = oTxtRng.Replace(FindWhat:="like", _
            Replacewhat:="NOT LIKE", WholeWords:=True)
      Loop
   Next oShp
End Sub 

Additional query words: PPT2K PPT2000 PPT POWERPNT 2000 PPT9 9.0 vba


Keywords          : kbdta kbdtacode OffVBA KbVBA 
Version           : WINDOWS:2000
Platform          : WINDOWS 
Issue type        : kbbug 

Last Reviewed: June 28, 1999