Reputation: 691
I want to add this code to ThisDocument
in my Word .docm file.
Sub AutoClose()
ActiveDocument.Saved = False
End Sub
The code prevents the "Do you want to save?" dialog from appearing. I only want to add this however after a certain click event in the document, because I also have Save As
and Save
disabled, and if I add all three, then I wouldn't be able to save the document myself. (In fact, I can't add all three, for that reason.)
If I add the above code only to be active after the last click event in the document however it should be fine, since then I can still save changes I make, as long as I haven't clicked that trigger at the end yet. Preventing people from closing and getting the "Do you want to save?" dialog at the end is the most important to me.
is there a way to write the above code to ThisDocument when another click event fires? Or put another way, is there any way to have a click event put the above code into effect?
Here's the sub at the end that I'd want to have trigger the above code to (activate? Be written? Be enabled? Be uncommented?)
Private Sub formatSaveB_Click()
ActiveDocument.Bookmarks("mark3").Select
Selection.Delete
ActiveDocument.InlineShapes(1).Delete
With Dialogs(wdDialogFileSaveAs)
.Format = wdFormatFilteredHTML
.Show
End With
End Sub
So after that event happens, I want
Sub AutoClose()
ActiveDocument.Saved = False
End Sub
to be active in ThisDocument -- but not before that event.
Upvotes: 1
Views: 390
Reputation: 2241
Programatic access to the VBA project will need to be enabled if it isn't already. However, to add the sub you described after the other code runs add this code to your sub:
ThisDocument.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString "Sub AutoClose(): ActiveDocument.Saved = False: End Sub"
That should do the trick. You may have to tinker a little, I can't test fully because I'm in an environment where programatic access to the VBA project is disabled, but that should do what it sounds like you want to do.
Slight edit: If you want to prevent the "Do you want to save changes you made ... " message when closing word you would need to do this:
ThisDocument.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString "Private Sub Document_Close(): ActiveDocument.Saved = True: End Sub"
Which will prevent that message from displaying when closing the document.
Upvotes: 1
Reputation: 691
Okay adding this line from MattB did the trick, after I discovered that his suggestion had a typo, and I deleted the extra letter :)
ThisDocument.VBProject.VBCompontents("ThisDocument").CodeModule.AddFromString "Private Sub Document_Close(): ActiveDocument.Saved = True: End Sub"
I was just pasting it in to try it like the others, and it had VBCompontents
which I didn't spot until just now going over this again, and once I deleted the extra t
, bingo.
Just what I needed. Thanks a bunch.
Upvotes: 0
Reputation: 22195
Set a flag in the event handler you want to disable the dialog, then test it when you exit. Note that you have to check for either the flag or if the document is already saved to avoid being prompted if the document actually has been saved but you haven't triggered formatSaveB_Click(). Assumes that formatSaveB_Click()
is in ThisDocument.
In ThisDocument:
Private clicked As Boolean
Sub AutoClose()
ActiveDocument.Saved = clicked Or ActiveDocument.Saved
End Sub
Private Sub formatSaveB_Click()
ActiveDocument.Bookmarks("mark3").Select
Selection.Delete
ActiveDocument.InlineShapes(1).Delete
With Dialogs(wdDialogFileSaveAs)
.Format = wdFormatFilteredHTML
.Show
End With
'Set your flag here.
clicked = True
End Sub
Upvotes: 0
Reputation: 17637
I can't completely follow your logic but I think what you're asking is something like this:
ThisDocument module
Public event1 As Boolean
Public event2 As Boolean
Sub AutoOpen()
event1 = False
event2 = False
End Sub
Event Procedures
Private Sub formatSaveB_Click()
'// Event code here...
event1 = True
End Sub
Private Sub otherEvent_Click()
'// Event code here...
event2 = True
End Sub
Then in your final sub
Sub AutoClose()
'// Saved state only set to true if previous 2 events have been executed.
ActiveDocument.Saved = (event1 And event2)
End Sub
Upvotes: 0