PDA

View Full Version : Solved: Time Bomb codes for Word - Need help!



joelle
12-27-2005, 05:18 PM
Hello Experts,

Please how do I set some vba code to expire a Word document. What I mean is, when people open my word document that is older that 90 days from the date created, the word document will self protect with a password (says, mydog) and they will see a msg box saying,"Document has expired".
I can do some basic vba with excel, but I frankly know nothing about Word vba so if you can donate some code, I'm so appreciative.

Nee

fumei
12-27-2005, 07:34 PM
You have a conceptual problem. If they HAVE opened the document...well, it IS opened.

You could certainly write code that checks dates and then protects itself. But it must save itself before that protection will work (that is, prevent people from opening the document). How could it be otherwise?

Although.......I just thought of a sneaky way to do something like what you are looking for.

What is your document like? Does it have forms in it?

fumei
12-27-2005, 08:17 PM
Actually, on second thought (always a good idea), this is very straightforward.
Private Sub Document_Open()
Dim oDate As Date
oDate = ActiveDocument.BuiltInDocumentProperties("Creation date")
If Now > oDate + 90 Then
MsgBox "Document has expired."
With ActiveDocument
.Password = "mydog"
.Save
.Close
End With
End If
End Sub
Put this as the Document_Open event in the ThisDocument module.

joelle
12-28-2005, 09:43 AM
Hello Gerry,

Thanks for the postback. And I'm sorry that I complicated the situation earlier: I actually would like to code in a date in the module as the one I have below for excel.

Sub Auto_Open()
Dim exdate As Date
exdate = "07/14/2005"
If Date < exdate Then Exit Sub
Application.DisplayAlerts = False
(more) .....

But as said, I know nothing about Word module.
So please how to change the Word code a bit so:
1. it has a set date of today's date (so I can test)
2. it has an error check and exit the Sub as can be done with excel

btw, it is just regular Word document but the purpose is to force employees to download the current version from our intranet (some are lazy and keep using the older version and they created confusion when the doc was sent out to customers).

As you can see, you are my lifesaver if I can achieve this purpose. http://vbaexpress.com/forum/images/smilies/notworthy.gif
Nee

Ken Puls
12-28-2005, 10:01 AM
Hi Nee,

Just melding your and Gerry's code:

Private Sub Document_Open()
Dim exdate As Date
exdate = "07/14/2005"
If Date < exdate Then
Exit Sub
Else
MsgBox "Document has expired."
With ActiveDocument
.Password = "mydog"
.Save
.Close
End With
End If
End Sub

HTH,

joelle
12-28-2005, 10:15 AM
Hello Kpuls,

Many thanks for stopping by and for the codes. I already plugged it in as today's date and I can't wait see it expire tomorrow (very exciting!)

Thank you both.

Nee

Ken Puls
12-28-2005, 10:22 AM
LOL!

Nee, why not just make a second file and put yesterday's date in? Then you don't have to wait. ;)

joelle
12-28-2005, 10:33 AM
Hello Ken,

You did have reason to LOL ! I cant barely hold from laughing at myself!

I'm more shallow that you can measure. I thought about it, tried yesterday's date and reopen the doc, but nothing happened. Therefore I set one for today.

Now that I wanna do it again I cannot find the Document_Open macro. It is a Private Sub and not a regular macro sub and so I dont know how to locate it. Pretty shallow rite?
Please land a hand!

Nee

Ken Puls
12-28-2005, 10:42 AM
No problem, Nee.

It should be in the ThisDocument module for the Document. And make sure it's NOT in the Normal one.

:)

joelle
12-28-2005, 10:58 AM
Ken,

Now I'm pulling my hair ...
What did I miss from these:

1. Open my Word doc
2. Alt+F11
3. Click ThisDocument (under Project) then Insert/Module
4. Paste the code and change date to 12/26/2005
5. Save the document

6. Reopen the doc and see nothing happens ..

I truly appreciate your patience and pls shed some light.

Nee

Ken Puls
12-28-2005, 11:03 AM
Hi Nee,

1. Open the Word doc
2. Alt+F11
3. Double click ThisDocument (under Project)
4. Paste the code in the ThisDocument module and change date to 12/26/2005
5. Save the document
6. Reopen the doc

The code must go into the ThisDocument (class) module, not a regular code module.

Give it a shot and let me know. :)

joelle
12-28-2005, 11:12 AM
Hello Ken,

Very fast turnaround - as if you're from the next cube -- thanks!

I did as told but nothing happens -- does this have to do with the save time?
I plugged in and save the module as of today?

Nee

Ken Puls
12-28-2005, 11:17 AM
Nope... the save date shouldn't make any issue at this point.

I've uploaded a sample file, which is working fine for me. Give this a shot and see.

You're sure that macros are running, correct?

joelle
12-28-2005, 11:29 AM
Ken,

It must be my system or what else?

I opened your file and I did not see anything -- instead there was a "control toolbox" on my screen (the whole set of it like when you wanna create a combo box ..)

Feeling awkward right now to bug you to this extend .. dunno what to say anymore.

Nee

Ken Puls
12-28-2005, 11:32 AM
Aha!

On the Control Toolbox, is the first icon (the triangle, ruler & pencil) clicked? If so, you are in design mode. You'll want to try turning that off, then trying again. Personally, I'd shut off design mode, close Word all together, and then try.

Let me know,

joelle
12-28-2005, 12:00 PM
Aha!

On the Control Toolbox, is the first icon (the triangle, ruler & pencil) clicked? If so, you are in design mode. You'll want to try turning that off, then trying again. Personally, I'd shut off design mode, close Word all together, and then try.

Ken,

You pinpointed it -- thats the one that was clicked!!
It took me a bit long because I did as told:
* turn it off from the doc screen
* go to tools/customize and turn it off from there
* save the doc
* exit everything, plus
* turn our my system (reboot)

* turn system on
* reopen the "test" doc
* then my gosh, the control toolbox's still there and already clicked!!!

how can I kill it once and for all?

needless to say ... IOU big http://vbaexpress.com/forum/images/smilies/notworthy.gif

Nee

Ken Puls
12-28-2005, 12:06 PM
I'm stumped...

You shouldn't need to actually get rid of the toolbar, just make sure that design mode isn't on. (If you hold your mouse over that button, it should say "Design Mode", not "Exit Design Mode". Click it once if it says the latter.)

On my system, if I close Word, it reopens with design mode off...

I'm nto sure where to go now... Try just this:
-open the doc
-unclick design mode (don't get rid of the toolbars)
-close the doc
-reopen it

What version of Word are you running, and what is your macro security level set to?

joelle
12-28-2005, 12:22 PM
What version of Word are you running, and what is your macro security level set to?

I was about to ask you to quit on me because of course you are helping others to0, and hanging up your time makes me feel BAD ..

But -- a thousand thanks for pointing that out the above!!
my macro security was set to "high" and trust source only (blank from trust source window)

I reset it to "medium" and BAM - works!

Ken, again, excellent help from you and excellent PATIENCE level!!!

Wish I could deliver to you some packs of Heineken!!

Many many thanks.

Nee http://vbaexpress.com/forum/images/smilies/notworthy.gif http://vbaexpress.com/forum/images/smilies/beerchug.gif http://vbaexpress.com/forum/images/smilies/notworthy.gif http://vbaexpress.com/forum/images/smilies/beerchug.gif http://vbaexpress.com/forum/images/smilies/notworthy.gifhttp://vbaexpress.com/forum/images/smilies/notworthy.gifhttp://vbaexpress.com/forum/images/smilies/notworthy.gif

Ken Puls
12-28-2005, 12:56 PM
Ken, again, excellent help from you and excellent PATIENCE level!!!

Wish I could deliver to you some packs of Heineken!!

:rotlaugh:

Not a problem, Nee. Glad you got it working!

Cheers!

Zack Barresse
12-28-2005, 03:10 PM
You can deliver them to me! I'm closer - and between you two! :D

joelle
12-28-2005, 05:00 PM
Wow, Zack, I know you're a fireman not a fisherman, right?

No problem, so long as the beers can be RE-delivered to Ken, deal!

Anyway, dont wanna be hard on you. I OWE you too! (since Ken is too far http://vbaexpress.com/forum/images/smilies/rotlaugh.gif)

Happy Holidays!

Nee