#1
|
|||
|
|||
Macro to Mark Text with Index Codes
Hi, I'm trying to write a macro that will go through an entire document, find any text in red font and mark that text with an index code. I've tried many different variations of the code below, but nothing works correctly. This latest attempt just inserts blank index codes at the top of the document, one for each instance of every word in red text. I clearly have no idea what I'm doing. Can anyone help? Thank you in advance for any information. Code:
Sub MarkIndexMacro() Dim MyRange As Range Selection.HomeKey Unit:=wdStory Set MyRange = ActiveDocument.Range With MyRange.Find .Font.Color = wdColorRed Do While .Execute ActiveDocument.Indexes.MarkEntry Range:=Selection.Range, Entry:=Selection.Range.Text Loop End With End Sub |
#2
|
||||
|
||||
You were almost right
Code:
Sub MarkIndexMacro() Dim myRange As Range Set myRange = ActiveDocument.Range With myRange.Find .Text = "" .Font.ColorIndex = wdRed Do While .Execute ActiveDocument.Indexes.MarkEntry Range:=myRange, Entry:=myRange.Text myRange.Collapse 0 Loop End With Set myRange = Nothing End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Hi, thanks for your response. I just tried the macro, and it seems to give me an infinite loop, with the XE codes repeating one right after another. I've experimented with modifying the code a few different ways, but I can't get it to stop looping. It also seems to put in a character that looks like a question mark in a box. I tried to copy and paste it here, but I can't.
Last edited by MyUserName123; 07-06-2020 at 11:35 PM. Reason: Edited to add more information. |
#4
|
||||
|
||||
The code looks about right.
Is the last thing in your test document red text? If you add this following line before myRange.Collapse, what is special about the selection when you step through the code? myRange.Select
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
Quote:
I tried adding the line you suggested, but the only difference seems to be that when I step through the code I can see that the text in red is selected, but the result of the macro is the same whether I use that line or not. It just ends up in an infinite loop and includes those weird question mark in the box symbols in the index codes. With the exception of the very first index code that gets inserted. I've been experimenting some more, and I think the problem might be that when the index code gets inserted, the curly braces surrounding it are red, so when it goes to move on to the next instance of red text, it stops on the index code and inserts another index code based on that, and it just keeps going. Is the solution to somehow turn the newly inserted index code font color to wdAuto? I've tried doing that in several different ways, but I can't get it to work. |
#6
|
||||
|
||||
Reboot the PC and try the code I posted again. Word VBA has a bad habit of holding on to error conditions, especially with regard to endless loops as in the case of your original code.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#7
|
|||
|
|||
Quote:
Hi, thanks for your response. I rebooted the computer and I just tried it again now and it did the same thing. It's kind of hard to explain what it's doing with seeing it, but it inserts one XE code with the text entry and puts the curly brackets in red. Then it inserts another XE code directly after that, also with the curly brackets in red but with XE XE and the text entry, then another one directly after that with XE XE XE and the text entry, etc. It seems to keep inserting XE codes one right after another but the number of XEs increases with each code it inserts. Maybe it's just one of those weird things. I haven't done much VBA in a while, not that I was ever very good at it, of course, but I do remember seeing some very strange things that just didn't seem to make any sense. |
#8
|
||||
|
||||
I tested the code and there is a difference when the trailing space is red vs when it isn't. If it is ONLY the word that is red, the code goes in to a loop. This doesn't happen if the trailing space is also red.
I haven't got time to work out the solution at the moment but will try to get back to it later today to come up with the workaround.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#9
|
|||
|
|||
Quote:
Hi, thanks for your response. That's very interesting. None of the words in my sample file had a red trailing space, but I just tried adding one to some of them, and then the macro seems to work until it hits an entry that doesn't have a red trailing space. And actually, something else I noticed while experimenting is that it goes into an infinite loop if there's a period immediately after the red text. And it doesn't seem to matter what color font the period is. It's really weird. |
#10
|
||||
|
||||
The easiest fix for the code is to temporarily hide the field codes while the macro is running.
Code:
Sub MarkIndexMacro() Dim myRange As Range, aFld As Field, sXE As String ActiveWindow.View.ShowHiddenText = False Set myRange = ActiveDocument.Range With myRange.Find .Font.Color = wdColorRed Do While .Execute sXE = Trim(myRange.Text) ActiveDocument.Indexes.MarkEntry Range:=myRange, Entry:=sXE myRange.Collapse Direction:=wdCollapseEnd Loop End With ActiveWindow.View.ShowHiddenText = True End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#11
|
|||
|
|||
Quote:
Hi, I tried your code with a couple of different files and it works great. Thank you for all your help. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro for mark index entry for every word in a document? | Jay Jay | Word VBA | 5 | 08-13-2015 03:44 PM |
Hyperlinking from the Index to the Origin Mark | Phil H | Word | 2 | 08-30-2013 04:03 AM |
Index Addition Leaves Hidden Codes On | SQLUSA | Word | 3 | 07-23-2012 02:58 AM |
Options of Index Mark Entry | smaria | Word | 1 | 04-22-2010 05:48 AM |
Macro to mark non-coloured/non-highlighted text as hidden | PeterB | Word | 0 | 10-28-2009 07:54 AM |