使用 VBA 解析 MS Word 文档中的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/756990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using VBA to parse text in an MS Word document
提问by Leejo
I was hoping someone could help with a MS Word Macro.
我希望有人可以帮助使用 MS Word 宏。
Basically, I have a MS Word document which lists out several text files and specific pages of interest in each file.
基本上,我有一个 MS Word 文档,其中列出了几个文本文件和每个文件中感兴趣的特定页面。
The file format is similar to:
文件格式类似于:
textdocument1.txt P. 6, 12 - issue1
textdocument2.txt P. 5 - issue1
P. 13, 17 - issue3
textdocument3.txt P. 10
I want to read each line into my Macro as a string.
我想将每一行作为字符串读入我的宏。
Then traverse through it to identify the file name. With the file name, I can then open the file, go to the page number, and copy the data I need.
然后遍历它来识别文件名。有了文件名,我就可以打开文件,转到页码,然后复制我需要的数据。
But I'm stuck at step 1, how do I capture the line into a string in an MS Word Macro?
但是我被困在第 1 步,如何在 MS Word 宏中将该行捕获为字符串?
Any help will be appreciated.
任何帮助将不胜感激。
采纳答案by e.James
The following code should get you started:
以下代码应该可以帮助您入门:
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
I found the basic algorithm in this article.
我在这篇文章中找到了基本算法。
回答by Tomalak
If your word document lists all the text files like this:
如果你的 word 文档列出了所有这样的文本文件:
<name>{tab}<page ref>{newline}
<name>{tab}<page ref>{newline}
<name>{tab}<page ref>{newline}
Then all the lines are available in the Paragraphs collection. You can loop through that with a simple For Eachloop:
然后所有的行都在Paragraphs 集合中可用。你可以用一个简单的For Each循环来循环:
Dim p As Paragraph
For Each p In ActiveDocument.Paragraphs
Debug.Print p.Range.Text
Next p
回答by Anonymous Type
per line
每行
Public Sub ParseDoc()
Dim doc As Document
Set doc = ActiveDocument
Dim paras As Paragraphs
Set paras = doc.Paragraphs
Dim para As Paragraph
Dim sents As Sentences
Dim sent As Range
For Each para In paras
Set sents = para.Range.Sentences
For Each sent In sents
Debug.Print sent.Text
Next
Next
End Sub

