#1
|
|||
|
|||
Can't select shapes via macro
I have a word documents created from a converted PDF. I'm trying to programatically select in turn the shapes within the document. If I create a test macro, place a break point in it with a watch expression ActiveDocument.Shapes.Count and run it I can see that there are >2000 of shapes in the document; however the code inside a 'for each shape...' loop never gets executed.
Note that if I copy and paste the shape into a new document the macro works. The code I am using is: Sub SelectShapes() Dim sh As Shape For Each sh In ActiveDocument.Shapes sh.Select 'Will do some other things here Next End Sub Any ideas what might be going on here? Thanks! |
#2
|
||||
|
||||
hi TishyMouse,
You shouldn't need to select the shapes to do something with them. For example: Code:
Sub ProcessShapes() Dim sh As Shape For Each sh In ActiveDocument.Shapes With sh MsgBox .Name 'Will do some other things here End With Next End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thanks for the response Paul. Actually the problem was not the selection, the problem was that the code never even entered the For...Each loop - it didn't pick up the fact that there were shapes in the document at all. It transpires that this is due to the shapes being in a different StoryRange in the document. I finally managed to select the shapes (Text Frames in this case) using the code below. I was able to successfully copy and paste the content of each of the text frames into a separate document. Ideally I would have just liked to remove the text frames in-situ (copying the content into the main body of the document) but I couldn't get this to work.
Code:
Sub SelectShapes() Set docSource = ActiveDocument Set docTarget = Documents.Add("Normal") docSource.Activate For Each sr In docSource.StoryRanges If sr.StoryType = wdTextFrameStory Then sr.Select done = False While Not done sr.Select Selection.WholeStory Selection.Copy docTarget.Activate Selection.PasteAndFormat (wdPasteDefault) docSource.Activate Set sr = sr.NextStoryRange If sr Is Nothing Then done = True Wend End If Next sr End Sub |
#4
|
||||
|
||||
Hi TishyMouse,
Yes, unless you specify a storyrange (and there are various ways of doing this), Word assumes you're only referring to the main story. While the selection might not have been the 'problem', your code can be made much more efficient without it: Code:
Sub ProcessShapes() Dim DocSource As Document, DocTarget As Document Dim RngStry As Range, Shp As Shape Set DocSource = ActiveDocument Set DocTarget = Documents.Add("Normal") For Each RngStry In DocSource.StoryRanges For Each Shp In RngStry.ShapeRange With Shp .Copy DocTarget.PasteAndFormat (wdPasteDefault) End With Next Next Set DocTarget = Nothing: Set DocSource = Nothing End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
shapes |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Select Series Color - Macro | judicial85 | Excel Programming | 0 | 03-14-2011 02:35 PM |
Where did map shapes go? | SueK | PowerPoint | 1 | 01-20-2011 04:30 AM |
Find and add new Shapes | bonani | PowerPoint | 1 | 11-26-2009 06:21 PM |
Shapes Will Not Display | JoeTx | Visio | 0 | 03-13-2008 09:01 AM |
My Shapes some appear some don't | Jean-Paul | Visio | 0 | 03-01-2006 01:38 AM |