Link to home
Start Free TrialLog in
Avatar of mxgrogg
mxgrogg

asked on

Auto Close a Microsoft Word document with a VB MACRO

Hello all!
I am running XP Pro and Microsoft Word 2002 SP3.

I have been trying for a long time (three hours) to make a VB Macro that will <b>Save and Close</b> this particular Microsoft Word document after "x" time of typical document inactivity.  (This is a very sensitive docuement)

I know this is an easy script, but I am a NewB at VB and Macros, so it is kicking my butt.  

Points will be awarded to the script that
1) Comes with the most comprehensive installation instructions (i am very, VERY NewB)
2) Functions Correctly

Thank you all for your help!
-mxgrogg-
ASKER CERTIFIED SOLUTION
Avatar of mvidas
mvidas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mxgrogg
mxgrogg

ASKER

Wow, that was the most comprehensive answer I have ever received.  Bravo!

In trying to teach myself and understand the script, I cannot see where it also "Saves before it closes" the application.  

What part of your code indicates this?
-mxgrogg-
Well, you did ask for comprehensive instructions :)

The line that does that is the line:
   ThisDocument.Close True

In the block:
  WaitForIdle
  If bCancel = False Then
   bCancel = True
   ThisDocument.Close True
  End If

"ThisDocument" refers to the document the macro is in, and the .Close after it is what closes the file. The "True" after that is for an option argument "SaveChanges", I could have also written the line like:
   ThisDocument.Close SaveChanges:=True

That one line would be the same as typing:
   ThisDocument.Save
   ThisDocument.Close

The WaitForIdle function is what is always running; it checks to see if there is any keyboard or mouse movement. It keeps looping while there is movement, and once it goes idle it checks against the time of last movement. Once the difference is greater than the desired wait time, it leaves the function, which then progresses to the above codeblock.

Matt
Avatar of mxgrogg

ASKER

I just implemented it.
It works perfectly, and your instructions are exactly what I needed. Nothing missing

You truly are a WIZARD.
I bow my three headed, 1000 hit point, swamp dragon to your glory.
-mxgrogg-
Glad to help! The only thing that you *may* want to do is add a "digital signature" to the macro, so the users wont always have to hit Enable Macros (they can Trust the macro source). If you're interested in this, search on google for "selfcert.exe", it is an easy process and there are plenty of articles out there about it

Let me know if you need anything else!
matt