0
votes

As a non-programmer, here are my beginning, non-VBA, thoughts on how to copy/paste the first sentence of every paragraph:

Dim Terminalpoint as String

  1. Terminalpoint = “.” Or “?” or “!”
  2. Run through text until you find a terminal point
  3. Select everything before that terminalpoint.
  4. Copy selection
  5. Insert copied text into a new worksheet
  6. Run through text until you find a paragraph break or tabbed line
  7. Repeat steps 2-6 until end of text

Can I do this using VBA? How can I get started?

1
Select everything before that terminalpoint returns the first sentence, not the first word. Which do you need?David Zemens

1 Answers

2
votes

Here is an example of extracting the first SENTENCE from each paragraph in a document. This would be code written in and called from Word Application.

Sub test()
Dim doc As Document
Dim p As Paragraph
Dim s As String

Set doc = ActiveDocument

For Each p In doc.Paragraphs
    Debug.Print p.Range.Sentences(1)

Next

End Sub

From Excel, a simple routine to take each first sentence and place it in a new worksheet in the active Excel workbook:

Sub GetSentences()
'Word objects
Dim wdApp as Object
Dim doc as Object
Dim p as Object
Dim s as String

Dim ws as Worksheet

Set wdApp = CreateObject("Word.Application")
Set doc = wdApp.Documents.Open("c:\your filename.docx")

For each p in doc.Paragraphs
    Set ws = Worksheets.Add
    ws.Range("A1").Value = p.Range.Sentences(1)
Next

End Sub