Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-16-2021, 01:43 AM
Guessed's Avatar
Guessed Guessed is offline Looping through comments is slow: inefficiently written code or par for the course? Windows 10 Looping through comments is slow: inefficiently written code or par for the course? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
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

Do you really need to be specific with the find highlight color? Isn't it constrained enough being in the comments, having the text "Andy:" and being highlighted?. Also you shouldn't need to loop through each comment if you search in the storyrange.
Code:
Sub FindReplace2() ' 05/14/2021
  Dim iFindColor As Integer, iReplaceColor As Integer, myComment As Comment
  
  'Application.ScreenUpdating = False
  
  ' (These variables are ordinarily set via input box
  ' 7 = yellow; 5 = pink; 4 = Bright Green; 3 = Turquoise (Bright Blue)):
  iFindColor = 5
  iReplaceColor = 4
                  
  'DoEvents ' For large docs, Word crashes without DoEvents
  Options.DefaultHighlightColorIndex = iReplaceColor
  With ActiveDocument.StoryRanges(wdCommentsStory).Find
    .ClearFormatting
    .Text = "Andy:"
    .Highlight = True
    .Replacement.Highlight = True
    .Forward = True
    .Execute Replace:=wdReplaceAll
  End With

  'Application.ScreenUpdating = True
  MsgBox "Done"
End Sub
If you still need to include the hilite color test then the macro drags a whole lot slower. This chunk looks cleaner than what you had but I don't know that it will be noticeably faster.
Code:
  With ActiveDocument.StoryRanges(wdCommentsStory).Find
    .ClearFormatting
    .Text = "Andy:"
    .Highlight = True
    .Replacement.Highlight = True
    .Forward = True
    .Execute
    Do While .Found = True
      If .Parent.HighlightColorIndex = iFindColor Then .Parent.HighlightColorIndex = iReplaceColor
      .Execute
    Loop
  End With

__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia

Last edited by Guessed; 05-16-2021 at 05:30 PM.
Reply With Quote
  #2  
Old 05-17-2021, 08:55 PM
Peterson Peterson is offline Looping through comments is slow: inefficiently written code or par for the course? Windows 10 Looping through comments is slow: inefficiently written code or par for the course? Office 2019
Competent Performer
Looping through comments is slow: inefficiently written code or par for the course?
 
Join Date: Jan 2017
Posts: 143
Peterson is on a distinguished road
Default

Thank you for taking a look at and reworking these, Andrew. I could've sworn there was a comments story, but when a quick online search didn't turn it up, I concluded I'd have to loop through each comment. Need to get better at using the object browser...

When searching for names, no, the highlight color isn't important. But I wanted to include the color-specific functionality so that the macro could be used for other purposes, e.g., we occasionally have large, multi-author documents in which highlight colors are used to denote various things, and it'd be useful to be able to selectively clear them.

Thanks again.


EDIT: Your version that tests color ran through my doc with ~430 comments in 15 seconds.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping through comments is slow: inefficiently written code or par for the course? Error 4605 when looping through files in folder and deleting comments Peterson Word VBA 2 04-19-2018 08:45 AM
Specific Macro Written brent_bris Excel Programming 0 02-20-2017 05:55 AM
Looping through comments is slow: inefficiently written code or par for the course? Slow internet = Slow Word... User12344321 Word 4 09-21-2015 12:54 PM
Looping through comments is slow: inefficiently written code or par for the course? Need a formula written for me Dbjenkin Excel 1 05-13-2014 05:46 AM
Code Error - Hiding comments and revisions silvrwoman Word VBA 6 03-24-2012 10:15 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:20 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft