Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-01-2023, 06:45 PM
elaineAda elaineAda is offline macro to close document without saving but password protected Windows XP macro to close document without saving but password protected Office 2010
Novice
macro to close document without saving but password protected
 
Join Date: Mar 2019
Posts: 25
elaineAda is on a distinguished road
Default macro to close document without saving but password protected

Hi Hi


I have problem writing a code to close a document but with password protected without saving if user does not want to save.

This code is not able to password protect if user decides not to save the file and exit word.

Code:
Private Sub Document_Close()

With ActiveDocument
    If .ProtectionType = wdNoProtection Then
         .Protect wdAllowOnlyReading, NoReset:=True
    Else
        .Unprotect
    End If
End With
Reply With Quote
  #2  
Old 02-01-2023, 10:07 PM
gmayor's Avatar
gmayor gmayor is offline macro to close document without saving but password protected Windows 10 macro to close document without saving but password protected Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,103
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

What you appear to be asking is not possible. If you make any change to the structure of the document, such as adding a password, then if you close the document without saving, any change you make is lost.
Why if the document is supposed to be protected as read only, would the protection have been removed?
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 02-01-2023, 10:26 PM
gmayor's Avatar
gmayor gmayor is offline macro to close document without saving but password protected Windows 10 macro to close document without saving but password protected Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,103
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

See your other post for a possible workaround.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #4  
Old 02-01-2023, 10:44 PM
elaineAda elaineAda is offline macro to close document without saving but password protected Windows XP macro to close document without saving but password protected Office 2010
Novice
macro to close document without saving but password protected
 
Join Date: Mar 2019
Posts: 25
elaineAda is on a distinguished road
Default

Hi
This is not for a form but rather an editable word document.
I am trying to password protect the file, user need to click on 'edit content' to be able to use the file.
It works nicely if user save and exit the document. The file will also be password protected when the user next open the file.
However, if user exit the document without saving the file, the file will become not protected and encounter error message when the user next open the file.
One workaround method is to undo all changes and save the file, but i am not sure how to write the macro for undo changes.
Appreciate your help!
Thanks.
Attached Files
File Type: docm test 2Feb.docm (21.5 KB, 1 views)
Reply With Quote
  #5  
Old 02-02-2023, 10:32 PM
gmayor's Avatar
gmayor gmayor is offline macro to close document without saving but password protected Windows 10 macro to close document without saving but password protected Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,103
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Unfortunately I fail to see the logic of what you are doing and your example document doesn't disclose anything useful to aid understanding.

Either the document is editable, in which case the protection would appear superfluous, or only parts of it are editable, in which case marking those parts as editable, or better still using content controls (which are not limited to forms) would seem to make more sense.
If the document is intended to be reused then surely it would be better saved as a template and users create new documents from it, which would preclude the need to clear it between uses as the documents would match the template?
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #6  
Old 02-02-2023, 10:51 PM
elaineAda elaineAda is offline macro to close document without saving but password protected Windows XP macro to close document without saving but password protected Office 2010
Novice
macro to close document without saving but password protected
 
Join Date: Mar 2019
Posts: 25
elaineAda is on a distinguished road
Default

Hi
I am sorry, I think i have not elaborated on my intention.
My intention is to force user to 'enable content' before using the document as this is a macro-embeded document. This document is not a form, it is only a skeleton to help users and users can add in as many sections or graphics as they like. This document will be placed in a sharepoint, user to download this document, complete and send to another user (user 2, user 3 ...) to edit/approve. The use of the document will stop there.

So after user complete and save and close the document, the document should remain as password protected. And when the next user (user2, user 3...) open this file, they will also need to 'enable content' in order to edit the file.

Does this help?
Reply With Quote
  #7  
Old 02-03-2023, 02:18 AM
gmayor's Avatar
gmayor gmayor is offline macro to close document without saving but password protected Windows 10 macro to close document without saving but password protected Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,103
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

From everything you have reported, I cannot see any good reason to have the document protected, if users can change it at will. The 'skeleton' document appears to be a template. Save it as a template and get users to create new documents from it. Then the macro and password would be superfluous.
As I mentioned earlier, you cannot make changes to a document and then retain those changes when you close it without saving it.
Clearing the data and saving it is problematical for a document that allows free editing. You would have to store the original content somewhere that the user has access to and replace the edited version with the stored original. None of this is necessary if you use a template.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
macro to close document without saving but password protected Close Document Without Saving Prompt Joe528 Word 3 10-08-2021 06:12 PM
macro to close document without saving but password protected Looking for ideas for a foolproof password-protected template/document Sarcas Word 2 09-11-2019 01:21 AM
VBA Macro to Open password protected Word Documents gennylk Word VBA 2 01-11-2018 11:03 PM
macro to close document without saving but password protected Macro to Unprotect password protected document pooklet Word 2 12-08-2014 01:32 AM
How to open a password-protected word document? navalava Word 1 07-01-2012 12:15 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:16 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft