Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Repeat Script until user answers no.

Status
Not open for further replies.

cbsarge

IS-IT--Management
Jun 20, 2001
219
US
If this script is run prior to opening a word doc it pops a little box asking if you want to save your doc. I want it to repeat every 10 or 15 minutes (sleep?) prompting the user to save their work. Since there is no autosave function in Office (only autorecover for when it crashes) I'm trying to make a script to do just that. I could have it monitor the other Office apps too to cover all the bases.

Code:
strComputer = "." 
 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 
 Set colProcesses = objWMIService.ExecNotificationQuery("SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_Process'") 
 Do Until DefResp = vbNo 
      Set objNewProcess = colProcesses.nextEvent 
      If objNewProcess.TargetInstance.Name = "WINWORD.EXE" THEN  
      set WshShell = WScript.CreateObject("WScript.Shell") 
      DefResp = MsgBox (MyNum & " Do you want to save now?", vbYesNo) 
      WshShell.SendKeys "^s"
	  End If 
 Loop
 
Maybe try changing this

"SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_Process'"

to

"SELECT * FROM __InstanceCreationEvent WITHIN 15 WHERE TargetInstance ISA 'Win32_Process' Amd TargetInstance.Name='WINWORD.exe'"

Then you can get rid of this If...Else and simply keep your msgBox.
If objNewProcess.TargetInstance.Name = "WINWORD.EXE" THEN
...
End If

Put a WScript.Sleep in your Do...Loop too of about 15 seconds

Also, move this line to right before the loop...no need to recreate this object each time.

set WshShell = WScript.CreateObject("WScript.Shell")


--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
O.k. - I decided to try doing this a little differently. I had a backup script that runs whenever someone logs into the domain. I am now trying to modify it to use a send key command to do a ctrl-s every 10 minutes. This will (in most cases) save whatever the user has open. I am still struggling trying to figure out how to get the script to run, sleep for 10 minutes and then run again until the user says "No".

Here is the script without the looping part:
Code:
On Error Resume Next

Dim WshShell, BtnCode

Set WshShell = WScript.CreateObject("WScript.Shell")

BtnCode = WshShell.Popup("If you would like to backup your active document every 10 minutes click Yes now.", 10, "Brian's Auto Save Utility", 4 + 32)

Select Case BtnCode

   case 6      	set WshShell = WScript.CreateObject("WScript.Shell")
				WshShell.SendKeys "^s"
   				
   case 7      WScript.Echo "No auto saving will be done"

   case -1     WScript.Echo "Auto Save aborted due to user time out!"
 
End Select
 
Dim strResponse

Do Until strResponse = vbNo
strResponse = MsgBox(MyNum & " Do you want to save now?", vbYesNo)
Loop

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
What application are you useing? You said
Since there is no autosave function in Office
but this is not correct. This is native functionality to Office.

Within Excel you need to select to add AutoSave through the Add-In manager. Word has it. Tools, Options, Save Tab. Select to allow Quick Saves and always create a backup copy.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
In Office 2003 there is an Auto Recover function but, that only saves a document that can be restored if the application crashes. these docs are deleted when the application is gracefully closed. I looked at the Automation add-ins for Excel and didn't seen any Autosave add-ins.

ctrl+s is pretty universally used as a shortcut to save stuff so that's why I decided to trudge down this path.
 
This is where I'm at so far
Code:
On Error Resume Next

Dim WshShell, BtnCode

Set WshShell = WScript.CreateObject("WScript.Shell")

BtnCode = WshShell.Popup("If you would like to backup your active document every 10 minutes click Yes now.", 10, "Brian's Auto Save Utility", 4 + 32)

Select Case BtnCode

case 6      	Do Until strResponse = vbNo
				strResponse = MsgBox(MyNum & "Do you want to save now and keep running Brian's Auto Save script? If you answer No your data will be saved one last time before the script ends.", vbYesNo, "Brian's Auto Save Utility")
				set WshShell = WScript.CreateObject("WScript.Shell")
				WshShell.SendKeys "^s"
				WScript.Sleep 600000
				Loop
   case 7      WScript.Echo "No auto saving will be done."

   case -1     WScript.Echo "Auto Save aborted due to user time out!"
 
End Select

This is very close. I'd like it to not save the one last time if the user says "No" but I can't figure out the mechanics to have the Ctrl-s fire when the loop repeats and not when the loop will be ending.
 
If strResponse = vbNo Then Exit Do

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
This is the final version that I'm pretty happy with. It askes you if you want to run the script and have it backup your active document every 10 minutes. If the user starts the script and doesn't anser yes or no it times out and lets the user know it isn't auto saving. If the user answers yes it waits 10 minutes and then saves the active document. If the user answers no it ends and lets the user know it wont be auto saving.

Code:
On Error Resume Next

Dim WshShell, BtnCode

Set WshShell = WScript.CreateObject("WScript.Shell")

BtnCode = WshShell.Popup("If you would like to save your active document every 10 minutes click Yes now.", 10, "Brian's Auto Save Utility", 4 + 32)

Select Case BtnCode

   case 6      	set WshShell = WScript.CreateObject("WScript.Shell")
				WScript.Sleep 60000
				WshShell.SendKeys "^s"
				Do Until strResponse = vbNo
				strResponse = MsgBox(MyNum & "Your ACTIVE document has been saved. Do you want to autosave again in 10 minutes?", vbYesNo, "Brian's Auto Save Utility")
				Loop
   case 7      WScript.Echo "No auto saving will be done."

   case -1     WScript.Echo "Auto Save aborted due to user time out!"
 
End Select

This of course only saves whatever document currently has focus and counts on ctrl+s being the save shortcut.

Many thanks to all who helped!
 
Argh - scratch that previous post - it didn't work right. This version works.
Code:
On Error Resume Next

Dim WshShell, BtnCode

Set WshShell = WScript.CreateObject("WScript.Shell")

BtnCode = WshShell.Popup("If you would like to save your active document every 10 minutes click Yes now.", 10, "Brian's Auto Save Utility", 4 + 32)

Select Case BtnCode

   case 6      	set WshShell = WScript.CreateObject("WScript.Shell")
				Do Until strResponse = vbNo
				WScript.Sleep 600000
				WshShell.SendKeys "^s"
				WScript.Echo "Your active document has been saved."
				strResponse = MsgBox(MyNum & "Do you want to autosave again in 10 minutes?", vbYesNo, "Brian's Auto Save Utility")
				Loop
   case 7      WScript.Echo "No auto saving will be done."

   case -1     WScript.Echo "Auto Save aborted due to user time out!"
 
End Select
 
The AutoRecover option (in these Microsoft Office programs: Word, Excel, PowerPoint, Publisher, and Visio) and AutoSave option (in Microsoft Office Outlook) can help you avoid losing work in two ways:

Your data is automatically saved If you enable AutoRecover or AutoSave, your file (such as a Microsoft Office Word document) or item (such as an Outlook e-mail message) is automatically saved as often as you want. Therefore, if you have been working for a long time but forget to save a file or if your power goes out, the file you have been working on contains all or at least some of the work you have done since you last saved it.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Sorry but, there is no Autosave in Office 2003 - at least not one that I can find. It looks like they have built it into Office 2007 but, we havent migrated to it yet.
 
That is only for the autorecovery function not autosave.
 
O.k. - this version looks for the winword.exe process but, if a different application has focus it does the save in that app - not word. It still sees that winword.exe is running but, I havent figured out how to get the sendkeys to target a specific process. If I can figure out how to target winword.exe then I could also have it look for excel.exe and the rest of the office 2003 apps.

Anyone have any ideas?

Code:
Do Until DefResp = vbNo
	strComputer = "." 
	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 
	Set colProcesses = objWMIService.ExecNotificationQuery("SELECT * FROM __InstanceModificationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_Process' AND TargetInstance.Name='WINWORD.exe'") 
      Set objNewProcess = colProcesses.nextEvent 
      set WshShell = WScript.CreateObject("WScript.Shell")
		WScript.Sleep 600000
		WshShell.SendKeys "^s"
		WScript.Echo "The Word document has been automaticly saved."
DefResp = MsgBox (MyNum & " Do you want to save again in 10 minutes?", vbYesNo) 
Loop
 
1. Using ExecNotificationQuery in this case may not be what you want. A regular WMI query should suffice.
2. You should move the WMI connection outside your loop
3. You should move your WScript.Shell outside the loop too.
4. You might be able to use WshShell.AppActivate to bring focus to the word document, but it is not very reliable.

Will there only be one Word doc open at any given time or several?

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Something like this may work for you and be more reliable than using sendkeys.

Code:
Option Explicit

Dim strComputer : strComputer = "."
Dim objWMIService : Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Dim colItem, DefResp, MyNum, objWord, objDoc

Do Until DefResp = vbNo
		Set colItem = objWMIService.ExecQuery("Select * From Win32_Process Where Name = 'WINWORD.EXE'")
		If colItem.Count > 0 Then
			Set objWord = GetObject(,"Word.Application")
			For Each objDoc In objWord.Documents
				objDoc.Save
			Next
		End If
		WScript.Sleep 600000
		WScript.Echo "The Word document has been automaticly saved."
		DefResp = MsgBox (MyNum & " Do you want to save again in 10 minutes?", vbYesNo)
Loop

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Ok - I decided to revisit this since a user where I work lost some Excel data and for some reason, ven though it IS enabled, the auto recover function of Excel didn't work when excel closed. This is the same script I was working on before only for Excel. I added a prompt for how long between saves it should wait. What I can't figure out is how to have the Save dialog come up so if the user want's to save to a different location or with a different name they could do so.

Here is the current working version:
Code:
On Error Resume Next

Dim WshShell, BtnCode

Set WshShell = WScript.CreateObject("WScript.Shell")
'This checks to see if excel is running, if not it opens it
				Set objexcel = GetObject(, "Excel.Application")
				If TypeName(objexcel) = "Empty" Then
				WScript.Quit + WScript.Echo("Excel is not running - Utility will close.")
				End If
'*******************************************************************
'Asks user if they want to save the document every X minutes - 60000 is 1 minute
'Case 6 is if the user answers yes
'Case 7 is if the user answers no
'Case -1 is if the user doesn't answer and the script times out after 60 seconds
'*******************************************************************
Set colItem = objWMIService.ExecQuery("Select * From Win32_Process Where Name = 'EXCEL.EXE'")
'*******************************************************************
'*******************************************************************
BtnCode = WshShell.Popup("If you would like to be prompted to Save your Excel documents every 10 minutes click Yes now. Brian's Excel-Auto-Save Utility will ask you for confirmation before saving your Excel documents. If you answer Yes all your open Excel documents will be saved. When you answer No, Brian's Excel-Auto-Save Utility will stop running. You can start it again at any time.", 60, "Brian's Excel-Auto-Save Utility", 4 + 64)
'*******************************************************************
'Prompts user for how many minutes they would like between saves

Select Case BtnCode

   case 6      	strMinutes = InputBox("Enter the number of minutes between saves:") * 60000
				Do Until DefResp = vbNo
				WScript.Sleep strMinutes
				Set objExcel = GetObject(,"Excel.Application")
				DefResp = MsgBox (MyNum & " Do you want to save all you Excel documents now and continue Auto Saving? If you answer No the Utility will close and not ask you again.", vbYesNo)
				'If user answers no to the prompt the script will end
				If DefResp = vbNo Then
				WScript.Quit
				'Saves all the open Excel documents
				End If
				If colItem.Count > 0 Then
				For Each objXls In objExcel.Workbooks
				objXls.Save
				Next
				End If
				Loop
'*******************************************************************
	'Ends the script if the user answered No to running the script in the first place
	case 7      WScript.Echo "No prompting for saving will be done. Brian's Excel-Auto-Save Utility will now close."
'*******************************************************************
	'Ends the script if the user doesn't answer the prompt at the beginning of the script
   case -1     WScript.Echo "Brian's Excel-Auto-Save Utility aborted due to user time out!"
End Select


Thanks!
 
Use the GetSaveAsFilename method of the Excel.Application object.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
This is a vbscript file. Isn't that for VBA? How would I use that method in my vbs file?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top