Consulting

Results 1 to 19 of 19

Thread: Powerpoint VBA to delete text boxes based on name/contained text

  1. #1
    VBAX Regular
    Joined
    Jul 2016
    Posts
    8
    Location

    Powerpoint VBA to delete text boxes based on name/contained text

    I am a newcomer to VBA and am much better at tweaking existing code than writing it myself. That said, I am trying to create a powerpoint VBA code in a template that will essentially 'strip' a presentation of multiple types of textboxes that are all serving the same function (page numbering) but have come from various templates and so all function differently and can't all be deleted using a single code. One VBA code I am using is one to delete these placeholder text boxes via their name (ie 'Text Placeholder 1') and even after three days of research I can't figure out how to use a wildcard function so that any text box named 'Text Placeholder' and then any number is deleted, and instead have this very unwieldy code here which only deletes up to 10:

    Sub DeletePageNumbers()
        On Error Resume Next
        Dim PPSlide As Slide
        For Each PPSlide In ActivePresentation.Slides
            PPSlide.Shapes("Text Placeholder 1").Delete
            PPSlide.Shapes("Text Placeholder 2").Delete
            PPSlide.Shapes("Text Placeholder 3").Delete
            PPSlide.Shapes("Text Placeholder 4").Delete
            PPSlide.Shapes("Text Placeholder 5").Delete
            PPSlide.Shapes("Text Placeholder 6").Delete
            PPSlide.Shapes("Text Placeholder 7").Delete
            PPSlide.Shapes("Text Placeholder 8").Delete
            PPSlide.Shapes("Text Placeholder 9").Delete
            PPSlide.Shapes("Text Placeholder 10").Delete
        Next
    End Sub
    This works, but is not efficient. I have tried every variation of wildcards and ranges and have included the MatchWildcards=True coding which doesn't seem to work at all (apparently I don't know exactly where to implement it in the code). I also cannot find or tweak a working code that will locate a text box containing a particular phrase or single word and delete the entire text box, as we have templates that don't have these text placeholders at all and instead are CONTENT placeholder text boxes containing the word 'page' or 'outline page' and still need to be deleted (and I can't run a similar VBA code to the above or it will delete the other content text boxes as well). We are trying to streamline our templates from about 10 existing ones with various formatting to just a single one, so we need this 'strip' VBA code to apply to any slides being imported from older presentations with the other templates and need to make it as user friendly as possible because otherwise it confuses the people working with the slides (who are not the most computer savvy). I've attached a .pptx with examples of the kind of text boxes that need to be deleted (any regular text box on the slide master is okay and does not need to be deleted, only the placeholder text boxes regarding the page numbers or the content placeholder page number textboxes) and I hope someone will be able to help me with at least one aspect of this. I've spent hours with this, even trying to learn VBA from the ground up, and am really at a loss.
    Attached Files Attached Files
    Last edited by 0andy0; 07-19-2016 at 11:10 AM.

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    1,843
    Location
    Something like this would remove all text placeholders and number placeholders

    Sub killthem()Dim ocl As CustomLayout
    Dim oshp As Shape
    Dim osld As Slide
    Dim L As Long
    'remove from master layouts
    For Each ocl In ActivePresentation.Designs(1).SlideMaster.CustomLayouts
    For L = ocl.Shapes.Count To 1 Step -1
    If ocl.Shapes(L).Type = msoPlaceholder Then
    Select Case ocl.Shapes(L).PlaceholderFormat.Type
    Case Is = ppPlaceholderBody, ppPlaceholderSlideNumber
    ocl.Shapes(L).Delete
    End Select
    End If
    Next L
    Next ocl
    'remove from slides
    For Each osld In ActivePresentation.Slides
    For L = osld.Shapes.Count To 1 Step -1
    If osld.Shapes(L).Type = msoPlaceholder Then
    Select Case osld.Shapes(L).PlaceholderFormat.Type
    Case Is = ppPlaceholderBody, ppPlaceholderSlideNumber
    osld.Shapes(L).Delete
    End Select
    End If
    Next L
    Next osld
    End Sub
    
    Test on a copy!
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    VBAX Regular
    Joined
    Jul 2016
    Posts
    8
    Location
    This does work for deleting text placeholders, but doesn't delete any content placeholders, or regular text boxes that have been placed there manually outside of the slide master. People do all kinds of things to get their page numbers into slides and that's why we have all these bizarre 'templates' that are all set up different ways, and they all need to be stripped. Is there a way to delete everything in the lower right-hand corner? I also tried to code for that method but I was unable to figure out how to determine the 'coordinates' or position of an object on a slide, and not all text boxes would be in the exact same coordinates anyway. Thanks for this code though, it probably works a lot better than my original!

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    1,843
    Location
    If you know the probable names you can use wildcards like this

    For L = osld.Shapes.Count To 1 Step -1
    If osld.Shapes(L).Name Like = "Text*" Then If osld.Shapes(L).Delete
    Next L
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  5. #5
    VBAX Master
    Joined
    Feb 2007
    Posts
    1,843
    Location
    You can do WILDCARD SEARCH like this

    For Each osld In ActivePresentation.Slides
    For L = osld.Shapes.Count To 1 Step -1
    If osld.Shapes(L).Name Like "Text*" Then osld.Shapes(L).Delete

    Next L
    Next osld
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  6. #6
    VBAX Regular
    Joined
    Jul 2016
    Posts
    8
    Location
    Unfortunately there will be multiple content placeholders in a single slide besides the page number text box (and named with varying numbers) so that would delete the main content of the slide as well (and without actually deleting the text boxes themselves, just the content. Maybe this is an unsolveable problem based on how many ways people have stuck page numbers into multiple documents.

  7. #7
    VBAX Master
    Joined
    Feb 2007
    Posts
    1,843
    Location
    You have to be able to write a "rule" as to which to delete. If you can vba is likely to be able to use it but it isn't magic!

    If the slides have working slide number one way to find them is to create a duplicate slide. This will be identical except the number will change. Find the changes and delete. Would that work?

    Something like this:

    Sub chexSN()
    Dim osld As Slide
    Dim osldcopy As SlideRange
    Dim L As Long
    For Each osld In ActivePresentation.Slides
    Set osldcopy = osld.Duplicate
    For L = osld.Shapes.Count To 1 Step -1
    If osld.Shapes(L).HasTextFrame Then ' Added later prevents crash with non text
    If osld.Shapes(L).TextFrame.TextRange <> osldcopy(1).Shapes(L).TextFrame.TextRange Then
    osld.Shapes(L).TextFrame.DeleteText
    osld.Shapes(L).Delete
    End If
    Next L
    osldcopy.Delete
    End If
    Next osld
    End Sub
    Last edited by John Wilson; 07-21-2016 at 10:48 AM.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  8. #8
    VBAX Regular
    Joined
    Jul 2016
    Posts
    8
    Location
    Maybe I'm not explaining very clearly. On some particular slides, there will be three text boxes. One is a title, usually called Title # (ex Title 2), and two content placeholders, ex. Content Placeholder 3 which as the actual slide content and Content Placeholder 5 which has a page number. These numbers differ across every single slide, so designating the macro to delete 'Content Placeholder 5' might delete the page number on 4 or 5 slides, but not on 15 other ones, and it might delete the main content of the slide on 10 of those because those ones are called 'Content Placeholder 5' instead of the page number box being called 'Content Placeholder 5'. This is why I was wondering if there was a way to delete a shape/text box based on it's location, as all the page numbers are generally in the lower right hand corner. Deleting them via names (when they aren't specifically TEXT placeholders) is nearly impossible because the names are so varied. This is years of people poorly using PP to shoving page numbers in any way they can, and then those presentations being imported into other presentations with other layouts, and now every presentation has about 3 or 4 layouts and can't be easily unformatted just by programming 'delete Content Placeholder 5', because you have no idea what it will end up deleting.

  9. #9
    VBAX Master
    Joined
    Feb 2007
    Posts
    1,843
    Location
    Did you read my last post? It deletes shapes that have a slide number.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  10. #10
    VBAX Regular
    Joined
    Jul 2016
    Posts
    8
    Location
    That code was not there before when I read your post; did you edit it in later?

  11. #11
    VBAX Master
    Joined
    Feb 2007
    Posts
    1,843
    Location
    Yes and actually you should add a further check like this

    Sub chexSN()Dim osld As Slide
    Dim osldcopy As SlideRange
    Dim L As Long
    For Each osld In ActivePresentation.Slides
    Set osldcopy = osld.Duplicate
    For L = osld.Shapes.Count To 1 Step -1
    If osld.Shapes(L).HasTextFrame Then
    If osld.Shapes(L).TextFrame.TextRange <> osldcopy(1).Shapes(L).TextFrame.TextRange Then
    osld.Shapes(L).TextFrame.DeleteText
    osld.Shapes(L).Delete
    End If
    Next L
    osldcopy.Delete
    End If
    Next osld
    End Sub
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  12. #12
    VBAX Regular
    Joined
    Jul 2016
    Posts
    8
    Location
    As I said before I'm not a VBA expert. I can use it and generally figure out what a code is doing when I look at it, but can you explain exactly what this is doing?

  13. #13
    VBAX Master
    Joined
    Feb 2007
    Posts
    1,843
    Location
    OK

    The code duplicates each slide.
    The duplicate slides should be identical BUT if a shape has a slide number the new slide will show a different number. The code searches each shape and if it finds a difference deletes the text and then deletes the parent shape. These should be shapes with slide numbers. Finally it deletes the duplicate and moves to the next slide to check.

    Confusing but maybe the only way.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  14. #14
    VBAX Regular
    Joined
    Jul 2016
    Posts
    8
    Location
    Also these numbers on the text box don't correspond to the slide number, if that's how this is being coded. This code gives me a Next without For error as well. Maybe I should give up on this.

  15. #15
    VBAX Master
    Joined
    Feb 2007
    Posts
    1,843
    Location
    Yep I made a small error when adding the check

    Should be

    Sub chexSN()Dim osld As Slide
    Dim osldcopy As SlideRange
    Dim L As Long
    For Each osld In ActivePresentation.Slides
    Set osldcopy = osld.Duplicate
    For L = osld.Shapes.Count To 1 Step -1
    If osld.Shapes(L).HasTextFrame Then
    If osld.Shapes(L).TextFrame.TextRange <> osldcopy(1).Shapes(L).TextFrame.TextRange Then
    osld.Shapes(L).TextFrame.DeleteText
    osld.Shapes(L).Delete
    End If
    End If
    Next L
    osldcopy.Delete
    Next osld
    End Sub
    If the numbers do not correspond to the slide number then your only answer is to get a big stick and beat them around the body till they do things properly!
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  16. #16
    VBAX Regular
    Joined
    Jul 2016
    Posts
    8
    Location
    Are you talking about the numbers in the actual name of the text boxes corresponding (ie Content Placeholder 1 being on slide 1), or the numbers that are typed into the text box? Because neither of them ever correspond to the slide number, usually because 1) the slide was likely imported from another presentation and b) the page numbers are actually corresponding to a different document that is meant to be read while the presentation is being given. So if this hinges on these numbers matching, it really won't work.

  17. #17
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,600
    Location
    I'd use something like this

    If a (shape is NOT a Placeholder) AND (shape text consists of a number) then
    Delete the shape

    So a TB with [22] will be deleted, but a TB with [ABCD1234] would not

    I excluded placeholders since I'd think the master layout's slide number position would be used

    Easy enough to change

    You could also delete similar text boxes that only contain a date, i.e. the presentation date



    Option Explicit
    Sub DeleteNumberTextBoxes()
        Dim oPres As Presentation
        Dim oSlide As Slide
        Dim oShape As Shape
        
        Set oPres = ActivePresentation
        
        For Each oSlide In oPres.Slides
            For Each oShape In oSlide.Shapes
                If Not oShape.Type = msoPlaceholder Then
                    If oShape.HasTextFrame Then
                        If oShape.TextFrame.HasText Then
                            If IsNumeric(oShape.TextFrame.TextRange.Text) Then
                                oShape.Delete
                            End If
                        End If
                    End If
                End If
            Next
        Next
        
    End Sub
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  18. #18
    VBAX Master
    Joined
    Feb 2007
    Posts
    1,843
    Location
    Sometimes simpler is better. That should work as long as there no other numeric text boxes.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  19. #19
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,600
    Location
    1. Who knows what users can and will do?

    2. I had thought about a 2 step process:

    Step1 - Macro 1 - like above to mark (.Tag) shapes and shade somehow for possible eventual deletion
    Step2 - review shaded shapes, untagging any that need to by chance remain
    Step3 - Macro 2 - delete remaining tagged shapes

    3. That seems like overkill, at least for now
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •