Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-10-2012, 08:37 AM
TishyMouse TishyMouse is offline Can't select shapes via macro Windows XP Can't select shapes via macro Office 2007
Novice
Can't select shapes via macro
 
Join Date: Feb 2012
Posts: 22
TishyMouse is on a distinguished road
Question 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!
Reply With Quote
  #2  
Old 04-10-2012, 06:17 PM
macropod's Avatar
macropod macropod is online now Can't select shapes via macro Windows 7 64bit Can't select shapes via macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
PS: When posting code, please use the code tags (they're on the Advanced tab).
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 04-13-2012, 12:51 AM
TishyMouse TishyMouse is offline Can't select shapes via macro Windows XP Can't select shapes via macro Office 2007
Novice
Can't select shapes via macro
 
Join Date: Feb 2012
Posts: 22
TishyMouse is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 04-13-2012, 03:26 AM
macropod's Avatar
macropod macropod is online now Can't select shapes via macro Windows 7 64bit Can't select shapes via macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply

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
Can't select shapes via macro Where did map shapes go? SueK PowerPoint 1 01-20-2011 04:30 AM
Can't select shapes via macro 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

Other Forums: Access Forums

All times are GMT -7. The time now is 06:37 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