#1
|
|||
|
|||
"Stitch" word sections together
Hi all! I have a software that generates documents with some repetitive pattern in the middle. The following is an example: "|" indicates where the repetitive starts
(normal content)...apple is red and banana | apple is red and banana is yellow...(normal content) And I want to turn that into: (normal content)...apple is red and banana is yellow...(normal content) The problem is that the repetitive part's content is changeable. I can somehow control the length of the repetitive part (I can define it as integer L) and also the frequency of such thing based on time, but not word-wise or letter-wise. My current thought is to compare a combination of several words (like "apple is red", the length of this string < L) with its possible repetitive that is located on the next Lth word; if they're the same, delete the content in between. Or search a combination within the range from itself to the next Lth word to see if there's a repetitive one and then delete the content between these 2. Could someone help me out on this please? I also appreciate other more efficient/accurate methods. Thank you very much. |
#2
|
||||
|
||||
Without access to the text the following should work - at least it does with your example.
Basically it looks for the " | " string which separates the similar texts, then identifies the next word, then deletes all the previous words until the identified next word is found. It then deletes the previous word (which is the repeated identified word) and then sets the found string to a single space before looking for the next " | " string. A big problem is that what Word sees as a 'word' may not be what you think of as a 'word' and punctuation in the strings can screw it all up. Code:
Sub Macro1() Dim orng As Range Dim orngA As Range On Error GoTo err_Handler Set orng = ActiveDocument.Range With orng.Find Do While .Execute(FindText:=" | ") Set orngA = orng.Next.Words.First Do Until orng.Previous.Words.Last = orngA orng.Previous.Words.Last.Delete Loop orng.Previous.Words.Last.Delete orng.Text = " " orng.Collapse 0 Loop End With lbl_Exit: Set orng = Nothing Set orngA = Nothing Exit Sub err_Handler: Err.Clear GoTo lbl_Exit End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Quote:
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Unprotect sections in a word document using VBA | dyb | Word VBA | 5 | 02-17-2022 01:43 AM |
Word VBA - "Page left Blank" between Sections to make all Sections start on even page | Cov_ATC | Word VBA | 10 | 11-28-2021 03:41 PM |
Word Sections - Bookmarks | ldoodle | Word VBA | 3 | 11-07-2018 07:04 AM |
Word is switching sections upon printing/PDF | rlockwood | Word | 0 | 01-08-2018 09:23 AM |
Word 2007 - how to manage sections? | JQP | Word | 2 | 10-10-2011 11:06 AM |