使用 VBA 解析 MS Word 文档中的文本 [英] Using VBA to parse text in an MS Word document
问题描述
我希望有人可以帮助使用 MS Word 宏.
基本上,我有一个 MS Word 文档,其中列出了几个文本文件和每个文件中感兴趣的特定页面.
文件格式类似于:
<前>textdocument1.txt P. 6, 12 - issue1textdocument2.txt 第 5 页 - 问题 1第 13、17 页 - 第 3 期textdocument3.txt 第 10 页我想将每一行作为字符串读入我的宏.
然后遍历它来识别文件名.有了文件名,我就可以打开文件,转到页码,复制我需要的数据.
但我卡在第 1 步,如何在 MS Word 宏中将该行捕获为字符串?
任何帮助将不胜感激.
以下代码应该可以帮助您入门:
公共子 ParseLines()将单行调暗为段落Dim lineText 作为字符串对于 ActiveDocument.Paragraphs 中的每个单行lineText = singleLine.Range.Text'//在这里解析文本...下单行结束子
我在这篇文章.
I was hoping someone could help with a MS Word Macro.
Basically, I have a MS Word document which lists out several text files and specific pages of interest in each file.
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?
Any help will be appreciated.
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.
这篇关于使用 VBA 解析 MS Word 文档中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!