Consulting

Results 1 to 6 of 6

Thread: Loop through shapes on current page

  1. #1
    VBAX Regular
    Joined
    Dec 2015
    Posts
    9
    Location

    Loop through shapes on current page

    Is there a way to loop through the shapes on only the current page of a Word document? I can get the current page number, and I know how to loop through all pages and shapes in the document. But I only want to work with a single page in the middle of the document. Probably I'll have to loop through all shapes and ask each shape what page it is on. Maybe an algorithm like this would work?

    get and save the current page number
    loop through all shapes in the document
    select each shape in turn (and hope that resets the current page?)
    use selection.Information() to get the current page number of the selected shape
    (or use Shape.Anchor.Information(wdActiveEndPageNumber))
    
    If the page numbers match
      - then this shape is on the original current page

    UPDATE: Yes, this algorithm worked fine. Here's a bit of code:

    curpage = selection.Information(wdActiveEndPageNumber)
    For Each shp In doc.shapes
       shapepage = shp.Anchor.Information(wdActiveEndPageNumber)
       If curpage = shapepage Then
          ' do your thing with the shape on the original current page
       End If
    Next
    Last edited by kkkwj; 07-11-2018 at 07:12 PM.

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    2,845
    Location
    Why not just:

    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 7/11/2018
    Dim shp As Shape
    For Each shp In ActiveDocument.Shapes
       If shp.Anchor.Information(wdActiveEndPageNumber) = Selection.Information(wdActiveEndPageNumber) Then
         'Work with the shape
       End If
    Next
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Dec 2015
    Posts
    9
    Location
    Yes, that would work fine too. Is it necessary to loop through ALL shapes to find only the shapes on the current page? In other words, are shapes stored in the shapes collection in page order? (If they were, I could exit the loop early when shp.page > selection.page (so to speak).

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,607
    Location
    Try:
    Sub Demo()
    Dim Rng As Range, Shp As Shape
    Set Rng = Selection.Range
    Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\page")
    For Each Shp In Rng.ShapeRange
      ' do your thing with the shape
    Next
    End Sub
    Cheers
    Paul Edstein
    [MS MVP - Word]

  5. #5
    VBAX Regular
    Joined
    Dec 2015
    Posts
    9
    Location
    Hi Paul, it's too funny that you post your code with the shapes organized by a page range. In my original post, I had a sentence wondering if shapes were organized by pages internally - then I could go straight to the shapes that I needed. But I removed the sentence because I thought, "No way that would be true." But apparently, they are... Thank you! Cheers, Kevin

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,607
    Location
    Quote Originally Posted by kkkwj View Post
    In my original post, I had a sentence wondering if shapes were organized by pages internally - then I could go straight to the shapes that I needed. But I removed the sentence because I thought, "No way that would be true." But apparently, they are...
    They're not organised by page … but you can find what objects are on a page.
    Cheers
    Paul Edstein
    [MS MVP - Word]

Posting Permissions

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