+ Reply to Thread
Results 1 to 2 of 2

Error when testing for existence of shape object

Hybrid View

AlvaroSiza Error when testing for... 10-20-2012, 07:40 PM
AlvaroSiza Re: Error when testing for... 10-20-2012, 09:13 PM
  1. #1
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Error when testing for existence of shape object

    The routine begins by being passed two strings, one being the category of map, and the second being the shape name used to hold a GoogleStaticMapAPI map.
    Public Sub GetMap(strCategory As String, strShapeName As String)
    I'm attempting to error trap the instance where a request was made for a map, but the shape (container) that will be the location of the map does not exist (user deleted, anomaly, etc.) and then create one prior to requesting the map. How do I test for the existence of a shape (and re-work the provided code) with the name passed at routine onset?

    I continue to receive an error that the object doesn't exist. Exactly. I know it doesn't exist because I deleted for testing. Must I really cycle through all shapes in the worksheet via For Each Next construct as has been suggested elsewhere on the net?

    Direction appreciated.

    wsMaps.Activate
            If Not wsMaps.Shapes(strShapeName) Is Nothing Then ' shape object exists; request map
                Call GoogleStaticMap(wsMaps.Shapes(strShapeName), strGoogle)
            Else ' shape does not exist, create new shape, name it, then request map
                With wsLists
                    Set rngSource = .Range("DV2:DV14")
                    Set rngFound = rngSource.Find(strShapeName)
                    strName = CStr(rngFound)
                    strLoc = rngFound.Offset(0, 1)
               End With
               '   Set/create a msoShapeRectangle shape object with name and
               '   location from strings above 
               With wsMaps
                    .Activate
                    Set oShape = wsMaps.Shapes.AddShape(msoShapeRectangle, Range(strLoc).Left, _
                        Range(strLoc).Top, lngWidth, lngHeight)
                    oShape.Name = strName
                    oShape.Select
                    With Selection.ShapeRange ' Line = White; Solid Fill = White
                        With .Line
                            .Visible = msoTrue
                            .ForeColor.ObjectThemeColor = msoThemeColorBackground1
                            .ForeColor.TintAndShade = 0
                            .ForeColor.Brightness = 0
                            .Transparency = 0
                        End With
                        With .Fill
                            .Visible = msoTrue
                            .ForeColor.ObjectThemeColor = msoThemeColorBackground1
                            .ForeColor.TintAndShade = 0
                            .ForeColor.Brightness = 0
                            .Transparency = 0
                            .Solid
                        End With
                    End With
                End With
            End If
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

  2. #2
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Error when testing for existence of shape object

    Well, I took the Error Handling route with some success. If someone sees this and thinks it could be optimized, I would appreciate the insight.

    With wsMaps
            .Activate
            On Error GoTo ErrorHandler
            ' Error = shape object does not exist; go to error handler to create shape, resize, name, and request map
            ' No error = shape object exists; request map
            If Not .Shapes(strShapeName) Is Nothing Then 
            Call GoogleStaticMap(.Shapes(strShapeName), strGoogle)
            End If
        End With
        Exit Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1