Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-07-2018, 12:16 AM
puff puff is offline "Stitch" word sections together Windows 7 64bit "Stitch" word sections together Office 2013
Advanced Beginner
"Stitch" word sections together
 
Join Date: Apr 2017
Posts: 60
puff is on a distinguished road
Question "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.
Reply With Quote
  #2  
Old 01-07-2018, 07:52 AM
gmayor's Avatar
gmayor gmayor is offline &quot;Stitch&quot; word sections together Windows 10 &quot;Stitch&quot; word sections together Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 01-07-2018, 03:55 PM
puff puff is offline &quot;Stitch&quot; word sections together Windows 7 64bit &quot;Stitch&quot; word sections together Office 2013
Advanced Beginner
&quot;Stitch&quot; word sections together
 
Join Date: Apr 2017
Posts: 60
puff is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
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
It's actually more complicated than this since the document actually doesn't have "|" to indicate where the repeat starts. Although I can somehow ask the software to insert a "|" before the repeat, it's not promising. So that is why I want to use a combination words rather than just 1 word for comparing.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
&quot;Stitch&quot; word sections together Unprotect sections in a word document using VBA dyb Word VBA 5 02-17-2022 01:43 AM
&quot;Stitch&quot; word sections together 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

Other Forums: Access Forums

All times are GMT -7. The time now is 12:50 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