Microsoft Office Forums keep only sentences beginning with: Wind

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-16-2011, 09:13 PM
Michael007 Michael007 is offline keep only sentences beginning with: Wind Windows 7 64bit keep only sentences beginning with: Wind Office 2003
Novice
keep only sentences beginning with: Wind
 
Join Date: Dec 2010
Posts: 18
Michael007 is on a distinguished road
Default keep only sentences beginning with: Wind

Hi everyone,

I am a beginner with VBA. I wonder if it is possible with a VBA to keep in a long text, only the sentences beginning with the words: "Wind" or "Donaway" or "Sagway". If yes, could you write a piece of VBA, pls.

For instance, in the following text:
=====
Donaway to Isle-Icer - eastern half.
Winds southwest 15 to 25 knots diminishing to light this evening. Wind increasing to northeast 20 to 30 near noon saturday. Periods of drizzle and occasional fog turning to snow tonight. Low tonight minus 1. High Saturday plus 2.

Sagway to Tadouvik.
Gale warning in effect.


Wind light becoming east 10 to 15 knots early this evening then diminishing to light near midnight.Wind increasing to east 15 to 25 near noon Saturday and to 25 to 35 Saturday evening. Risk of freezing drizzle late this afternoon.
====

I wish to keep only
====
Donaway to Isle-Icer - eastern half.
Wind southwest 15 to 25 knots diminishing to light early this evening and to light near midnight. Wind increasing to northeast 15 to 25 near noon Saturday.

Sagway to Tadouvik.
Wind light becoming east 10 to 15 knots early this evening thendiminishing to light near midnight. Wind increasing to east 15 to25 near noon Saturday and to 25 to 35 Saturday evening.
=========


Thanks for your help
Michael
Reply With Quote
  #2  
Old 01-16-2011, 11:32 PM
macropod's Avatar
macropod macropod is offline keep only sentences beginning with: Wind Windows 7 32bit keep only sentences beginning with: Wind Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,599
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Hi Michael,

Try:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim RngSnt As Range, RngTmp As Range, strWrd As String
Dim i As Integer, bWrd As Boolean, ArrWrds As String
ArrWrds = "Wind,Winds,Donaway,Sagway"
With ActiveDocument
  For Each RngSnt In .Sentences
    bWrd = False
    With RngSnt
      strWrd = Trim(.Words.First.Text)
      For i = 0 To UBound(Split(ArrWrds, ","))
        If Split(ArrWrds, ",")(i) = strWrd Then bWrd = True: Exit For
      Next
      If bWrd = False Then
        Set RngTmp = RngSnt
        RngTmp.End = RngTmp.End - 1
        RngTmp.Delete
      End If
    End With
  Next
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[MS MVP - Word]

Last edited by macropod; 01-16-2011 at 11:36 PM. Reason: More efficient code
Reply With Quote
  #3  
Old 01-17-2011, 03:15 PM
Michael007 Michael007 is offline keep only sentences beginning with: Wind Windows 7 64bit keep only sentences beginning with: Wind Office 2003
Novice
keep only sentences beginning with: Wind
 
Join Date: Dec 2010
Posts: 18
Michael007 is on a distinguished road
Default Keep only sentences beginning with: wind

Hi Paul,

it is very close but there are still a few details to fix.
1. The VBA keeps only the text of the current line (until the next paragraph and the rest of the sentence is lost). Infact, I want to keep the text until the next "." .
2. It keeps the blank lines from one region to the other. How can I get rid of them.

I joined an example of a marine forecast. I did make the appropriate changes "Wind,Northerly,Southerly,Easterly,Westerly"

Thanks for your help
Michael
Attached Files
File Type: doc Marine4.doc (37.5 KB, 1 views)
Reply With Quote
  #4  
Old 01-17-2011, 04:11 PM
macropod's Avatar
macropod macropod is offline keep only sentences beginning with: Wind Windows 7 32bit keep only sentences beginning with: Wind Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,599
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Hi Michael,

The problem is due to your data having paragraph breaks at the end of every line. That means that every line is regarded as a paragraph of however many sentences it may contain, in its own right. ISTR I gave you a macro before for cleaning up such documents. If you do that first, the latest macro should do its job.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sentences and paragraphs keep disappearing in Word XP (2002) at bottom of page urobolus Word 3 08-26-2011 10:02 AM
Dash in the beginning of a new line Wakinyan Word 0 07-11-2010 07:10 AM
keep only sentences beginning with: Wind Add text to the beginning/ending of each line in word ballmoney Word 1 01-17-2010 02:19 AM
Looping macros to add text to beginning and end of a paragraph pachmarhi Word VBA 0 02-16-2009 06:57 AM
capitalize first letter of sentences norco1 Word 0 06-25-2006 12:37 PM


All times are GMT -7. The time now is 01:57 PM.


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