Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-23-2014, 10:32 AM
RupertM RupertM is offline Changing color font between 2 specific flags Windows 7 64bit Changing color font between 2 specific flags Office 2013
Novice
Changing color font between 2 specific flags
 
Join Date: May 2014
Posts: 3
RupertM is on a distinguished road
Default Changing color font between 2 specific flags

Hi everyone,



Just as a brief introduction, I'm comfortable using VBA on Excel and doing all sorts of interesting stuff, but I never used VBA on Word. I am currently struggling with the following situation:

what I'd like to, is to build a macro that will change color (and size) for the text between 2 specific flags.

For example, if my text is:
Et quoniam mirari posse quosdam peregrinos existimo haec lecturos forsitan, si contigerit, quamobrem cum oratio ad ea monstranda deflexerit quae Romae gererentur.
/Ex: nihil praeter seditiones narratur et tabernas et vilitates harum similis alias, summatim causas perstringam nusquam a veritate sponte propria digressurus./Ex


I want Word to see the flag "/Ex:" and to apply a specific change in color and size to all the text between "/Ex:" and the second flag "/Ex". (And that for the whole document of course.)

I've no idea where to start with. I browsed the net but only found topics that use a find/replace function which is not applicable in my case I think.
I also thought about a macro built like a loop, selecting each word and checking if it's a flag or not, but first, I don't really know how to compute it, and second I don't think this is a very optimized solution.

I will appreciate any kind of help!
Thanks
Reply With Quote
  #2  
Old 05-23-2014, 03:26 PM
macropod's Avatar
macropod macropod is offline Changing color font between 2 specific flags Windows 7 32bit Changing color font between 2 specific flags Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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 don't actually need a macro for what you've described; it could also be done with a wildcard Find/Replace, where:
Find = /Ex:*/Ex
Replace = "^&"
and you specify the replacement font colour. A macro equivalent is:
Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Replacement.Font.ColorIndex = wdRed
    .Text = "/Ex:*/Ex"
    .Replacement.Text = "^&"
    .Format = True
    .Forward = True
    .Wrap = wdFindContinue
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 05-24-2014, 01:43 AM
RupertM RupertM is offline Changing color font between 2 specific flags Windows 7 64bit Changing color font between 2 specific flags Office 2013
Novice
Changing color font between 2 specific flags
 
Join Date: May 2014
Posts: 3
RupertM is on a distinguished road
Default

Thanks for the answer macropod, I didn't know that function.
Your macro works fine, but surprisingly when I try doing it manually, it doesn't find anything.
Do you think it is linked to the language of my Office?
BTW what does ^& stand for?

Thanks in advance,
Reply With Quote
  #4  
Old 05-24-2014, 01:46 AM
macropod's Avatar
macropod macropod is offline Changing color font between 2 specific flags Windows 7 32bit Changing color font between 2 specific flags Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Quote:
Originally Posted by RupertM View Post
Your macro works fine, but surprisingly when I try doing it manually, it doesn't find anything.
Did you check the 'use wildcards' option?
Quote:
BTW what does ^& stand for?
That's a standard replace expression telling Word to use the found text for the replacement (i.e. don't change the text).
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 05-24-2014, 04:47 AM
RupertM RupertM is offline Changing color font between 2 specific flags Windows 7 64bit Changing color font between 2 specific flags Office 2013
Novice
Changing color font between 2 specific flags
 
Join Date: May 2014
Posts: 3
RupertM is on a distinguished road
Default

Ok my mistake, I didn't know what you were referring to by "wildcards". It's very clear now and works!

Thank you very much for your help macropod!
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
changing the font color dbsoccer Project 1 11-01-2013 06:21 AM
changing color aerospace Outlook 0 04-03-2013 05:56 AM
Changing bar color automatically Mahmuz PowerPoint 0 03-28-2012 11:49 PM
Changing the margins on a specific paragraph Xmosis Word 1 03-15-2011 02:04 PM
changing font size without changing leading carolns Word 1 09-14-2009 12:30 PM

Other Forums: Access Forums

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