Peter.Ward
Peter.Ward

Reputation: 7

Copy/paste first sentence of every paragraph

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?

Upvotes: 0

Views: 6058

Answers (1)

David Zemens
David Zemens

Reputation: 53623

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

Upvotes: 2

Related Questions