Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-06-2020, 07:55 PM
MyUserName123 MyUserName123 is offline Macro to Mark Text with Index Codes Windows 10 Macro to Mark Text with Index Codes Office 2019
Novice
Macro to Mark Text with Index Codes
 
Join Date: Jul 2020
Posts: 10
MyUserName123 is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 07-06-2020, 10:48 PM
gmayor's Avatar
gmayor gmayor is offline Macro to Mark Text with Index Codes Windows 10 Macro to Mark Text with Index Codes 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 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
Reply With Quote
  #3  
Old 07-06-2020, 11:16 PM
MyUserName123 MyUserName123 is offline Macro to Mark Text with Index Codes Windows 10 Macro to Mark Text with Index Codes Office 2019
Novice
Macro to Mark Text with Index Codes
 
Join Date: Jul 2020
Posts: 10
MyUserName123 is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 07-07-2020, 12:38 AM
Guessed's Avatar
Guessed Guessed is offline Macro to Mark Text with Index Codes Windows 10 Macro to Mark Text with Index Codes Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #5  
Old 07-07-2020, 03:08 AM
MyUserName123 MyUserName123 is offline Macro to Mark Text with Index Codes Windows 10 Macro to Mark Text with Index Codes Office 2019
Novice
Macro to Mark Text with Index Codes
 
Join Date: Jul 2020
Posts: 10
MyUserName123 is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
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
Hi, thanks for your response. No, the last text in the document is not red. It's just the regular automatic font color. The test document I'm using is all in normal with no styles or formatting at all.

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.
Reply With Quote
  #6  
Old 07-07-2020, 04:16 AM
gmayor's Avatar
gmayor gmayor is offline Macro to Mark Text with Index Codes Windows 10 Macro to Mark Text with Index Codes 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

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
Reply With Quote
  #7  
Old 07-07-2020, 05:41 AM
MyUserName123 MyUserName123 is offline Macro to Mark Text with Index Codes Windows 10 Macro to Mark Text with Index Codes Office 2019
Novice
Macro to Mark Text with Index Codes
 
Join Date: Jul 2020
Posts: 10
MyUserName123 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
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.

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.
Reply With Quote
  #8  
Old 07-07-2020, 04:26 PM
Guessed's Avatar
Guessed Guessed is offline Macro to Mark Text with Index Codes Windows 10 Macro to Mark Text with Index Codes Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #9  
Old 07-07-2020, 06:31 PM
MyUserName123 MyUserName123 is offline Macro to Mark Text with Index Codes Windows 10 Macro to Mark Text with Index Codes Office 2019
Novice
Macro to Mark Text with Index Codes
 
Join Date: Jul 2020
Posts: 10
MyUserName123 is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
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.

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.
Reply With Quote
  #10  
Old 07-07-2020, 06:55 PM
Guessed's Avatar
Guessed Guessed is offline Macro to Mark Text with Index Codes Windows 10 Macro to Mark Text with Index Codes Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #11  
Old 07-07-2020, 07:59 PM
MyUserName123 MyUserName123 is offline Macro to Mark Text with Index Codes Windows 10 Macro to Mark Text with Index Codes Office 2019
Novice
Macro to Mark Text with Index Codes
 
Join Date: Jul 2020
Posts: 10
MyUserName123 is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
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

Hi, I tried your code with a couple of different files and it works great. Thank you for all your help.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to Mark Text with Index Codes 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

Other Forums: Access Forums

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