Using VBA to parse text in an MS Word document

I was hoping someone could help with the MS Word macro.

Basically, I have a MS Word document that lists several text files and specific pages of interest for 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 every line in my Macro as a line.

Then go through it to identify the file name. With the file name, I can open the file, go to the page number and copy the data I need.

But I got stuck in step 1, how do I bind a string to a string in a MS Word macro?

Any help would be appreciated.

+4
source share
3 answers

The following code should get 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 .

+4
source

If your Word document lists all text files:

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

Then all lines are available in the paragraph collection . You can execute the loop with a simple For Each loop:

 Dim p As Paragraph For Each p In ActiveDocument.Paragraphs Debug.Print p.Range.Text Next p 
+3
source

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 
+2
source

All Articles