Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-30-2015, 03:40 AM
Marco Marco is offline finding a string containing a combination of formats and adding text before Windows Vista finding a string containing a combination of formats and adding text before Office 2007
Novice
finding a string containing a combination of formats and adding text before
 
Join Date: Apr 2015
Posts: 15
Marco is on a distinguished road
Default finding a string containing a combination of formats and adding text before

I have a text with several paragraphs.


For each of them I trying to find the following combinations, and put before them a particular character:

[space][one italic character]
should become:
§[found text]

[one italic character][space][one non italic character]
should become:
@[found text]

for example "Aaaaa bbbb cccc" should become "Aaaa§ bbbb@ cccc"

Any idea? thanks in advance
Reply With Quote
  #2  
Old 11-02-2015, 05:08 PM
macropod's Avatar
macropod macropod is offline finding a string containing a combination of formats and adding text before Windows 7 64bit finding a string containing a combination of formats and adding text before Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You could use a macro like:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .Font.Italic = True
    .Execute
  End With
  Do While .Find.Found
    i = i + 1
    If .Characters.First = " " Then
      .Characters.First.Font.Italic = False
      .Start = .Start + 1
    End If
    .Characters.First.Previous.InsertBefore "§"
    If .Characters.Last = " " Then
      .Characters.Last.Font.Italic = False
      .End = .End - 1
    End If
    .Characters.Last.InsertAfter "@"
    .Characters.Last.Font.Italic = False
    .End = .End + 1
    'The next line is only needed if the Find is based on formatting without regard to text
    If .End = ActiveDocument.Range.End Then Exit Sub
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
MsgBox i & " instances found."
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
finding a string containing a combination of formats and adding text before Retain source format even on combination of multiple documents of different formats G.Anusha Word 12 12-14-2021 03:31 PM
Is it possible to have a style apply two formats based upon text? DMcCollum Word 3 05-02-2015 06:29 PM
finding a string containing a combination of formats and adding text before Way to search for a string in text file, pull out everything until another string? omahadivision Excel Programming 12 11-23-2013 12:10 PM
finding a string containing a combination of formats and adding text before One line of text - two formats? judicial85 Word 4 02-18-2011 04:24 AM
Forms: combination of a list and a text field to be filled in bart014 Word 0 04-23-2010 12:55 AM

Other Forums: Access Forums

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