PDA

View Full Version : Solved: Close without saving



joelle
04-12-2007, 04:17 PM
Hello Experts,

Time is running out because I tried to fix this piece of code below myself with no success so far (3 hours passed).
Please if you could land a quick hand, I'm much more than appreciated.

What this macro does:
1. If user open this word doc with macro disabled, then there is a message asking user to reopen the doc with macro enabled.
2. Then the code works fine if user has macro enabled: it deletes the "BookEmDano" and shows just the valid pages for user to use & modify.
But what drives me nuts for hours is that when user exits, it automatically saves what he/she puts in "without" asking whether user wants to save???

Please please what is the glitch here, and which line to modify so that if the document is open with macro enabled, whether or not user has made changes, it should ask before exiting. If user chooses not to save, it should *not* save.
I attach a simple file here. Please help me. Many thanks.
Nee

Option Explicit

Private Sub Document_Close()
If ActiveDocument.Bookmarks.Exists("BookEmDano") = True Then
With ActiveDocument
.ActiveWindow.View.Type = wdPrintPreview
.Protect wdAllowOnlyFormFields, "abc"
.Save
End With
Else
With Selection
.HomeKey Unit:=wdStory
.TypeText Text:="Please re-open this file with Macros Enabled."
.InsertBreak Type:=wdPageBreak
.HomeKey Unit:=wdStory, Extend:=wdExtend
ActiveDocument.Bookmarks.Add Name:="BookEmDano", _
Range:=Selection.Range
End With
With ActiveDocument
.ActiveWindow.View.Type = wdPrintPreview
.Protect wdAllowOnlyFormFields, Password:="abc"
.Save
End With
End If
End Sub

Private Sub Document_Open()

MacrosEnabled
DocExpired
End Sub
Sub MacrosEnabled()
ActiveDocument.Unprotect Password:="abc"
ActiveDocument.Bookmarks("BookEmDano").Select
Selection.Delete
End Sub
Sub DocExpired()
Dim exdate As Date
exdate = "12/31/2008"
If Date < exdate Then
Exit Sub
Else
MsgBox "This template has been expired."
With ActiveDocument
.Password = "abc"
.Save
.Close
End With
End If
End Sub

lucas
04-12-2007, 08:08 PM
Hi Nee,
Just have a second to look at this but you need to look at these entries:
.Save
you tell it to save itself several times in the code......try commenting those lines out and see if it helps.

joelle
04-12-2007, 08:21 PM
Hi Nee,
Just have a second to look at this but you need to look at these entries:
.Save
you tell it to save itself several times in the code......try commenting those lines out and see if it helps.

Hello Lucas,
Thanks for the guide above but thats where I went to and struggled for hours. I'm deadly tired now.
When I played around with the ".save", it either disrupted my other tagged along subs, or it gave me a debugging message.
My mind is blank, please help me out.
SOS
Nee

lucas
04-12-2007, 08:38 PM
Try it now Nee. I just commented the .save in 2 places in the document close statement. Let me know if this was the problem or not..

joelle
04-12-2007, 09:42 PM
Hello Lucas,
Thanks for your recent post although now it is pretty late (9:40pm my time).
I did comment out the 1st ".save" - does not help.
then I comment out the 2nd ".save" - it gave me a debug screen and the 1st line of my tagged-on vba below is highlighted.
I did try all these earlier this evening without success that is why I turned in here for help. I do know a bit of Excel vba, but none about Word vba.
Again, I'm thankful for any light you & others can shed here. [nee]


Sub MacrosEnabled()
ActiveDocument.Unprotect Password:="abc"
ActiveDocument.Bookmarks("BookEmDano").Select
Selection.Delete

fumei
04-12-2007, 11:31 PM
1. If user open this word doc with macro disabled, then there is a message asking user to reopen the doc with macro enabled. Ummm, let's be clear here.

There is no message asking the user to reopen the document. There is a page with some text on it that states that. If the doc is opened with macro enabled that page is removed.

What do you mean by:
But what drives me nuts for hours is that when user exits, it automatically saves what he/she puts in "without" asking whether user wants to save???

As has been pointed out, you have an explicit .Save with Document_Close. You close the document...it saves it.

NOTE: you have TWO .Save instructions in Document_Close. If the bookmark BookEmDano exists...there is a .Save. If the bookmark does NOT exist...you recreate the bookmark, and make an explicit .Save.

Regarding the going into debug. Please....you ALSO got an error, didn't you? When posting, whenever you get errors, and error messages, you should always state so. Did you get a 4605 error?

joelle
04-13-2007, 07:44 AM
Gerry - please my reply below:


Ummm, let's be clear here. There is no message asking the user to reopen the document. There is a page with some text on it that states that. If the doc is opened with macro enabled that page is removed. You are correct - I just wanted to keep it brief to sail down to the problem with ".save"



As has been pointed out, you have an explicit .Save with Document_Close. You close the document...it saves it.
NOTE: you have TWO .Save instructions in Document_Close. If the bookmark BookEmDano exists...there is a .Save. If the bookmark does NOT exist...you recreate the bookmark, and make an explicit .Save. This is the part I struggled tremendously with. If I comment out one of the 2 ".save", I got an error message.
So somehow this code locks itself in -- meaning I cannot keep the 2 ".save";s. But if I comment out one of the ".save", the whole code does not work correctly, for example, there would be an error msg if the file is saved under a new name???



Regarding the going into debug. Please....you ALSO got an error, didn't you? When posting, whenever you get errors, and error messages, you should always state so. Did you get a 4605 error? I did state I had an error msg from my earlier post:
"When I played around with the ".save", it either disrupted my other tagged along subs, or it gave me a debugging message."

Again, I'm very unfamiliar with Word vba, and this task is the biggest challenge I can so far with my project. Do you suggest a workaround macro to achieve the same purpose. I still look forward to a solution - Please help.

Nee

lucas
04-13-2007, 09:51 AM
Nee,
your code cannot tell it to .save in the document close statment anywhere or it will save......document close will kick in evey time you close the document and save if you have .save in one or 2 or 3 places....

.save in the document close statement will save the document when you close it.....you should have none in the document close statement if you don't want it to save when you close it.....

when reporting errors you need to say exactly what they are.....are you aware of how many errors can be reported.....help Gerry help you....read what he tells you. Don't just scan over it when your told that .save in document close is saving your document when you close it and then say you tried to take one of the .save statments out and can't understand whey it doesn't work....

joelle
04-13-2007, 10:22 AM
Hello Lucas, Gerry & readers,


Nee,
your code cannot tell it to .save in the document close statment anywhere or it will save......document close will kick in evey time you close the document and save if you have .save in one or 2 or 3 places....

.save in the document close statement will save the document when you close it.....you should have none in the document close statement if you don't want it to save when you close it.....

when reporting errors you need to say exactly what they are.....are you aware of how many errors can be reported.....help Gerry help you....read what he tells you. Don't just scan over it when your told that .save in document close is saving your document when you close it and then say you tried to take one of the .save statments out and can't understand whey it doesn't work....
I'm sure above are great hints! But I know I do not have the Word vba level to work on these guides accordingly.

However, my project now has a slightly different target that I hope can get me out of the crazy "bookemdano" and ".save" loop from the original vba.
I still need your great help but I think the change below is more achievable:

1. If macro is disable, user will see a warning text asking them to reopen document with macro enable.

2. If macro is enabled, macro will do the followings:
a) erase the warning line of text and remove the page break to reveal the 1st page of the document.
b) macro will then check to see if the filename is equal to "master-version"; if yes, macro will check the expiration date code and run it accordingly. If filename is <> "master-version", it will not check or run the expiration date code.
(reason is once user saves it with a different name to his harddisk, I "do not" want the expiration date to run from his local copy).

c) Then finally, how do I assign a word macro to an autoshape? (no problem with excel; how come Word is so picky?)

I can handle item [a], I do look forward to any help with items b & c; I really do not know how to do If-then-else in word vba.

I know I was not clear from my earlier posts (except this one :) ) -- I was under big time pressure. No matter what, your help and patience sure stay in me for a long time.

Nee

lucas
04-13-2007, 10:27 AM
Nee,
I apologize if I was short with you. I'm catching a plane in about an hour so I won't be able to follow up on this but it looks like you have laid out your task much better and I have faith that Gerry or one of the other able members will come along and bail you out....

fumei
04-13-2007, 10:38 PM
Oh-oh. Why do you want to assign it to an autoshape? I would not recommend this.

Using a keyboard shortcut, or a icon on a toolbar is better. If you must have a "thing" in the document to fire code, use a ActiveX commandbutton.

"master version"...oh-oh. Could you expand this thought?

You way of having the text asking for the user to reopen with SAecurity changed is OK. Technically, there is nothing wrong with it at all. Of course the user can ignore it... Plus I am not sure about the idea of telling users to change their system security settings. It is not just a matter of macro disable/enable. It is a matter of setting security settings. You do even mention putting the security setrtinbgs BACK. frankly, if I was a network admin...I would nbot like this at all.

However, your stuff works. If Security if Low, then the Document_Open code fires, and the text with the requrest to re-open is removed.

You are going to have to expand on:
the filename is equal to "master-version"

Are you OK on the .Save thing????? Is this settled? I am not sure.

My point re: error messages is that even saying you get one is NOT good enough. For future posts, if you get error messages, you need to tell us that - and the error number, AND what the message is. The only way I knew what you got was to run it and get the error myself.

If you had stated the error I could have told you that somewhere you are running something to unprotect the document incorrectly. Which is...in fact, the case.

Do YOU really think that:
I did state I had an error msg from my earlier post:
"When I played around with the ".save", it either disrupted my other tagged along subs, or it gave me a debugging message."is all that helpful?

If YOU had someone write that to you, would YOU know what the error was, and what its message was?

Not.

fumei
04-13-2007, 10:46 PM
Oh...and assigning a macro to a autoshape is not really tied in with this thread, is it? If it is not (and I can not see how it is), please post as a separate thread. I want to stay on track with the issue of THIS thread.

Hopefully, we can clear this up soon. As I am going away as well. Off to hike in the quiet of the redwoods. No computer.

joelle
04-14-2007, 11:22 AM
Hello Steve & Gerry,

Steve: If you see this msg, thank you for posting to me although you were on your way catching the plane - hope you enjoyed your flight.

Gerry: I'm here in my cube on Saturday but I saw your recent 2 postings (a little spicy but frank) and I feel good again no matter. Thank you for writing detailed posting @ such late pm time. And yes, I admit my weaknesses as inserted below:


Oh-oh. Why do you want to assign it to an autoshape? I would not recommend this.
Using a keyboard shortcut, or a icon on a toolbar is better. If you must have a "thing" in the document to fire code, use a ActiveX commandbutton.
Oh, I just thought Word could use an autoshape like I did for sereral in Excel. But Word is different, so yes, I will take your advice for keyboard shortcut.


You way of having the text asking for the user to reopen with SAecurity changed is OK. Technically, there is nothing wrong with it at all. Of course the user can ignore it... Plus I am not sure about the idea of telling users to change their system security settings. It is not just a matter of macro disable/enable. It is a matter of setting security settings. You do even mention putting the security setrtinbgs BACK. frankly, if I was a network admin...I would nbot like this at all.

However, your stuff works. If Security if Low, then the Document_Open code fires, and the text with the requrest to re-open is removed.
You are going to have to expand on:

Are you OK on the .Save thing????? Is this settled? I am not sure.
I have not gone very far with the ".Save". However, the "bookemdano" and the ".Save" stuffs scare me now; I dont understand them a bit.
So, I would like to use a simpler approach:
step 1: I will save the doc with warning msg there on the very 1st page. step 2: if user opens the doc with macro enabled, macro will,
* unprotect the doc then,
* erase the warning text and page break to reveal the 1st page of the usable doc.
The simpler steps (1&2) above achieve the same purpose for my project, except that I have to do like you pointed out: I have to include and give users info about "Security Level" from my warning message.



"master version"...oh-oh. Could you expand this thought? What I'd like to do is running a vba to automatically check and expire the doc called "master-version" upon a given date in the future - but this is just part 1 of the module. Part 2 is, if user download this doc from the main server, uses this doc and files it under a different name to his harddrive, then I dont want to run expiration check anymore now that the doc belongs him to use as long as he wants (he cannot override the one from the server plus, the plan is to allow him to use his file as long as he wants without an expiration date).



My point re: error messages is that even saying you get one is NOT good enough. For future posts, if you get error messages, you need to tell us that - and the error number, AND what the message is. The only way I knew what you got was to run it and get the error myself.

If you had stated the error I could have told you that somewhere you are running something to unprotect the document incorrectly. Which is...in fact, the case.

Do YOU really think that:is all that helpful? No - as you pointed out, it was messy. I was running short of time but this is not a good excuse [blush]


If YOU had someone write that to you, would YOU know what the error was, and what its message was?
Not. Got it Sir. This wont happen again.
=======================

Right now, my incomplete macro is as shown below which has area that I do really need help with:

Private Sub Document_Open()

' If user enables macro, this macro will unprotect the doc, erase the
' warning line of text, and remove the pagebreak to reveal the
' usable/saveable doc. It will also check for expiration date if the doc being
' used is downloaded from the server and if user chooses to save with the
' same name "master-version". Else, macro will not run the expiration code.

ActiveDocument.Unprotect Password:="abc"
Selection.MoveRight Unit:=wdCharacter, Count:=89, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=4, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1

' Gerry -- I need help with the If-then-else here.
' How do I say: If filename is not equal to "master-version", do nothing,
' otherwise, do the expiration check below.

Dim exdate As Date
exdate = "12/31/2008"
If Date < exdate Then
Exit Sub

Else
MsgBox "This template has been expired. Please use the current Template posted in Folder-A."

With ActiveDocument
.Password = "abc"
.Close
End With

End If
End Sub

-------------------------
Many many thanks.
nee
PS: Oh - if big dog goes hiking, small dog goes suffering. Kidding, hope you have a great time and you could collect more tennis balls on the way.

fumei
04-14-2007, 05:24 PM
' Gerry -- I need help with the If-then-else here.
' How do I say: If filename is not equal to "master-version", do nothing,
' otherwise, do the expiration check below.It sounds like straight-forward logic to me.

If ActiveDocument.Name <> "master version.doc" Then

Is the file actuually named master version.doc? Whatever. Note that .Name is the name of the file, and does NOT include the path. This may be important for you, as you seem to want to verify a specific file. If the user saved the file to their local drive, and kept the name master version.doc, .Name will be....master version.doc.

If path and name is important, then use .FullName for your verification logic.

I have to tell you though, as an attempt at verifying this is weak. As I pointed out, the user can simply ignore the suggestion to change the security settings and reopen. What I am saying is that I think I understand what you are trying to do, but it is a weak process. It can be very easily worked around.

joelle
04-16-2007, 08:45 AM
I have to tell you though, as an attempt at verifying this is weak. As I pointed out, the user can simply ignore the suggestion to change the security settings and reopen. What I am saying is that I think I understand what you are trying to do, but it is a weak process. Gerry: Thank you for understanding my purpose for this project, and for your patience this far.
You pointed this whole process is a weak one - of course you're exactly right again. I was worn out on Sat with too much thinking about this that I totally missed the simple point above, but afterwards, I was getting too weak myself to edit my last posting. I actually went blank and did not know which direction to head to.


It can be very easily worked around. Sounds like a light at the end of the tunnel to me. What is that easy workaround if you dont mind to donate?

Also, I think I should close this post at this point. If I'm luck enough to receive a workaround from you or another poster, then it has very little if nothing to do with the original code & questions of this thread.
nee