Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-04-2019, 05:26 PM
Chayes Chayes is offline VBA for selecting given text and moving the whole line Windows 7 32bit VBA for selecting given text and moving the whole line Office 2003
Advanced Beginner
VBA for selecting given text and moving the whole line
 
Join Date: May 2012
Posts: 79
Chayes is on a distinguished road
Default VBA for selecting given text and moving the whole line

Hi All


I have some text in which I'd like to select certain given text , and then place a return above and below so that the whole line it is in is isolated.



For example , the code would find and make bold every line in which the word 'Wrote:' is found in the text below , then put returns above and below :


Original

------------------------------------------------------------

On Tue, May 21, 2019 at 2:54 PM <wrote:


Random Text Random Text Random Text Random
Random Text Random Text Random Text Random
Random Text Random Text Random Text Random
Random Text Random Text Random Text Random
On 20/05/2019 15:41,wrote:
Random Text Random Text Random Text Random
Random Text Random Text Random Text Random
Random Text Random Text Random Text Random
Random Text Random Text Random Text Random
On 15/05/2019 16:03,wrote:
Random Text Random Text Random Text Random
Random Text Random Text Random Text Random
Random Text Random Text Random Text Random
Random Text Random Text Random Text Random


------------------------------------------------------------


After the running the code it would read :


----------------------------------------------------------



On Tue, May 21, 2019 at 2:54 PM <wrote:

Random Text Random Text Random Text Random
Random Text Random Text Random Text Random
Random Text Random Text Random Text Random
Random Text Random Text Random Text Random


On 20/05/2019 15:41,wrote:

Random Text Random Text Random Text Random
Random Text Random Text Random Text Random
Random Text Random Text Random Text Random
Random Text Random Text Random Text Random


On 15/05/2019 16:03,wrote:

Random Text Random Text Random Text Random
Random Text Random Text Random Text Random
Random Text Random Text Random Text Random
Random Text Random Text Random Text Random

--------------------------------------------------


Clearly the amount of occurrences of the chosen text could vary , and this gives an added complication.


Very grateful for any advice on this one. I've attached a sample file.



Thanks.
Attached Files
File Type: doc Sample3.doc (22.5 KB, 5 views)
Reply With Quote
  #2  
Old 06-04-2019, 07:57 PM
gmayor's Avatar
gmayor gmayor is offline VBA for selecting given text and moving the whole line Windows 10 VBA for selecting given text and moving the whole line Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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

You can do this with a simple replacement of
wrote:^p
with
^& Format > Style > Heading 1
and modify the style Heading 1 to produce the format and spacing to require.

If you want a macro you can record the steps e.g.

Code:
Sub Macro1()
    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Style = ActiveDocument.Styles("Heading 1")
    With Selection.Find
        .Text = "wrote:^p"
        .Replacement.Text = "^&"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    With ActiveDocument.Styles("Heading 1").Font
        .Name = "Times New Roman"
        .Size = 12
        .Bold = True
        .Italic = False
    End With
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 06-05-2019, 07:14 AM
Chayes Chayes is offline VBA for selecting given text and moving the whole line Windows 7 32bit VBA for selecting given text and moving the whole line Office 2003
Advanced Beginner
VBA for selecting given text and moving the whole line
 
Join Date: May 2012
Posts: 79
Chayes is on a distinguished road
Default

Hi


OK thanks for this. I'm grateful to you. It's working perfectly now.


^_^


Very Best Wishes ,


Colin
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Data point and box is not moving when selecting name from slicer JoemanNville Excel 0 02-23-2019 07:30 PM
VBA for selecting given text and moving the whole line Cursor positioning vs. selecting at end of line Alpha Zen Word 1 04-26-2017 02:56 AM
Moving Image Vertically Changes Line Spacing My-Kl Word 2 03-31-2017 03:14 PM
Selecting and moving text boxes identified by specific text. Chayes Word VBA 8 02-22-2016 07:01 AM
Selecting a Text Box gajesh Word 0 09-02-2009 11:45 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:19 PM.


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