5

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.

0

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This will break the document up into paragraphs. If you want sentences do this per line (i.e. sentence) check my answer below.
3

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

2 Comments

For Each messes up if you make changes to the object, it can get stuck on the same element for ever, I had to use an index variable.
Very true, if you alter the collection you iterate over (deleting or adding new paragraphs) then weird things will happen. Using a indexed loop might fail in this case as well.
2

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.