Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 03-02-2018, 11:20 PM
Peterson Peterson is offline Loop through all shapes in all stories not working Windows 7 64bit Loop through all shapes in all stories not working Office 2016
Competent Performer
Loop through all shapes in all stories not working
 
Join Date: Jan 2017
Posts: 143
Peterson is on a distinguished road
Default Loop through all shapes in all stories not working

I'm trying to cobble together a macro that finds all text boxes that contain the word "draft" and deletes them. Using a loop structure by macropod (many thanks!), I've gotten to the point where I can get through all stories; however, the macro isn't looping through all shapes in each story -- it addresses just one before moving to the next story. What am I doing wrong?

Code:
Sub DraftStampDelete0301()

' This macro removes all text boxes containing "DRAFT" from a document.

' StoryRanges loop by macropod -- http://www.vbaexpress.com/forum/archive/index.php/t-27391.html
' Shapes code adapted from https://www.experts-exchange.com/questions/27274592/Word-VBA-access-Text-box-in-header.html

    Dim pRange As Range ' The story range, to loop through each story in the document
    Dim sShape As Shape ' For the text boxes, which Word considers shapes
    Dim strTextBoxText As String ' To test whether text in a text box contains "DRAFT"
    
    'Loop through all of the stories
    For Each pRange In ActiveDocument.StoryRanges

        Do
            'Loop through all shapes in the current story, to find the text boxes
            For Each sShape In pRange.ShapeRange
                               
                'Test to see if the shape a text box (then see if it contains "DRAFT," then delete it, if so)
                If sShape.Type = msoTextBox Then
                    Debug.Print sShape.Name
                                        
                    'If the shape is a text box, then select it
                    sShape.Select
                    
                    'Determine if the text box has "DRAFT": retrieve the text...
                    Selection.ShapeRange.TextFrame.TextRange.Select
                    '...and assign it to the strTextBox variable, to be dropped into an InStr function
                    strTextBoxText = Selection.Text
                    
                    Debug.Print strTextBoxText
                    
                    '...and test the string
                    If InStr(strTextBoxText, "DRAFT") = 1 Then '' InStr function returns a 1 if found, 0 if not

                        Debug.Print "Draft found in text box"
                        
                        '...and delete it if it's a draft stamp
                        sShape.Delete
                    End If

                End If

            Next 'sShape
           
            Set pRange = pRange.NextStoryRange
            
        Loop Until pRange Is Nothing
    Next

End Sub

Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop through all shapes in all stories not working Loop through worksheet range to show / hide shapes. wlcdo2 Excel Programming 2 02-22-2017 05:10 PM
Loop through all shapes in all stories not working Can a word doc have 2 continuous 'stories' across multiple pages? Cosmo Word 1 06-19-2014 01:09 PM
While loop not working right Cbrehm Excel Programming 0 05-11-2011 11:05 AM
Loop through all shapes in all stories not working Where did map shapes go? SueK PowerPoint 1 01-20-2011 04:30 AM
Loop through all shapes in all stories not working PPT 2010 - Video Shapes and Effects Not Working stepper PowerPoint 2 01-13-2011 12:44 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:03 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft