Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    MS word 2010 macro How to select all shapes on a specific page

    I need to delete the first and the last shape on each and every page of a 300+ page word document. (The shapes do not occur in the header / footer).
    My approach is to select all shapes occurring on one page. Delete the first and the last shape. Then go to the next page (i.e. loop through the pages).
    I tried
    ActiveDocument.Pages(1).Shapes.Range.Select

    (this comand i picked up from the msdn vba reference office 2003).
    It doesnot work in word 2010.

    What would be the equivalent command in word 2010?

    Thanks for taking time to read through this post.

    Regards

  2. #2
    New Lounger
    Join Date
    Dec 2009
    Location
    Verona, WI
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I cannot provide a way with code to pick the first and last shape on a particular page but the following code will help you step through and delete them one by one. As written, this works with text boxes.

    Sub TextBoxDeleteOneByOne()
    ' Scroll through all text boxes, delete if directed to
    Dim shp As Shape, intNbrShapes As Integer, vResponse As Variant
    Dim strBoxNbr As String, intBoxNbr As Integer, strMsg As String

    intNbrShapes = 0
    For Each shp In ActiveDocument.Shapes
    If shp.Type = msoTextBox Then
    intNbrShapes = intNbrShapes + 1
    shp.Select
    ' Scroll through the active document so that the current selection is visible in the document window.
    ActiveWindow.ScrollIntoView Selection.Range, True
    strMsg = shp.TextFrame.TextRange.Text

    vResponse = MsgBox("Text box name: " & shp.Name & vbCrLf & "Text Contents: " & strMsg & vbCrLf & "Delete?", vbYesNoCancel, "Finding Text Boxes")
    shp.Select
    If vResponse = vbYes Then ' User chose Yes.
    shp.Delete
    Else
    If vResponse = vbCancel Then
    Exit Sub
    End If
    End If
    End If
    Next shp

    End Sub

  3. #3
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    87
    Thanks
    5
    Thanked 5 Times in 5 Posts
    The shapes collection in a Word document applies to the entire story (i.e. the main body or the headers and footers), and is not divided by pages. Pages don't have a shapes collection. Shapes per se don't have a page property either.

    What might work, and I don't have time to test this, is to work with the Information property of the shape's range.

    What you are going to have to do is iterate through the shapes in the document and select each one. Use the range.information(wdActiveEndPageNumber) property to determine the page that the shape is on. Cache that information. Then iterate until the page number changes. That shape is at the top of the page. To find the shape at the bottom of the page, iterate until the property changes, then back up one shape. That shape is at the bottom of a page.

    BUT there is a conceptual problem. Unless you are using hard page breaks, when you delete a shape you will probably change the page divisions. How will you handle that?

    Maybe someone else has a better solution.

    - Jessica

  4. #4
    New Lounger
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you can use this code

    Sub SelectAllShapes()
    ActiveDocument.shapes.SelectAll

    End Sub

Tags for this Thread

Posting Permissions

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