#1
|
|||
|
|||
VBA, Place Sentence in its own Line
I have the following code. So its able to locate each sentence, but now I want to place the sentence in its own line. Not sure what to add to this VBA code to do that. thanks for any help.
Code:
Public Sub ParseDoc() Dim doc As Document Dim paras As Paragraphs Dim para As Paragraph Dim sents As Sentences Dim sent As Range Set doc = ActiveDocument Set paras = doc.Paragraphs For Each para In paras Set sents = para.Range.Sentences For Each sent In sents 'place sentence in its own line Next Next End Sub The Securities Exchange Act of 1934 was the second major piece of legislation. It resulted from the market crash of 1929. The Securities Exchange Act of 1934 regulates the secondary market. It consists of investor-to-investor transactions. All transactions between two investors that are executed on any of the exchanges. The over-the-counter (OTC) market are secondary market transactions. AFTER The Securities Exchange Act of 1934 was the second major piece of legislation. It resulted from the market crash of 1929. The Securities Exchange Act of 1934 regulates the secondary market. It consists of investor-to-investor transactions. All transactions between two investors that are executed on any of the exchanges. The over-the-counter (OTC) market are secondary market transactions. |
#2
|
|||
|
|||
It won't work. You can get close with the following, but Word doesn't really know what a sentence is.
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim i As Long Dim oRng As Range Set oRng = ActiveDocument.Range oRng.Collapse wdCollapseStart Do oRng.MoveEnd wdParagraph, 1 For i = 2 To oRng.Sentences.Count oRng.Sentences(i).InsertBefore Chr(11) Next oRng.Collapse wdCollapseEnd Loop Until oRng.End + 1 = ActiveDocument.Range.End lbl_Exit: Exit Sub End Sub The Securities Exchange Act of 1934 was the second major piece of legislation. Introduced by Mr. McGoo, it resulted from the market crash of 1929. The Securities Exchange Act of 1934 regulates the secondary market ... and run the code again. |
#3
|
|||
|
|||
thanks Greg.
I installed your 'Sentence Deducer' which is great, but is the purpose to visually inspect sentences and manually add formatting? Is there anyway I can use the functions within VBA code? if so how? thanks |
#4
|
||||
|
||||
You don't really need any VBA for what you're doing - simply do a wildcard Find, where:
Find = [.\?\!^l^13]*[.\?\!^l^13] As per VBA, of course, this will identify anything with a period in it as the end of a sentence but, other than that, each time you press 'Find Next' in the F/R dialogue, it will select the next 'sentence'. Since you're wanting to stop to add formatting, there's probably not much benefit in having a macro.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
thanks Macropod. I am actually looking to use it in VBA to process an entire document not just 1 line at a time with using Find. But I assume i can use the 'regular expression' you provided in code to see if it catches sentences better than Word.
|
#6
|
||||
|
||||
But if you're wanting to stop to add formatting - which is what you said you want to do - you will have to quit the macro every time. Sure, you could include the wildcard Find expression in a macro but: a) it won't do any better than the code Greg provided; and b) won't change the fact you'll have to stop execution to do any formatting. The only way you can automate the formatting is if it can be put into a set of rules that a macro with Word's VBA sentence limitations could apply.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
thanks Paul
I mentioned formatting sentence as a reference to Gregs Sentence Deducer addin. The addin selects each sentence with the "Next" button, you can at that point add formatting. i have simple non-fiction documents without any quotes, but it does have numbering. I have been able to sperate each sentence into its own line with my current document, but I will have to see how other documents respond.thanks |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
MS word 2007, capital letter after number (1. place turn to 1. Place) | enzo321 | Word | 1 | 10-23-2016 06:19 AM |
sentence/line spacing.. | rynegold | Word | 2 | 11-19-2014 12:36 AM |
Delete does not bring second sentence closer to first sentence | Andoheb | Word | 29 | 07-03-2014 01:48 PM |
editing a number/text at one place and changes taking place wherever it appears | anurag.butoliya | Word | 1 | 06-14-2014 06:27 PM |
Use macro to place line style around graphic | AdvanceTheMan | Word VBA | 1 | 04-20-2012 08:18 PM |