Search code examples
vbapowerpoint

Remove all Korean content in a Powerpoint


I am writing code to remove all the content in Korean in all the slides of a Powerpoint.

My code is:

Sub remove_language()

Korean = "msoLanguageIDKorean"

For Each oSlide In ActivePresentation.Slides
    Dim oShape As Shape
    For Each oShape In oSlide.Shapes
       If oShape.HasTable Then
          For r = 1 To oShape.Table.Rows.Count
              For c = 1 To oShape.Table.Columns.Count
                  If oShape.Table.Cell(r, c).Shape.TextFrame.TextRange.LanguageID = Korean Then
                       oShape.Table.Cell(r, c).Shape.TextFrame.DeleteText
                  End If
              Next
           Next
        Else
            Set gi = oShape.GroupItems
            If Not gi Is Nothing Then
                If oShape.GroupItems.Count > 0 Then
                    For i = 0 To oShape.GroupItems.Count - 1
                        If oShape.GroupItems(i).TextFrame.TextRange.LanguageID = Korean Then
                            oShape.GroupItems(i).TextFrame.DeleteText
                        End If
                    Next
                End If
            Else
                If oShape.TextFrame.TextRange.LanguageID = Korean Then
                    oShape.TextFrame.DeleteText
                End If
            End If
        End If                  
    Next
Next            
End Sub

It is a code based in one to set a language in all the objects that have text in the Powerpoint. But, I want to remove all the content that is in Korean and only leave the content that is in English. The problem is that Debugger gives an error in the line: Set gi = oShape.GroupItems

The debugger says that this member only can be accessed for a group. Also I don't know if I am missing something else.


Solution

  • Substitute this snippet for what you have between Else and End If above. Also, it's good practice ALWAYS to DIM all variables and to include Option Explicit at the beginning of each module to enforce good practice.

    Else
    ' There's no GroupItems object as such, so you can't
    ' assigned it to a variable.  However, you don't really need to.
    '    Set gi = oShape.GroupItems
    ' Instead, test to see if the shape is a group:
        If oShape.Type = msoGroup Then  ' it's a group
            If oShape.GroupItems.Count > 0 Then
                ' Collections are 1-based, not 0 based so this will error:
                'For i = 0 To oShape.GroupItems.Count - 1
                ' instead, this:
                For i = 1 To oShape.GroupItems.Count
                    If oShape.GroupItems(i).TextFrame.TextRange.LanguageID = Korean Then
                        oShape.GroupItems(i).TextFrame.DeleteText
                    End If
                Next
            End If
        Else    ' It's not a group
            If oShape.TextFrame.TextRange.LanguageID = Korean Then
                oShape.TextFrame.DeleteText
            End If
        End If  ' oShape.Type = msoGroup
    End If