Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim objXmlDom
Set objXmlDom = CreateObject("Microsoft.XMLDOM")
Dim objWord
On Error Resume Next
Set objWord = GetObject(,"Word.Application") '已有word运行则直接得到对象
If Err.Number <> 0 Then
Err.Clear
Set objWord = CreateObject("Word.Application")
If Err.Number <> 0 Then
If UCase(fso.GetBaseName(WScript.FullName)) = "CSCRIPT" Then
WScript.StdErr.WriteLine "老刘的温馨提示:Word的COM自动化组件无法创建!"
Else
MsgBox "Word的COM自动化组件无法创建!",vbQuestion,"老刘的温馨提示"
End If
WScript.Quit 1
End If
End If
On Error Goto 0
Dim objWordDocument,strArg,objXmlDocument,objProtecctionNodes,objProtecctionNode
For Each strArg In WScript.Arguments
Rem 文档→XML。
With objWord
Set objWordDocument = fso.GetFile(strArg)
.ChangeFileOpenDirectory objWordDocument.ParentFolder & "\"
.Documents.Open objWordDocument.Name, False, True
.ChangeFileOpenDirectory Fso.GetSpecialFolder(2) & "\"
.ActiveDocument.SaveAs2 _
fso.GetBaseName(objWordDocument.Path)&".XML", _
wdFormatFlatXML
.ActiveDocument.Close
Set objXmlDocument = _
fso.GetFile( _
fso.GetSpecialFolder(2) & "\" & _
fso.GetBaseName(objWordDocument.Path) & ".XML" _
)
End With
Rem 去除w:documentProtection节点。
With objXmlDom
.load objXmlDocument.Path
Set objProtecctionNodes = .getElementsByTagName("w:documentProtection")
If objProtecctionNodes.length = 0 Then
If UCase(fso.GetBaseName(WScript.FullName)) = "CSCRIPT" Then
WScript.StdErr.WriteLine objWordDocument.Name&" 未被限制编辑"
End If
Else
For Each objProtecctionNode In objProtecctionNodes
objProtecctionNode.parentNode.removeChild(objProtecctionNode)
Next
End If
.save objXmlDocument.Path
.abort
End With
Rem XML→DOCX。
With objWord
.Documents.Open objXmlDocument.Name
.ChangeFileOpenDirectory objWordDocument.ParentFolder & "\"
.ActiveDocument.SaveAs2 _
"[Unlocked]"&fso.GetBaseName(objWordDocument.Path)&".DOCX", _
wdFormatXMLDocument
.ActiveDocument.Close
objXmlDocument.Delete True
End With
Next
objWord.Visible = True
Set fso = Nothing
Set objXmlDom = Nothing
Set objWord = Nothing '该操作不会关闭Word
WScript.Echo "老刘的提醒:由于要保证处理速度(开关WORD太费时),不使用时请手动退出WORD。"