Consulting

Results 1 to 5 of 5

Thread: Solved: Need to prevent save or close if msoShapeCross exists in specified sheet.

  1. #1
    VBAX Expert
    Joined
    Sep 2010
    Posts
    604
    Location

    Solved: Need to prevent save or close if msoShapeCross exists in specified sheet.

    Would it be complicated to prevent save or close if an msoShapeCross exists in specified sheet"

    I wrote this to determine if a cross shape exist, but have no idea how to implement it in the before save and before close events.
    [vba]Sheets("DATA").Select

    For Each shp In ActiveSheet.Shapes

    If shp.AutoShapeType = msoShapeCross Then
    shp.TopLeftCell.EntireRow.Hidden = False
    shp.TopLeftCell.Select
    MsgBox "Cannot close or Save this workbook until the Cross shape has been clicked"
    'The cross is tied to a macro where after the macro runs the cross is deleted
    End If

    Next[/vba] Thanks

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Option Explicit

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If CheckShape Then Cancel = True
    End Sub

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If CheckShape Then Cancel = True
    End Sub

    Private Sub Workbook_Open()

    End Sub

    Private Function CheckShape() As Boolean
    Dim shp As Shape

    With Sheets("DATA")

    For Each shp In .Shapes

    If shp.AutoShapeType = msoShapeCross Then

    shp.TopLeftCell.EntireRow.Hidden = False
    .Activate
    shp.TopLeftCell.Select
    MsgBox "Cannot close or Save this workbook until the Cross shape has been clicked"
    'The cross is tied to a macro where after the macro runs the cross is deleted
    End If
    Next
    End With
    End Function
    [/vba]

    This is workbook event code.
    To input this code, right click on the Excel icon on the worksheet
    (or next to the File menu if you maximise your workbooks),
    select View Code from the menu, and paste the code
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Expert
    Joined
    Sep 2010
    Posts
    604
    Location
    HI Bob,

    I appreciate the help. The code as is prevents saving, but does not prevent closing.
    - When I attempt to close the file, I get my msgbox, then a dialog asking if I want to save the workbook, if I click yes I get my msgbox again, then it closes.

    Sample workbook attached

    Click the in or out buttons to create the the cross.
    - Click the cross to delete it.

    Thanks
    Attached Files Attached Files

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I orgot the main bit in that code

    [vba]

    Private Function CheckShape() As Boolean
    Dim shp As Shape

    With Sheets("Tool_Log")

    For Each shp In .Shapes

    If shp.AutoShapeType = msoShapeCross Then

    shp.TopLeftCell.EntireRow.Hidden = False
    .Activate
    shp.TopLeftCell.Select
    MsgBox "Cannot close or Save this workbook until the Cross shape has been clicked"
    'The cross is tied to a macro where after the macro runs the cross is deleted

    CheckShape = True
    Exit For
    End If
    Next
    End With
    End Function
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Expert
    Joined
    Sep 2010
    Posts
    604
    Location
    Awesome, works great now

    Thank you much Bob

Posting Permissions

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