How can I save all open word documents using vbscript, wmi or whatever

advcom
advcom used Ask the Experts™
on
I need a script that will run all the time and automatically save any open word documents to a specific path. There are quite a few programs to do this, but I want to add my own features. I would prefer to use vbscript, wmi or vb.net, but I am open to other options.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Set objW = GetObject(, "Word.Application")
Set docs = objW.Documents
WScript.Echo(docs.Count)
For Each doc in docs
 WScript.Echo(doc)
 doc.Save
Next
Top Expert 2009

Commented:
To save the documents to a specific path, you need a way to select that path (or hard-code it).  Here is a FolderSelector function that uses the Office FileDialog object:
Public Function SelectFolder() As String
'Requires Office XP (2002) or higher
'Requires a reference to the Microsoft Office Object Library
'Created by Helen Feddema 3-Aug-2009
'Last modified 3-Aug-2009

On Error GoTo ErrorHandler

   Dim strFolderPath As String
   Dim fd As Office.FileDialog
   Dim strPath As String

   'Create a FileDialog object as a Folder Picker dialog box.
   Set fd = Application.FileDialog(msoFileDialogFolderPicker)

   'Set strPath to the folder you want to open initially
   strPath = "G:\Data"

   With fd
      .Title = "Browse for folder where _________ are located"
      .ButtonName = "Select"
      .InitialView = msoFileDialogViewDetails
      '.InitialFileName = strPath
      If .Show = -1 Then
         strFolderPath = CStr(fd.SelectedItems.Item(1)) & "\"
      Else
         Debug.Print "User pressed Cancel"
         strFolderPath = ""
      End If
   End With

   SelectFolder = strFolderPath
   
ErrorHandlerExit:
   Exit Function

ErrorHandler:
   MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
   Resume ErrorHandlerExit

End Function

Open in new window

Top Expert 2009

Commented:
Also, you may want to reconsider what you mean by saving all the time.  Once the script is created and tested, you could run it from the Windows Scheduler at a regular interval, say every 15 mins.

Author

Commented:
Thanks jmatix for the code, that worked fine, had to modify to use saveAs.
Thanks Helen Feddema for the code, I probably will not sue it for this situation, but will be useful in other situations.

One more question... I would like it to retrieve the full path instead just the name of the open docs. Any way to do that? If I had that, I could keep the directory structure in the backup folder, so that if there were two docs with the same name, but in different folders, it would be easy to know which one is which.


Set oWord = GetObject(, "Word.Application")
WScript.Echo(oWord.Documents.Count)
For Each doc in oWord.Documents
	WScript.Echo(doc)
	oWord.Documents(doc).SaveAs "x:\WordDocs\" & doc
Next

Open in new window

path = doc.Path & Application.PathSeparator &  doc.Name

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial