|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
I am trying to delete text of any format between two bold, recurring keywords or symbols
Greetings,
I have tried to come up with a Word macro or other gadget that will remove all text between two defined "keywords/symbols". The beginning and ending keywords/symbols are always in bold font, and they are consistent throughout the records with which I work. The intervening words between the beginning and ending keywords/symbols might be a mix of formatting other than bold-font characters -- i.e., not-bold, italic, and/or underlined. Here is a generic example that is representative: . . . Research Refs.: [O]striches are a family, Struthionidae, of flightless birds. The two extant species of ostrich are the common ostrich and Somali ostrich, both in the genus Struthio, which also contains several species known from Holocene fossils such as the Asian ostrich. Please refer to § 18.05 for more information. The common ostrich is the more widespread of the two living species, and is the largest living bird species. Other ostriches are also among the largest bird species ever . . . . § It is common for a not-bold "§" symbol to appear in the intervening content between the keywords/symbols of interest. My goal is to strip out all this content from lengthy records. In other words, I desire to remove all content that begins with "Research Refs.:" and ends with "§" -- the keyword/symbol and everything in between. I have been unable, however, to derive a pattern to work. Specifically, I am unclear how to cause Word to "catch" or stop at the ending instance of "§", and how to cause Word to ignore intervening "§" symbols that are not-bold. This seems horribly simple, so I apologize in advance that I must look to finer minds for guidance on the point. With much thanks. Q. |
#2
|
||||
|
||||
You need to do this with a series of search and replaces:
First find the bold "Research Refs" and append a start pattern there eg "|||Research Refs" Then find the bold "§" and append an end pattern there eg "§|||" Then find those patterns without the bold being a factor.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Thank you for your reply -- I had not come across the "|||" for starting and ending a pattern, and I would have continued to be at a loss without your input.
I will put this idea to use, and I appreciate that you took a moment to teach me something new. With much thanks, Q. |
#4
|
|||
|
|||
You can also do this using a nested .Find method:
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim oRng As Range, oRngE As Range Set oRng = ActiveDocument.Range With oRng.Find .Text = "Research Refs" .Font.Bold = True While .Execute Set oRngE = oRng.Duplicate oRngE.End = ActiveDocument.Range.End With oRngE.Find .Text = "§" .Font.Bold = True If .Execute Then oRngE.Start = oRng.Start oRngE.Delete End If End With oRng.Collapse wdCollapseEnd Wend End With lbl_Exit: Exit Sub End Sub |
#5
|
|||
|
|||
Greg,
I appreciate your suggestion -- I will examine this and make use of it. I love this forum! Q. |
#6
|
|||
|
|||
Greetings,
I have been working with the above code (a) to understand it and (b) to coax it or something like it to work with an additional wrinkle. I have been researching and working on the latter for a couple of weeks. No success. Records I am working with look something like this very random example: * * * * § 1. By unanimous-consent, agreement was reached on providing megaphones for antelopes. Research Refs Bruce Wayne, of Gotham, was nominated to be Ambassador to the Russian Federation, and That Guy's Cats, of Texas, to be Commissioner of Food and Drugs, Department of Health and Human Services, and Aurelia Skipwith, of Indiana, to be Director of the United States Fish and Wildlife Service . . . . § 2. Committee on Armed Services: . . . both of the Department of Defense. * * * * I am trying to make the code search for multiple, optional "key word" end points for oRngE. In the above example, for instance, I intend for the range selected for deletion to end with That Guy's Cats, rather than at the next § symbol. For each occurrence of Research Refs in my records, an instance of That Guy's Cats may appear thereafter and before the next §; sometimes, however, the phrase That Guy's Cats does not show up between the two. It appears to do no good for me to define two ranges in separate "With" structures, and search for the respective "key words" in successive order. That will delete more text than I intend to remove. I have wrestled with Do ... While, and I tried nested If ... Then ... Else structures. My coding ideas look (and also work) like something left behind by That Guy's Cats. The snag that I cannot unravel is how to look for That Guy's Cats within the already-defined range that starts with Research Refs and ends with §. With thanks and regards, Q. |
#7
|
||||
|
||||
I'm not sure I understand what your requirements are but this might be what you are looking to do
Code:
Sub ScratchMacro() 'Modifications to Word macro originally coded by Greg Maxey Dim oRng As Range, oRngE As Range, oRngF As Range, sFind As String sFind = "That Guy's Cats" Set oRng = ActiveDocument.Range With oRng.Find .Text = "Research Refs" .Font.Bold = True While .Execute Set oRngE = oRng.Duplicate oRngE.End = ActiveDocument.Range.End With oRngE.Find .Text = "§" .Font.Bold = True If .Execute Then oRngE.Start = oRng.Start Set oRngF = oRngE.Duplicate With oRngF.Find .Text = sFind .ClearFormatting If .Execute Then oRngE.Start = oRng.Start oRngE.End = oRngF.End 'oRngE.Select End If End With oRngE.Delete End If End With oRng.Collapse wdCollapseEnd Wend End With lbl_Exit: Exit Sub End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#8
|
|||
|
|||
Figured it Out -- Sort of . . . .
I derived code that works reasonably well, but I wonder if there is a more simple or straightforward way to ring the last bell.
My goal is to isolate six possible ranges between two broader "end points" which represent the largest possible (recurring) range, to isolate the shortest range among them, delete that range, and move on to the next relevant "end points" in the record. I can explain it best this way: A . . . . . . . . . . . . X is the longest possible range; however, there might (or might not) be a "B" of interest between "A" and "X", as in -- A . . . . . . B . . . . . X; alternatively, there might (or might not) be a "C" of interest between "A" and "B", as in -- A . . . C . . B . . . . . X; etc. Complicating matters, the range "A" to "C" might be longer than the range "A" to "B", or it might be shorter. The code I derived identifies the relevant ranges beautifully, and it stores a variable defining their respective "lengths". The elusive part has been identifying the shortest range that is not zero from among the six possibilities. To do this, I undertook a seriously complex set of inequalities that makes my head hurt. It performs acceptably. My question is conceptual: Is there a technique or function that can be deployed (roughly analogous to "Min" in MS Access) by which to simply detect the shortest range, not equal to zero, and delete it? If such a thing exists, I have not come upon it in researching the topic. Thanks as always for your kind attention. Q. |
#9
|
||||
|
||||
Since you haven't provided your code we can just talk in concepts.
I would approach this by using a recurring function along the lines of: 1. Build a list of all the relevant ranges and sort in length order 2. Remove the shortest 3. Repeat steps 1 & 2 until there are no more ranges found. The important part is after removing the shortest range, you do a complete rebuild of the list
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
Tags |
word vba macro |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
bold (or unbold) section of text instead of bold toggling | Burt | Word | 6 | 04-06-2019 09:09 AM |
Macro to delete text from cells with specific format | Soenke | Word VBA | 4 | 09-01-2016 08:55 AM |
Bold each line of text that starts with a recurring symbol | qubie | Word | 6 | 08-26-2016 07:10 AM |
Text in #1 is made bold, rest of the document is edited, text in #1 is now not bold | footer-assistance | Word | 1 | 06-29-2015 03:49 AM |
Format Bold in one line makes all lines bold | Nitte | Word | 2 | 02-07-2013 12:34 AM |