0

I am writing Find/Replace in VBA Excel. I have picked this code from google. When I run this code by pasting on PPT as a Macro, it runs fine but it is not running on Excel(When I Paste this code on Excel as MAcro) VBA for PPT. Actually I have an application which is made on VBA Excel. I open a PPT file through my application on which find and replace action is performed.

Dim FindWhat As String
Dim ReplaceWith As String
Dim oShp As PowerPoint.Shape
Dim oRng As TextRange
Dim oPres As Presentation
Dim oSld As Slide
Dim pptSlide As PowerPoint.Slide

public sub ReplaceInPPT()
FindWhat = searchtext
ReplaceWith = valuetext
    For Each oPres In Application.Presentations '<- Throws "Object doesn't support this property or method"
        For Each pptSlide In oPres.Slides
                On Error Resume Next

                For Each oShp In oSld.Shapes
                    Call ReplaceTextPPT(oShp, FindWhat, ReplaceWith)
                Next oShp
        Next pptSlide
    Next oPres
End Sub


Public Sub ReplaceTextPPT(oShp As Object, FindString As String, ReplaceString As String)
On Error Resume Next
Select Case oShp.Type

Case 19
        'msoTable
    For iRows = 1 To oShp.Table.Rows.count
        For icol = 1 To _
        oShp.Table.Rows(iRows).Cells.count
            Set oTxtRng = oShp.Table.Rows(iRows).Cells(icol).Shape.TextFrame.TextRange
            Set oTmpRng = oTxtRng.Replace(FindWhat:=FindString, _
                         Replacewhat:=ReplaceString, WholeWords:=True)
Do While Not oTmpRng Is Nothing
Set oTmpRng = oTxtRng.Replace(FindWhat:=FindString, _
              Replacewhat:=ReplaceString, After:=oTmpRng.Start + oTmpRng.Length, WholeWords:=True)
Loop
        Next
    Next
Case msoGroup 'Groups may contain shapes with text, so look within it
    For i = 1 To oShp.GroupItems.count
        Call ReplaceText(oShp.GroupItems(i), FindString, ReplaceString)
    Next i
Case 21 ' msoDiagram
    For i = 1 To oShp.Diagram.Nodes.count
        Call ReplaceText(oShp.Diagram.Nodes(i).TextShape, FindString, ReplaceString)

        Next i
Case Else
    If oShp.HasTextFrame Then
        If oShp.TextFrame.HasText Then
            Set oTxtRng = oShp.TextFrame.TextRange
        Set oTmpRng = oTxtRng.Replace(FindWhat:=FindString, _ 
                     Replacewhat:=ReplaceString, WholeWords:=True)
            Do While Not oTmpRng Is Nothing
                Set oTmpRng = oTxtRng.Replace(FindWhat:=FindString, _
                              Replacewhat:=ReplaceString, _
                        After:=(oTmpRng.Start - 1) + oTmpRng.Length, WholeWords:=True)
            Loop
       End If
    End If
End Select
End Sub
Community
  • 1
  • 1
Pallav Raj
  • 1,684
  • 3
  • 17
  • 29
  • what is your actual problem? Try reading your own question as if you were me, would you understand it? –  May 02 '14 at 08:38
  • `For Each oPres In Application.Presentations` - _Application_ refers to _excel_ application – Dmitry Pavliv May 02 '14 at 08:42
  • 'For Each oPres In Application.Presentations' this line gives Runtime error '438' Object doesn't support this property or method. I am not getting why it is giving such an error. – Pallav Raj May 02 '14 at 08:48
  • @me how.. I edited my question. Am I that much clear to make you understand? – Pallav Raj May 02 '14 at 08:50
  • @simoco It refers to Application. – Pallav Raj May 02 '14 at 08:53
  • `When I Paste this code on Excel` - in that case `Application = Excel.Application` but not `PowerPoint.Application` – Dmitry Pavliv May 02 '14 at 09:00
  • still a dump of code, no general description of the problem, no description on what macro is doing. Useless questions for any future reader –  May 02 '14 at 09:09
  • @simoco I am afraid to say, I did not understand, Can you please make me understand more clearly? – Pallav Raj May 02 '14 at 09:10
  • @mehow I try to describe it more clearly. – Pallav Raj May 02 '14 at 09:10
  • @mehow, I have an application, which takes 2 files as input. File1 is excel file which consists of some words which is to be find in target file is in Acell and Bcell consists of some words which will replace the existing word if found. File2 is a target file on which find and replace will perform. So here is a situation, I wrote a code for find and replace for PPT file(which is target file). But it is giving error as I mentioned above. So please help me if you can. And I hope this time I am clear enough. :) – Pallav Raj May 02 '14 at 09:16
  • I Answered the Question below. – Pallav Raj May 02 '14 at 09:44

2 Answers2

0

This code only works in PowerPoint, since this code:

For Each oPres In Application.Presentations '<- Throws "Object doesn't support this property or method"
    For Each pptSlide In oPres.Slides
            On Error Resume Next

            For Each oShp In oSld.Shapes
                Call ReplaceTextPPT(oShp, FindWhat, ReplaceWith)
            Next oShp
    Next pptSlide
Next oPres

loops through all presentations and slides of the current powerpoint-application. Excel doesn't have presentations nor slides, so this won't work.

Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
  • Yes, That is what I am saying. Thank you very much. I solved my problem right now. And I am posting it. :-) – Pallav Raj May 02 '14 at 09:38
0

I removed that line of code because that was only for PowerPoint as @Manu said. Now, this is working fine for me.

 For Each pptSlide In pptPres.Slides
     For Each oShp In pptSlide.Shapes
         Call ReplaceTextPPT(oShp, FindWhat, ReplaceWith)
     Next oShp
Next pptSlide

Thank you @simoco, @mehow and @Manu for you time and effort for helping me :-) .

Pallav Raj
  • 1,684
  • 3
  • 17
  • 29
  • 1
    Read this please: [Why should I use On Error Resume Next judiciously?](http://stackoverflow.com/questions/21673529/if-not-function-proceeds-when-value-is-the-one-specified/21673617#21673617) – Dmitry Pavliv May 02 '14 at 10:05