• Find/Replace in PPT

    Author
    Topic
    #356223

    I’m trying to write a macro in powerpt to find and replace a sequential code in a text box and replace it with a cell from excel. I can’t find the vba code to do a find/replace in ppt.

    dim i as integer
    dim findthis as string

    for i = 1 to 10
    findthis = “<>”
    next i

    Can someone tell me the code for find and replacing in ppt? thank you

    Viewing 0 reply threads
    Author
    Replies
    • #526787

      You should be able to find some useful information by looking for Find in the Object Browser (F2 when you’re in the VB Editor) – set Look In to PowerPoint, and then do a Search on “Find”.

      What comes up indicates that in PPT, Find is a child of the TextRange object.

      The following example comes from the (PPT 2000) VBA help for the Find Method:

      [indent]


      Find Method Example

      This example finds every occurrence of “CompanyX” in the active presentation and formats it as bold.

      For Each sld In Application.ActivePresentation.Slides
          For Each shp In sld.Shapes
              If shp.HasTextFrame Then
                  Set txtRng = shp.TextFrame.TextRange
                  Set foundText = txtRng.Find(FindWhat:="CompanyX")
                  Do While Not (foundText Is Nothing)
                      With foundText
                          .Font.Bold = True
                          Set foundText = _
                              txtRng.Find(FindWhat:="CompanyX", _
                              After:=.Start + .Length - 1)
                      End With
                  Loop
              End If
          Next
      Next

      [/indent]

      • #526946

        Thanks for the code. I found a way to name my text range in ppt. Now I want to look through my active presentation and find the text range i’ve named txtRng1. Once I select it I want to search for <> and change it to something contained in a cell in Excel.

        How do i select a text range called txtRng1 in ppt? Thanks for the help.

        • #527002

          Hi again,

          It looks like my quick look at the PPT object browser didn’t go far enough, in taking in only the Find method – it turns out that the Replace method should get closer to what you need.

          Try something along the lines of:

          txtRng1.Replace FindWhat:=”<>, ReplaceWhat:=strXLTxt

          (You can more details from the PPT VBA help for the Replace method.)

          Using this method, there shouldn’t be any need to select the TextRange.

          Gary

          • #528840

            Is there a method to reference a graphic in Find/Replace? See this question here

            • #528974

              Hi Catherine,

              I haven’t worked with Find in PPT before so don’t know if there are any undocumented tricks for doing this, but the Find method itself requires a TextRange object.

              Based on that, there doesn’t seem to be any way to find graphic objects themselves using the Find method. If there is some unique property or identifier about these graphics, then it might be able to get hold of them some other way.

              Gary

            • #530680

              This code works great from an excel file that i have open. I run it from ppt.

              Sub find_replace()
              Dim sld As Slide
              Dim shp As Shape
              Dim i As Integer
              Dim txtRng As TextRange
              Dim foundText As TextRange
              Dim replaceText As TextRange
              Dim code As String
              Dim pptApp As PowerPoint.Application
              Dim xlApp As Excel.Application
              Dim xlBook As Object
              Dim xlSheet As Excel.Worksheet
              Dim myArray(1 To 359)
              Dim intCell As Integer
              Dim market As String

              intCell = 0
              i = 0

              Set pptApp = GetObject(, “PowerPoint.Application”)

              Set xlApp = GetObject(, “Excel.Application”)
              xlApp.Visible = True
              Set xlBook = GetObject(, “Excel.Application”)
              xlBook.Windows(1).Visible = True
              xlBook.Worksheets(1).Select

              ‘ActiveWorkbook.Worksheets(“Text”).Select

              ‘ fill array with first row in Excel

              For i = 1 To 359
              code = “<>”
              myArray(i) = xlBook.Cells(i, 1)

              For Each sld In Application.ActivePresentation.Slides
              For Each shp In sld.Shapes
              If shp.HasTextFrame Then
              Set txtRng = shp.TextFrame.TextRange
              Set foundText = txtRng.Find(FindWhat:=code)
              Set replaceText = txtRng.Replace(FindWhat:=code, ReplaceWhat:=myArray(i))
              End If
              Next shp
              Next sld
              Next i
              ‘Next intCell

              MsgBox (“FindReplace is Finished”)
              market = InputBox(“filename = “)
              pptApp.ActivePresentation.SaveAs (“P:market & “.ppt”)

              pptApp.ActivePresentation.Close

              End Sub

              This save my ppt file. I don’t want to close the excel file that I have open. I want to open, however, a different one and run a macro in it. I then want to close it. Then I want to go to the first excel file (that stayed open) and run a macro in it. I want to keep it open

              Now I want to go to a ppt file and open it. I want to enable macros, yes, and update links, yes. How to i do that in powerpoint. Thank you for the help.

    Viewing 0 reply threads
    Reply To: Find/Replace in PPT

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: