Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-27-2017, 06:11 PM
ilcaa72 ilcaa72 is offline VBA, Place Sentence in its own Line Windows 7 64bit VBA, Place Sentence in its own Line Office 2013
Novice
VBA, Place Sentence in its own Line
 
Join Date: Jan 2014
Posts: 27
ilcaa72 is on a distinguished road
Default 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
BEFORE
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.
Reply With Quote
  #2  
Old 04-27-2017, 07:11 PM
gmaxey gmaxey is offline VBA, Place Sentence in its own Line Windows 7 32bit VBA, Place Sentence in its own Line Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
Now change to:
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.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 04-27-2017, 07:38 PM
ilcaa72 ilcaa72 is offline VBA, Place Sentence in its own Line Windows 7 64bit VBA, Place Sentence in its own Line Office 2013
Novice
VBA, Place Sentence in its own Line
 
Join Date: Jan 2014
Posts: 27
ilcaa72 is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 04-27-2017, 08:28 PM
macropod's Avatar
macropod macropod is offline VBA, Place Sentence in its own Line Windows 7 64bit VBA, Place Sentence in its own Line Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 04-28-2017, 04:04 AM
ilcaa72 ilcaa72 is offline VBA, Place Sentence in its own Line Windows 7 64bit VBA, Place Sentence in its own Line Office 2013
Novice
VBA, Place Sentence in its own Line
 
Join Date: Jan 2014
Posts: 27
ilcaa72 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Since you're wanting to stop to add formatting, there's probably not much benefit in having a macro.
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.
Reply With Quote
  #6  
Old 04-28-2017, 05:25 AM
macropod's Avatar
macropod macropod is offline VBA, Place Sentence in its own Line Windows 7 64bit VBA, Place Sentence in its own Line Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #7  
Old 04-28-2017, 07:01 AM
ilcaa72 ilcaa72 is offline VBA, Place Sentence in its own Line Windows 7 64bit VBA, Place Sentence in its own Line Office 2013
Novice
VBA, Place Sentence in its own Line
 
Join Date: Jan 2014
Posts: 27
ilcaa72 is on a distinguished road
Default

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
Reply With Quote
Reply



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
VBA, Place Sentence in its own Line Delete does not bring second sentence closer to first sentence Andoheb Word 29 07-03-2014 01:48 PM
VBA, Place Sentence in its own Line editing a number/text at one place and changes taking place wherever it appears anurag.butoliya Word 1 06-14-2014 06:27 PM
VBA, Place Sentence in its own Line Use macro to place line style around graphic AdvanceTheMan Word VBA 1 04-20-2012 08:18 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:34 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft