
Originally Posted by
tben2505
LosOjos, I don't suppose I could be particularly fussy and ask for this script to process the files not in alphabetical order, but in Date Modified Order (earliest to latest) or just in the order that they are in the folder to begin with?
Sorry about the epic delay in replying to you, but I've just not had time to look at it until now.
I believe this will do the trick:
Code:
'==========================================================================
'
' NAME: Word XML to DOC [By Date].vbs
'
' AUTHOR: Josh Johnson
' DATE : 04/02/2011
'
' COMMENT: Convert SIMS XML files in to XLS files [date sorted]
' HOW TO USE: Drag a folder containing the XML files onto the script.
'==========================================================================
Option Explicit
On Error Resume Next
Const adDate = 7
Const adVarChar = 200
Dim Arg 'Script argument
Dim FSO 'File System Object
Dim oFol 'Folder object
Dim oFil 'File object
Dim oFils 'Files
Dim myWord 'Word object
Dim myDoc 'Document object
Dim filePath 'Path to file (need for Word to open the file)
Dim rsFolder 'recordset to sort folder
'Check arguments
If WScript.Arguments.Length < 1 Then
MsgBox "Drag a folder containing Word XML files onto this script", vbOKOnly, "Error!"
WScript.Quit
End If
Set FSO = CreateObject("Scripting.FileSystemObject")
Arg = WScript.Arguments(0)
If FSO.FolderExists(Arg) = False Then
MsgBox "Drag and drop a folder onto this script to print the contents."
Set FSO = Nothing
WScript.Quit
End If
Set rsFolder = CreateObject("ADODB.Recordset")
'Get folder informaton
Set oFol = FSO.GetFolder(Arg)
With rsFolder.Fields
.Append "Path", adVarChar, 200
.Append "Name", adVarChar, 200
.Append "DateLastModified", adDate
End With
rsFolder.Open()
Set oFils = oFol.Files
For Each oFil in oFils
rsFolder.AddNew
rsFolder("Path") = oFil
rsFolder("Name") = oFil.Name
rsFolder("DateLastModified") = oFil.DateLastModified
Next
rsFolder.Sort = "DateLastModified ASC"
Set myWord = CreateObject("Word.Application")
myWord.Visible = True
myWord.Application.DisplayAlerts = False
rsFolder.MoveFirst()
While Not rsFolder.EOF
Set myDoc = myWord.Documents.Open(rsFolder("Path").Value, True, True, False, , , False, , , 8, , True)
myDoc.SaveAs Left(rsFolder("Path").Value, Len(rsFolder("Path").Value)-4) & ".doc", 0
myDoc.Close
FSO.DeleteFile rsFolder("Path").Value
If Err <> 0 Then MsgBox "Error converting " & rsFolder("Name").Value & vbNewLine & Err.Description, vbOKOnly, "Error"
Err.Clear
Set myDoc = Nothing
rsFolder.MoveNext()
Wend
myWord.Application.Quit
Set myWord = Nothing
Set oFils = Nothing
Set oFol = Nothing
MsgBox "Complete!", vbOKOnly, "Converting done"
Function strClean (strtoclean)
Dim objRegExp, outputStr
Set objRegExp = New Regexp
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "[(?*"",\\<>&#~%{}+_.@:\/!;]+"
outputStr = objRegExp.Replace(strtoclean, "-")
objRegExp.Pattern = "\-+"
outputStr = objRegExp.Replace(outputStr, "-")
strClean = outputStr
End Function EDIT: Just updated the code, there was a major error in it!