Search code examples
vbams-wordactivexobject

ActiveX control Blocks Me.Saved=True


Previously I have used this:

Private Sub Document_Close()
    Me.Saved = True
End Sub

to disable the save prompt when exiting a Word document and changes have been made, however I have added a "Combo Box (ActiveX Control)", and now Word is prompting to save again. Is there a way around this?

I've tried writing code to just delete the Combo Box when the document closes, but the box deletes itself after being used (not my code, it just does), then when the document closes the box doesn't exist and causes an error there. I could just do an if exist/error control, but I feel like that is just getting sloppy instead of finding the root problem... can someone help?


Solution

  • If had the same issue, and found the solution with Bing: http://answers.microsoft.com/en-us/office/forum/office_2007-customize/activex-control-blocks-ms-word-vba/71eca664-8e43-4e4f-84c5-59154661ee5a

    The following code helped me to get around this issue:

    Dim WithEvents App As Application
    
    Private Sub App_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
      'Did the user saved our file?
      If Not ThisDocument.Saved Then
        'Close our file?
        If Doc.Name = ThisDocument.Name Then
          'Cancel the dialog always!
          Cancel = True
          'Set the save state
          ThisDocument.Saved = True
          'Close the document after this event
          Application.OnTime Now + TimeSerial(0, 0, 1), Me.CodeName & ".Document_AfterClose"
        End If
      End If
    End Sub
    
    Sub Document_AfterClose()
      'Close the document without saving
      ThisDocument.Close False
    End Sub
    
    Private Sub cboEquip_Change()
      'Let the document know that a change is made
      ThisDocument.Saved = False
    End Sub
    
    Private Sub Document_Open()
      Set App = Application
    End Sub