Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-16-2020, 11:56 PM
killmenow killmenow is offline Find all shapes in a document Windows 10 Find all shapes in a document Office 2007
Novice
Find all shapes in a document
 
Join Date: Jun 2020
Posts: 2
killmenow is on a distinguished road
Default Find all shapes in a document

Hi all

I am new to VBA and new to this forum ("Hello Lawrence")



I have a word document with shapes that I added via the Insert \ Shapes menu entry

Some of the shapes are lines, others are rectangles and some are callouts. I even have a picture or two. (Some are in the header area)

I wanted a script to run through the shapes and select them. I tried this:

Sub FindShapes()
Dim doc As Word.Document, rng As Word.Range
Dim shp As Word.Shape


Set doc = ActiveDocument
Set tot = doc.Shapes

a = tot.Count

For Each shp In doc.Shapes
shp.Select
Debug.Print shp.Type; shp.ID, shp.Name


Next

End Sub


But it does not find all the shapes. In same cases it may find only one shape.

What am I doing wrong?

Thank you

Lawrence
Reply With Quote
  #2  
Old 06-17-2020, 12:40 AM
gmayor's Avatar
gmayor gmayor is offline Find all shapes in a document Windows 10 Find all shapes in a document Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

For a start you are not differentiating between shapes and inline shapes and you need to check all the story ranges where there are shapes e.g. as follows. Note that some of the parameters you want to list are not applicable to inline shapes.



Code:
Sub CheckShapes()
Dim oStory As Range
Dim oShape As Shape
Dim oiShape As InlineShape

    For Each oStory In ActiveDocument.StoryRanges
        For Each oShape In oStory.ShapeRange
Debug.Print oShape.Type & "; " & oShape.ID & "; " & oShape.Name
        Next oShape
        For Each oiShape In oStory.InlineShapes
Debug.Print oiShape.Type
        Next oiShape
        If oStory.StoryType <> wdMainTextStory Then
            While Not (oStory.NextStoryRange Is Nothing)
                Set oStory = oStory.NextStoryRange
                For Each oShape In oStory.ShapeRange
Debug.Print oShape.Type & "; " & oShape.ID & "; " & oShape.Name
                Next oShape
                For Each oiShape In oStory.InlineShapes
Debug.Print oiShape.Type
                Next oiShape
            Wend
        End If
    Next oStory
    Set oStory = Nothing
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 06-17-2020, 12:46 AM
killmenow killmenow is offline Find all shapes in a document Windows 10 Find all shapes in a document Office 2007
Novice
Find all shapes in a document
 
Join Date: Jun 2020
Posts: 2
killmenow is on a distinguished road
Default

Thank you very much

I will check this
Reply With Quote
Reply

Tags
shapes

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find all shapes in a document Find several words in document, copy paragraph and create new document coolio2341 Word VBA 6 01-31-2019 01:17 PM
Find This Or That On Line Six of Word Document StephenRay Word VBA 33 09-29-2017 02:01 PM
Find all shapes in a document Won't find Word document wblock@cnu.edu Word 4 08-23-2017 06:11 PM
Find all shapes in a document Does a document need to find original template? PatrickYork Word 1 01-05-2012 04:12 AM
Find all shapes in a document Find and add new Shapes bonani PowerPoint 1 11-26-2009 06:21 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:52 AM.


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