#1
|
|||
|
|||
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 |
#2
|
||||
|
||||
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] |
#3
|
|||
|
|||
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 |
#4
|
||||
|
||||
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 |
#5
|
|||
|
|||
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 |
#6
|
||||
|
||||
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
__________________
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. |
#7
|
|||
|
|||
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 |
#8
|
||||
|
||||
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] |
#9
|
|||
|
|||
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 |
#10
|
|||
|
|||
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? |
#11
|
||||
|
||||
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] |
#12
|
||||
|
||||
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] |
#13
|
|||
|
|||
Text_Delete
Great! It works.
Thank you again. How can I indicates that problem is solved? For your help and patience Michael |
#14
|
||||
|
||||
See Thread Tools -> Mark this thread as solved
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
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 |