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

发布于 2024-07-18 06:18:54 字数 450 浏览 5 评论 0原文

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

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

文件格式类似于:

textdocument1.txt              P. 6, 12 - issue1
textdocument2.txt              P. 5 - issue1
                               P. 13, 17 - issue3
textdocument3.txt              P. 10

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

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

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

任何帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

在巴黎塔顶看东京樱花 2024-07-25 06:18:54

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

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

我在 本文

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.

Spring初心 2024-07-25 06:18:54

如果您的 Word 文档列出了如下所示的所有文本文件:

<name>{tab}<page ref>{newline}
<name>{tab}<page ref>{newline}
<name>{tab}<page ref>{newline}

那么所有行都可以在 段落集合。 您可以使用简单的 For Each 循环来遍历它:

Dim p As Paragraph

For Each p In ActiveDocument.Paragraphs
  Debug.Print p.Range.Text
Next p

If your word document lists all the text files like this:

<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 Each loop:

Dim p As Paragraph

For Each p In ActiveDocument.Paragraphs
  Debug.Print p.Range.Text
Next p
梦一生花开无言 2024-07-25 06:18:54

每行

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

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
温柔戏命师 2024-07-25 06:18:54

如果文本是其他包含特殊字符或使用另一种语言,则上述代码将不起作用,这是我在执行的一项任务中提出的解决方案

    Dim para As Paragraph
    Dim sentence() As String
    For Each para In ActiveDocument.Paragraphs
          sentence() = Split(para.Range.Text, Chr(11))
            For i = 0 To UBound(sentence)
                  Msgbox(sentence(i))
            next i
    next

if text is in other contains special character or is in another language the above code will not work this was the solution i came up with on one of task i performed

    Dim para As Paragraph
    Dim sentence() As String
    For Each para In ActiveDocument.Paragraphs
          sentence() = Split(para.Range.Text, Chr(11))
            For i = 0 To UBound(sentence)
                  Msgbox(sentence(i))
            next i
    next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文