• Code not working in 2010

    Author
    Topic
    #479393

    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/showthread//141489-Identifying-objects-in-Word-to-format-or-delete

    Viewing 4 reply threads
    Author
    Replies
    • #1301391

      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!

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

      • #1301392

        RG thanks for that

        Which code?

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

    • #1301395

      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”

      :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #1301398

      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?

    • #1303411

      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.

    • #1303446

      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

    Viewing 4 reply threads
    Reply To: Code not working in 2010

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: