使用 VBA 解析 MS Word 文档中的文本

Lee*_*ejo 5 parsing vba ms-word

我希望有人可以帮助使用 MS Word 宏。

基本上,我有一个 MS Word 文档,其中列出了几个文本文件以及每个文件中感兴趣的特定页面。

文件格式类似于:

textdocument1.txt 第 6, 12 页 - 问题 1
textdocument2.txt P. 5 - 问题 1
                               第 13、17 页 - 第 3 期
textdocument3.txt 第 10 页

我想将每一行作为字符串读入我的宏。

然后遍历它来识别文件名。有了文件名,我就可以打开文件,转到页码,然后复制我需要的数据。

但我陷入了步骤 1,如何将该行捕获到 MS Word 宏中的字符串中?

任何帮助将不胜感激。

e.J*_*mes 6

以下代码应该可以帮助您入门:

Public Sub ParseLines()
    Dim singleLine As Paragraph
    Dim lineText As String

    For Each singleLine In ActiveDocument.Paragraphs
        lineText = singleLine.Range.Text

        '// parse the text here...

    Next singleLine
End Sub
Run Code Online (Sandbox Code Playgroud)

我在这篇文章中找到了基本算法。