#1
|
|||
|
|||
Adding exceptions to macro
Is there any way to add exceptions to a macro? For example, I´m using a macro that affects to all content in a document but dont want to apply it to certain elements such as hyperlinks or graphics. If this is possible, what´s the code to insert for that?
|
#2
|
||||
|
||||
There is lots of ways to add exceptions but you haven't provided enough information on what your code is actually doing.
Post your code and describe the problem clearly so someone can help you.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Sorry, this the macro where I want to insert the exception.
I want to change font color to red. The exception are the hyperlinks. Any URL in the document should keep their color without changing to red. Sub Macro1() Selection.Find.ClearFormatting Selection.Find.Font.Color = wdColorAutomatic Selection.Find.Replacement.ClearFormatting Selection.Find.Replacement.Font.Color = wdColorRed Selection.Find.Execute Replace:=wdReplaceAll End Sub Thanks! |
#4
|
||||
|
||||
Code:
If Not Selection.Range.Hyperlinks.Count > 0 Then Selection.Find.Replacement.Font.Color = wdColorRed End If
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
|||
|
|||
It works. Thanks a million!
What if the exception is for all tables in a document? I mean, preserving the color formatting in all content within a table. What´s the exception code for that? |
#6
|
||||
|
||||
Dita
If you want to learn to code for exceptions you need to study code already on this forum to see how people have dealt with issues. For instance, a good example of dealing with table content and exceptions can be seen in this thread https://www.msofficeforums.com/word-...ll-tables.html The code Graham provided is very simplistic and provides a 'test' but doesn't selectively apply formatting to the non-hyperlink parts of the selection. It is an 'all or nothing' approach which looks to see if there are ANY hyperlinks in the selection and if not, formats the whole selection. To apply the colour to chunks of the selection which aren't hyperlinks requires a lot more code and could be approached in few different ways. Probably the easiest would be to format everything selected as red and then loop through any hyperlinks and turn them back to your preferred colour.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#7
|
|||
|
|||
Very interesting link to learn about the exceptions. Thanks you both for your help!
|
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Adding another macro in the macro list | Marcia | Excel Programming | 6 | 11-07-2018 04:13 AM |
Making exceptions to PROPER | Chayes | Excel Programming | 8 | 06-05-2018 07:19 AM |
Exceptions on my documents | Martinod | Word | 1 | 03-12-2018 11:53 AM |
Adding to a macro | Ulodesk | Word VBA | 4 | 03-07-2016 01:40 PM |
Autocorrect Exceptions not working | danvdr | Word | 6 | 05-26-2014 09:37 PM |