Results 1 to 7 of 7
  1. #1
    4 Star Lounger
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    472
    Thanks
    43
    Thanked 0 Times in 0 Posts

    Code not working in 2010

    For many years we have usewd code to remove a graphic and watermarks from drtaft reports when they are finalised. However, since installing Office 2010 some of the code refuses to behave.
    In this code:

    If ActiveDocument.Shapes("Picture 9") = True Then
    Selection.ShapeRange.Delete
    Else
    Having manually, by recording a macro, identified the shapes we wish to delete the code is ignored when run.

    If I run this code:
    ActiveDocument.Shapes.SelectAll
    Selection.ShapeRange.Delete
    it works fine!

    This is causing great frustration and is hugely tmie wasting.

    This is very closely aligned to my last post http://windowssecrets.com/forums/sho...rmat-or-delete
    cheers

    Phil Carter

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,058
    Thanks
    196
    Thanked 765 Times in 699 Posts
    Phil,

    I tested this code in Word 2007 and it seems to do what I think you are after.
    Code:
       ActiveDocument.Shapes.SelectAll
       Selection.ShapeRange("AutoShape 3").Delete
    My test document contained 2 auto shaped AutoShape 3 & AutoShape 2. AutoShape 3 was the first physically in the document, i.e. closest to the top. AutoShape 3 was successfully deleted leaving AutoShape 2.

    Note: According to MSDN documentation InLine shapes are not in this collection!
    Last edited by RetiredGeek; 2011-10-09 at 20:08.
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  4. The Following User Says Thank You to RetiredGeek For This Useful Post:

    bonriki (2011-10-10)

  5. #3
    4 Star Lounger
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    472
    Thanks
    43
    Thanked 0 Times in 0 Posts
    RG thanks for that

    Which code?

    It's the first lot that we can't get to work
    cheers

    Phil Carter

  6. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,058
    Thanks
    196
    Thanked 765 Times in 699 Posts
    Ok,

    Here's a generalized function that I hope will be what you need.
    Code:
    Function bDeleteShape(zShapeToDel As String) As Boolean
    
      Dim zShapeName As String
      
      On Error GoTo NotFound
      bDeleteShape = True
      zShapeName = ActiveDocument.Shapes(zShapeToDel).Name
      On Error GoTo 0
      If bDeleteShape Then
        ActiveDocument.Shapes.SelectAll
       Selection.ShapeRange(zShapeToDel).Delete
      End If
       
      GoTo Out
      
    NotFound:
      bDeleteShape = False
      Resume Next
      
    Out:
    
    End Function   '*** bDeleteShape ***
    The function will return true if the item is found and deleted or false if the item is not found.

    Sample call: If bDeleteShape("Picture 9") then ...

    Or if you don't care about the result: bDeleteShape "Picture 9"

    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  7. The Following User Says Thank You to RetiredGeek For This Useful Post:

    bonriki (2011-10-10)

  8. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by bonriki View Post
    In this code:

    Code:
            If ActiveDocument.Shapes("Picture 9") = True Then
                Selection.ShapeRange.Delete
            Else
    How do you know that ActiveDocument.Shapes("Picture 9") is within Selection.ShapeRange?

  9. #6
    New Lounger
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You may need to check the object references - I forget exactly where they are and can't find it on the machine I'm currently using.

    Also, it would be helpful to know exactly what the error is that you're seeing.

  10. #7
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 28 Times in 27 Posts
    Just belatedly noting that your first snippet of posted code, won't appear to be doing anything when run, because the If test isn't testing for anything that can return a True! That is, what are you trying to test for with the line?:

    If ActiveDocument.Shapes("Picture 9") = True Then

    I think that's always going to return the default False, and therefore not run the Delete that's in the line following, because you're not testing any property of "ActiveDocument.Shapes("Picture 9")" that might return a True or False.
    Hard to see how this would have worked in 2003, either.

    If you're trying to test for the existence of a Shape called "Picture 9", that's a different thing, and would require different code.

    In any case, if RG's function works for you, run with it!

    Gary

Posting Permissions

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