Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-31-2010, 01:27 PM
Michael007 Michael007 is offline Delete text Windows 7 64bit Delete text Office 2003
Novice
Delete text
 
Join Date: Dec 2010
Posts: 18
Michael007 is on a distinguished road
Default Delete text

Hi everyone,

I am a beginner with VBA. I wonder if it is possible with a VBA to delete the text in a paragraph when a specific word is met. If yes, could you write a piece of VBA, pls.

For example, in the following text:
====


Wind southwest 10 to 20 knots becoming light early this evening.
Periods of drizzle and fog beginning this evening changing to a few
showers near noon Saturday.

Wind light. Periods of drizzle and fog patches beginning early this
evening changing to showers Saturday afternoon.
=====

I wish to delete the text from "Periods" to the end of the paragraph for both paragraph in order to get this result:
====
Wind southwest 10 to 20 knots becoming light early this evening.

Wind light.
=====

Of course, you guess that my real text is much longer than that!
Thanks
Michael
Reply With Quote
  #2  
Old 12-31-2010, 03:41 PM
macropod's Avatar
macropod macropod is offline Delete text Windows 7 32bit Delete text Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Hi Michael,

For what you've described, and assuming you're using Word, a simple wildcard Find/Replace will do, where:
Find = Periods[!.]{1,}.
Replace = nothing

This assumes that the sentence beginning with 'Periods' in each logical paragraph is the last sentence in that paragraph. If that's not the case, you'll need to provide some more-representative examples.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 12-31-2010, 04:34 PM
Michael007 Michael007 is offline Delete text Windows 7 64bit Delete text Office 2003
Novice
Delete text
 
Join Date: Dec 2010
Posts: 18
Michael007 is on a distinguished road
Default Delete text

Hi Paul,

Your solution is a good start but as you said:

"This assumes that the sentence beginning with 'Periods' in each logical paragraph is the last sentence in that paragraph. If that's not the case, you'll need to provide some more-representative examples. "

So, I joined a file with a case a bit more complicated on page 1. On page 2, I included the result. It would be nice if you can include a VBA in it.

Thank you for your help,
Michael
Attached Files
File Type: doc Donaway.doc (27.5 KB, 18 views)
Reply With Quote
  #4  
Old 12-31-2010, 04:46 PM
macropod's Avatar
macropod macropod is offline Delete text Windows 7 32bit Delete text Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Hi Michael,

Try a wildcard Find/Replace with:
Find = Periods*([^13]{2})
Replace = \1

Is there a particular reason you need this as a macro? I'm happy enough to do one, but I'd like to be sure the solution works and that the request isn't simply because of a mistaken belief that's what's required.

PS: If you insert a space before the 'Periods' in the Find box, the trailing space at the end of each para will also be deleted - but it'll cause problems if the 'Periods' starts a new line.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]

Last edited by macropod; 12-31-2010 at 05:08 PM. Reason: Better Find/Replace expressions & PS
Reply With Quote
  #5  
Old 12-31-2010, 06:00 PM
Michael007 Michael007 is offline Delete text Windows 7 64bit Delete text Office 2003
Novice
Delete text
 
Join Date: Dec 2010
Posts: 18
Michael007 is on a distinguished road
Default Text delete

Hi Paul,

I have access to thousands of marine forecasts in archive. My job is to verify the winds forecast only. Besides that, I don't really care about the weather. This is why, I want a macro to get rid of the weather section and to reduce the number of pages to read.

My next step will be to replace several redundant words (like: morning, afternoon, evening, becoming,.. by abbreviations like AM, PM, Evng, bcmg...) all that in order to fit every forecast within a page (actually a single forecast takes 2-3 pages).

Michael

PS: I tried your command in a macro (Find = Periods[!.]{1,}.
Replace = nothing) but the line "Find... appeared in red. I guess that I missed something... as I said in just beginning in VBA
Reply With Quote
  #6  
Old 12-31-2010, 08:06 PM
macropod's Avatar
macropod macropod is offline Delete text Windows 7 32bit Delete text Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Hi Michael,

Try:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim strFind As String, strRep As String, i As Integer
strFind = "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday,morning,afternoon,evening,midnight,"
strFind = strFind & "knots,knots,becoming,north,south,east,west"
strRep = "Mon,Tue,Wed,Thu,fri,Sat,Sun,AM,PM,Evng,Mnght,"
strRep = strRep & "KT,KT,bcmg,N,S,E,W"
With ActiveDocument.Content.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Forward = True
  .Wrap = wdFindContinue
  .Format = False
  .MatchWildcards = True
  .Text = "^13"
  .Replacement.Text = "^l"
  .Execute Replace:=wdReplaceOne
  .Text = "([^13]{2}[!^13]{1,})^13"
  .Replacement.Text = "\1^l"
  .Execute Replace:=wdReplaceAll
  .Text = "Periods*([^13]{2})"
  .Replacement.Text = "\1"
  .Execute Replace:=wdReplaceAll
  .Text = "([ ]{1,})(^13)"
  .Replacement.Text = "\2"
  .Execute Replace:=wdReplaceAll
  .Text = "([!^13])(^13)([!^13])"
  .Replacement.Text = "\1\3"
  .Execute Replace:=wdReplaceAll
  .Text = "([0-9]) to ([0-9])"
  .Replacement.Text = "\1-\2"
  .Execute Replace:=wdReplaceAll
  .MatchWildcards = False
  .MatchCase = False
  For i = 1 To UBound(Split(strFind, ","))
    .Text = Split(strFind, ",")(i)
    .Replacement.Text = Split(strRep, ",")(i)
    .Execute Replace:=wdReplaceAll
  Next
End With
Application.ScreenUpdating = True
End Sub
The macro does the initial F/R you asked for, as well as a bunch of other cleaning up, including the AM/PM changes you mentioned. If you want to process additional words, simply add them and their replacements to the strFind and strRep variables.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]

Last edited by macropod; 12-31-2010 at 08:21 PM. Reason: Added word string array to simplify coding.
Reply With Quote
  #7  
Old 01-01-2011, 01:48 PM
Michael007 Michael007 is offline Delete text Windows 7 64bit Delete text Office 2003
Novice
Delete text
 
Join Date: Dec 2010
Posts: 18
Michael007 is on a distinguished road
Default Text delete

Hi Paul,

For some reasons, the macro does not work. I have joined a new file with 2 recent forecasts. Your macro is already in the file. For sure, you will be able to see what's wrong.

Michael
Attached Files
File Type: doc Marine3.doc (47.5 KB, 16 views)
Reply With Quote
  #8  
Old 01-01-2011, 07:01 PM
macropod's Avatar
macropod macropod is offline Delete text Windows 7 32bit Delete text Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Hi Michael,

There's no macro in the document you posted, so it's hard to see what you might have been doing wrong. To make the code available for use in multiple documents, I'd suggest adding to the normal template. I'd also suggest giving it a meaningful name! For installation instructions, see: http://www.gmayor.com/installing_macro.htm
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 01-01-2011, 08:57 PM
Michael007 Michael007 is offline Delete text Windows 7 64bit Delete text Office 2003
Novice
Delete text
 
Join Date: Dec 2010
Posts: 18
Michael007 is on a distinguished road
Default Text_Delete

Sorry Paul,

Though, I was sure that I included the code, anyhow...

As suggested, the macro was called: Text_delete

On the second try, I got an Execution Error (Text search contains a special criteria not valid).

With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Text = "^13"
.Replacement.Text = "^l"
.Execute Replace:=wdReplaceOne
.Text = "([^13]{2}[!^13]{1,})^13"
.Replacement.Text = "\1^l"
.Execute Replace:=wdReplaceAll ==== 'VBA stopped here'====

I re-send the file with hopefully the code included.

Michael
Attached Files
File Type: doc Marine4.doc (47.0 KB, 16 views)
Reply With Quote
  #10  
Old 01-01-2011, 09:17 PM
Michael007 Michael007 is offline Delete text Windows 7 64bit Delete text Office 2003
Novice
Delete text
 
Join Date: Dec 2010
Posts: 18
Michael007 is on a distinguished road
Default

Paul,

I've just checked on a different computer and the code in the file Marine4 seems again absent while it is present on the initial computer used to send the last message? Mistery?
Reply With Quote
  #11  
Old 01-01-2011, 09:18 PM
macropod's Avatar
macropod macropod is offline Delete text Windows 7 32bit Delete text Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Hi Michael,

That error suggests your PC isn't configured with English-language settings. In that case, you need to change:
.Text = "([^13]{2}[!^13]{1,})^13"
to:
.Text = "([^13]{2}[!^13]{1;})^13"
(note the ',' changed to ';')
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #12  
Old 01-01-2011, 09:29 PM
macropod's Avatar
macropod macropod is offline Delete text Windows 7 32bit Delete text Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Quote:
Originally Posted by Michael007 View Post
Paul,

I've just checked on a different computer and the code in the file Marine4 seems again absent while it is present on the initial computer used to send the last message? Mistery?
That just tells me you've added the code to the normal template, as I suggested in a previous thread. That makes the code available for processing any file on your system, without having to add the code to any given data file.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #13  
Old 01-01-2011, 09:30 PM
Michael007 Michael007 is offline Delete text Windows 7 64bit Delete text Office 2003
Novice
Delete text
 
Join Date: Dec 2010
Posts: 18
Michael007 is on a distinguished road
Default Text_Delete

Great! It works.

Thank you again. How can I indicates that problem is solved?
For your help and patience
Michael
Reply With Quote
  #14  
Old 01-01-2011, 10:38 PM
macropod's Avatar
macropod macropod is offline Delete text Windows 7 32bit Delete text Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Quote:
Originally Posted by Michael007 View Post
How can I indicates that problem is solved?
See Thread Tools -> Mark this thread as solved
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Undo Delete jbelorit Outlook 0 11-22-2010 08:10 AM
delete email message via blackberry and have it delete on my pop3 and my outlook Iamthestorm Outlook 2 10-28-2010 12:21 AM
Highlighted text won't delete - when I press enter Gague Word 2 07-09-2010 12:53 PM
Some emails will not delete Shawn76 Outlook 0 07-01-2010 01:33 PM
Need to delete style and style text mclan Word 0 08-04-2008 12:05 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:40 AM.


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