我从一个朋友那里得到了一个奇怪的任务,就是解析一堆单词文件,并将其中的某些部分写到文本文件中进行进一步的处理。
VBscript不是我喜欢的一杯茶,所以我不知道该如何把这些东西装在一起。
这些文件如下所示:
Header
A lot of not interesting text
Table
Header
More boring text
Table我想解析这些文档,并从其中获取所有的标题和目录。我正在逐步浏览这份文档
For Each wPara In wd.ActiveDocument.Paragraphs我想我知道如何得到标题
If Left(wPara.Range.Style, Len("Heading")) = "Heading" Then但我不知道该怎么做
Else if .. this paragraph belongs to a table..因此,任何关于如何判断段落是否是表的一部分的提示都会很好。
发布于 2015-03-15 12:08:48
未经测试,因为我现在无法访问MS Word。
Option Explicit
Dim FSO, Word, textfile, doc, para
' start Word instance, open doc ...
' start FileSystemObject instance, open textfile for output...
For Each para In doc.Paragraphs
If IsHeading(para) Or IsInTable(para) Then
SaveToFile(textfile, para)
End If
Next
Function IsHeading(para)
IsHeading = para.OutlineLevel < 10
End Function
Function IsInTable(para)
Dim p, dummy
IsInTable = False
Set p = para.Parent
' at some point p and p.Parent will both be the Word Application object
Do While p Is Not p.Parent
' dirty check: if p is a table, calling a table object method will work
On Error Resume Next
Set dummy = obj.Cell(1, 1)
If Err.Number = 0 Then
IsInTable = True
Exit Do
Else
Err.Clear
End If
On Error GoTo 0
Set p = p.Parent
Loop
End Function显然,SaveToFile是您自己实现的东西。
由于"is in table“自然被定义为”对象的父级是表“,这是使用递归(进一步解构)的完美情况:
Function IsInTable(para)
IsInTable = IsTable(para.Parent)
If Not (IsInTable Or para Is para.Parent) Then
IsInTable = IsInTable(para.Parent)
End If
End Function
Function IsTable(obj)
Dim dummy
On Error Resume Next
Set dummy = obj.Cell(1, 1)
IsTable = (Err.Number = 0)
Err.Clear
On Error GoTo 0
End Functionhttps://stackoverflow.com/questions/29058477
复制相似问题