VBS 关于提取WORD第二行的文字为文件名的方式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VBS 关于提取WORD第二行的文字为文件名的方式相关的知识,希望对你有一定的参考价值。

如何用VBS代码批量提取WORD第二行内容为文档名,谢谢!!!

有看到用VBS提取WORD文档内容第一行为文档名的代码?第二行怎么提呢?
http://zhidao.baidu.com/question/162828108.html?fr=qrl&cid=1069&index=1
求完整代码??? 谢谢!

完整代码:

1、用记事本新建一个文本文件,把它保存为“批量重命名.vbs”(注意不要弄成了“批量重命名.vbs.txt”,也就是要确保其扩展名为“.vbs”);

2、把下列代码粘贴到这个VBS文件中:
Option Explicit

Const g_strRootPath = "c:\Temp\docs\Word\ToRename\" ' 指定存放所有文件的目录,可以有子目录
Const g_nTitleMaxLen = 16 ' 指定获取文档里面第一段中的前多少个字符来作为文件名
Const g_Line = 2 ' 指定获取文档里面第一段中的第2行
Call Main

' 主函数入口
Sub Main()

Dim fso, oFolder, oWordApp

Set oWordApp = CreateObject("Word.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(g_strRootPath)

RenameDocFilesUnderFolder oWordApp, fso, oFolder

oWordApp.Quit
Set oWordApp = Nothing

MsgBox "完成!"

End Sub

' 重命名指定文件夹(递归)下面的所有Word文件,按照文件里面的第一句可见的文字命名
Sub RenameDocFilesUnderFolder(oWordApp, fso, oFolder)

Dim oSubFolder, oFile, oDoc
Dim strTitle, strFileName

For Each oSubFolder In oFolder.SubFolders
RenameDocFilesUnderFolder oWordApp, fso, oSubFolder
Next

For Each oFile In oFolder.Files
Set oDoc = oWordApp.Documents.Open(oFile.Path)
strTitle = GetFirstVisibleTextContent(oDoc)
oDoc.Close
Set oDoc = Nothing

If Len(strTitle) <> 0 Then
strFileName = fso.BuildPath(fso.GetParentFolderName(oFile.Path), strTitle & "." & fso.GetExtensionName(oFile.Path))
strFileName = GetUniqueFileName(fso, strFileName)
fso.MoveFile oFile.Path, strFileName
End If
Next

End Sub

' 获取指定文档第二行可见文字
Function GetFirstVisibleTextContent(oDoc)

Dim oParagraph
Dim strContent
Dim i
For Each oParagraph In oDoc.Paragraphs
strContent = GetSafeFileName(oParagraph.Range.Text)
If Len(strContent) <> 0 Then
i = i + 1
If i = g_Line Then
GetFirstVisibleTextContent = strContent
Exit Function
End If
End If
Next

GetFirstVisibleTextContent = ""

End Function

' 过滤文件名里面的无效字符
Function GetSafeFileName(strFileName)

Dim arrUnsafeCharacters, strUnsafeChar
Dim nIndex

arrUnsafeCharacters = Array("\", "/", ":", "*", "?", """", "<", ">", "|")

For nIndex = 0 To &H2F
strFileName = Replace(strFileName, Chr(nIndex), "")
Next

For Each strUnsafeChar In arrUnsafeCharacters
strFileName = Replace(strFileName, strUnsafeChar, "")
Next

GetSafeFileName = Left(Trim(strFileName), g_nTitleMaxLen)

End Function

' 获取不重复的文件名,如果有重名则在文件名后面附加“_1”、“_2”……
Function GetUniqueFileName(fso, strFullName)

Dim strParentFolder, strBaseName, strExtensionName
Dim nIndex

If Not fso.FileExists(strFullName) Then
GetUniqueFileName = strFullName
Exit Function
End If

strParentFolder = fso.GetParentFolderName(strFullName)
strBaseName = fso.GetBaseName(strFullName)
strExtensionName = fso.GetExtensionName(strFullName)

nIndex = 0

While fso.FileExists(strFullName)
nIndex = nIndex + 1
strFullName = fso.BuildPath(strParentFolder, strBaseName & "_" & nIndex & "." & strExtensionName)
Wend

GetUniqueFileName = strFullName

End Function

3、修改代码中开始部分的三个设置,即:存放等待重命名的Word文件的根目录,第几行,以及获取文档第一段内容时最多保留多少个字符。

4、保存这个VBS文件,在资源管理器中双击运行它,直到看见“完成”!

5、检查所有文件是否已自动重命名。

注意:如果有两个以上的文档依据其内容提取出来的文字相同,则会自动在文件名后面附加“_1”、“_2”、“_3”……。

如果有什么问题,请和我联系。
参考技术A 针对文档内二级标题提取和格式修改的说明:
第一,确定每个标题是否为同一样式。方法:选中标题后右键可查看;
若不是统一的样式,可用格式刷刷成统一的样式。
第二步,选中任一二级标题后,右键→点击“选择格式相似的文本”,即可选中所有二级标题,这样就可以对所有二级标题进行复制提取和格式修改了。
针对多个文章标题的提取除了复制外暂无更好办法
参考技术B lily_blues确实是高手
你只要把这段修该成这样就行了

Function GetFirstVisibleTextContent(oDoc)

Dim oParagraph
Dim strContent

useful_line_count=0 '///////////////////有效行数////////////////////

For Each oParagraph In oDoc.Paragraphs
strContent = GetSafeFileName(oParagraph.Range.Text)
If Len(strContent) <> 0 Then
GetFirstVisibleTextContent = strContent
useful_line_count=useful_line_count+1

If useful_line_count=2 Then Exit Function ' ///////////////////第n行对应把=2 改为n即可

End If
Next

GetFirstVisibleTextContent = ""

End Function追问

如何利用VBA提取word页眉文字为文件名???

以上是关于VBS 关于提取WORD第二行的文字为文件名的方式的主要内容,如果未能解决你的问题,请参考以下文章

“word”上下文中 总差半个字符,并且不能对齐。如何处理这种情况?

在WPS中,第一行字数没满,怎么把第二行的合并上来,除了在第二行开头按Backspace之外

求一个让Windows应用程式置顶批处理或者VBS程序

文本文档怎么自动换行?

VBS如何提取指定进程的PID?

关于C#从Word文件中提取内容(包括样式文字,图片,公式,表格)等信息,解析分字段写入数据库的问题。