' ** 根据脚本的名称进行对应转换, 如可将脚本命名为:Word2Txt.vbs, Word2Doc.vbs, Word2Docx.vbs, Word2Html.vbs, 如为其它脚本名称则转换为 .txt**
' * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = *
' * 批量 Word 格式转换工具, BY: hongqingfa 2023-09-10 *
' * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = *
wdFormatDocument97=0 'Microsoft Word 97 document format.
wdFormatDocument=0 'Microsoft Office Word 97 - 2003 binary file format.
wdFormatDOSText=4 'Microsoft DOS text format.
wdFormatRTF=6 'Rich text format (RTF).
wdOpenFormatWebPages=7 'HTML 格式。
wdFormatXMLDocument=12 'XML document format.
wdFormatOpenDocumentText=23 'OpenDocument Text format.
'
'wdOpenFormatAuto 0 现有格式。
'wdOpenFormatDocument 1 Word 格式。
'wdOpenFormatDocument97 1 Microsoft Word 97 文档格式。
'wdOpenFormatTemplate97 2 Word 97 模板格式。
'wdOpenFormatTemplate 2 用作 Word 模板。
'wdOpenFormatRTF 3 RTF 格式。
'wdOpenFormatText 4 未编码的文本格式。
'wdOpenFormatEncodedText 5 编码文本格式。
'wdOpenFormatUnicodeText 5 Unicode 文本格式。
'wdOpenFormatWebPages 7 HTML 格式。
'wdOpenFormatXML 8 XML 格式。
'wdOpenFormatXMLDocument 9 XML 文档格式。
'wdOpenFormatXMLDocumentMacroEnabled 10 启用了宏的 XML 文档格式。
'wdOpenFormatXMLTemplate 11 XML 模板格式。
'wdOpenFormatXMLTemplateMacroEnabled 12 启用了宏的 XML 模板格式。
'wdOpenFormatAllWordTemplates 13 Word 模板格式。
'
' * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = *
'
Set objArgs = WScript.Arguments
If objArgs.Count=0 Then
MsgBox "本脚本 支持 .doc/.docx/.txt/.html 间的转换," &vbCrLf& "请将您要转换的文件拖到 本脚本!" &vbCrLf& "" &vbCrLf& "拖拽批量 Word 格式转换工具, BY: HongQingFa"
MsgBox "根据脚本的名称进行对应转换," &vbCrLf& "如可将脚本命名为:" &vbCrLf& "Word2Txt.vbs" &vbCrLf& "Word2Doc.vbs" &vbCrLf& "Word2Docx.vbs" &vbCrLf& "Word2Html.vbs" &vbCrLf& "-------" &vbCrLf& "-其它脚本名称-,则转换为 .txt"
End If
'
' 根据本脚本的名称进行对应转换,其它命名格式则转换为 .txt
scp_fn=UCase(wscript.scriptname)
For I = 0 To objArgs.Count - 1
FileUrl = objArgs(I)
' 根据脚本的名称进行对应转换,其它命名格式则转换为 .txt
' Call Word2Txt(FileUrl)
if scp_fn = UCase("Word2Txt.vbs") Then
Call Word2Txt(FileUrl)
elseif scp_fn = UCase("Word2Doc.vbs") Then
Call Word2Doc(FileUrl)
elseif scp_fn = UCase("Word2Docx.vbs") Then
Call Word2Docx(FileUrl)
elseif scp_fn = UCase("Word2Html.vbs") Then
Call Word2Html(FileUrl)
else
Call Word2Txt(FileUrl)
End If
Next
Function Word2Html(FileUrl)
FilePath = GetFileBaseName(FileUrl) & ".html"
Set objWord = CreateObject("Word.Application")
Set wordapp = objWord.Documents.Open(FileUrl, False, True)
wordapp.SaveAs FilePath, wdOpenFormatWebPages
wordapp.Close True
End Function
Function Word2Doc(FileUrl)
FilePath = GetFileBaseName(FileUrl) & ".doc"
Set objWord = CreateObject("Word.Application")
Set wordapp = objWord.Documents.Open(FileUrl, False, True)
wordapp.SaveAs FilePath, wdFormatDocument97
wordapp.Close True
End Function
Function Word2Docx(FileUrl)
FilePath = GetFileBaseName(FileUrl) & ".docx"
Set objWord = CreateObject("Word.Application")
Set wordapp = objWord.Documents.Open(FileUrl, False, True)
wordapp.SaveAs FilePath, wdFormatXMLDocument
wordapp.Close True
End Function
Function Word2Txt(FileUrl)
FilePath = GetFileBaseName(FileUrl) & ".txt"
Set objWord = CreateObject("Word.Application")
Set wordapp = objWord.Documents.Open(FileUrl, False, True)
wordapp.SaveAs FilePath, wdFormatDOSText
wordapp.Close True
End Function
' 取得文件基本名.
Function GetFileBaseName(ByVal sfilename)
n = InStrRev(sfilename, ".")
If n>1 Then
GetFileBaseName = Left(sfilename, n-1)
Else
GetFileBaseName = sfilename
End If
End Function
doc、docx、txt间格式转换的.vbs代码
于 2023-05-14 01:27:35 首次发布