Results 1 to 3 of 3

Thread: How to get the .Name of a Picture/Shape was behind another Picture/Shape

  1. #1
    Seeker NJR's Avatar
    Join Date
    Jun 2014
    Posts
    11
    Articles
    0
    Excel Version
    2010

    How to get the .Name of a Picture/Shape was behind another Picture/Shape



    Register for a FREE account, and/
    or Log in to avoid these ads!

    Hi,

    I have two Pictures I've inserted into a workbook - one Picture is directly on top of the other.

    I assign a Macro to the top Picture - which, when selected, calls "Sub Check" and moves that Picture to the back of the ZOrder - making the bottom Picture now visible.

    I then want to get:
    - The .Name of the Picture that had been on top and is now moved to the Back <-- This works
    - The Cell where the two Pictures are located <-- This works
    - The .Name of the Picture that was at the Back and is now at the Front <-- Stuck Here

    The code I've used is below - but I'm at a loss as to a solution to get that .Name.

    Any suggestions are welcome. (I also attached a sample of the code working so far - if that helps.)

    Code:
    Option Explicit
    Dim strTheCellSelected As String 'To track the Cell of the Picture that was selected.
    Dim shCoverPicture As Shape 'The Cover Picture that has been selected.
    Dim varUserSelection As Variant 'What has been selected on the screen.
    
    Sub Check()
    
        'Determine which Cover Picture was selected and Move it to the Back so the underlying Picture is showing.
        ActiveSheet.Shapes(Application.Caller).Select
        Selection.ShapeRange.ZOrder msoSendToBack
    
        'Pull-in what is selected on screen
        Set varUserSelection = ActiveWindow.Selection
    
        'Get the Name of the first Cover Picture selected.
        Set shCoverPicture = ActiveSheet.Shapes(varUserSelection.Name)
        MsgBox ("shCoverPicture.Name = " & shCoverPicture.Name)
        
        'Get the address of the selected Cell
        strTheCellSelected = shCoverPicture.TopLeftCell.Address
        MsgBox ("strTheCellSelected = " & strTheCellSelected)
        
        '***** Stuck Here *****
        'Get the .Name of the Picture/Shape that is undernearth the shCoverPicture that has been selected by the User.
        
            
    End Sub
    Attached Files Attached Files

  2. #2
    Super Moderator p45cal's Avatar
    Join Date
    Dec 2012
    Posts
    1,568
    Articles
    0
    Excel Version
    365
    try:
    Code:
    Sub Check()
    
    'Determine which Cover Picture was selected and Move it to the Back so the underlying Picture is showing.
    Set shCoverPicture = ActiveSheet.Shapes(Application.Caller)
    shCoverPicture.ZOrder msoSendToBack
    
    'Get the Name of the first Cover Picture selected.
    MsgBox ("shCoverPicture.Name = " & shCoverPicture.Name)
        
    'Get the address of the selected Cell
    strTheCellSelected = shCoverPicture.TopLeftCell.Address
    MsgBox ("strTheCellSelected = " & strTheCellSelected)
        
    '***** Stuck Here *****
    'Get the .Name of the Picture/Shape that is undernearth the shCoverPicture that has been selected by the User.
    zpos = -1    'impossible z position
    For Each shp In ActiveSheet.Shapes
      If shp.TopLeftCell.Address = strTheCellSelected Then
        If shp.ZOrderPosition >= zpos Then
          Set TopMostShp = shp
          zpos = shp.ZOrderPosition
        End If
      End If
    Next shp
    MsgBox "Top shape name is " & TopMostShp.Name
    End Sub

  3. #3
    Seeker NJR's Avatar
    Join Date
    Jun 2014
    Posts
    11
    Articles
    0
    Excel Version
    2010
    Hey p45cal - That worked like a charm.

    Thanks so much for the break-through. I had been picking away at that for quite some time.

    This solution taught me some new tricks...

Posting Permissions

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