PDA

View Full Version : Solved: Remove highlighting in all open documents



clhare
01-15-2007, 11:09 AM
I have a template that cycles through all open documents and performs some text replacements, then saves the files. I need to add code that will also remove all highlighting from the documents.

I added the code I found on the MVP site, but the highlighting is still in the documents after the macro runs.

Here's the code I use to cycle through the docs. The code for removing the highlighting is near the end:

' Update one document at a time
'For Each doc In Documents
For Each objDoc In Application.Documents
objDoc.Activate

' Make sure document is unlocked
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect
End If
' Replace variables with user's text
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[Premier Company] Benefits Center"
.Replacement.Text = strBCName
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "[Premier Company]"
.Replacement.Text = strClientName
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Assign values to variables
strOldFilename1 = ActiveDocument.Name
If strVersion = "3x4x" Then
' Take old filename minus last 11 characters (which would be ' (3x4x).dot')
strNewFilename_Short1 = Left(strOldFilename1, Len(strOldFilename1) - 11)
' Take revised filename minus first 3 characters (which would be ' QM')
strNewFilename_Short2 = Right(strNewFilename_Short1, Len(strNewFilename_Short1) - 3)
ElseIf strVersion = "4x" Then
' Take old filename minus last 9 characters (which would be ' (4x).dot')
strNewFilename_Short1 = Left(strOldFilename1, Len(strOldFilename1) - 9)
' Take revised filename minus first 3 characters (which would be ' QM')
strNewFilename_Short2 = Right(strNewFilename_Short1, Len(strNewFilename_Short1) - 3)
End If
' Add backslash to location to get new file in correct folder
strLocation = strLocation & "\"
' Save under corrected filename
ActiveDocument.SaveAs FileName:=strLocation & strNewFilename_Short2 _
, FileFormat:=wdFormatTemplate, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False

' Assign values to variables
strOldFilename2 = ActiveDocument.Name
' Take old filename minus last 4 characters (which would be '.dot')
strNewFilename_Short3 = Left(strOldFilename2, Len(strOldFilename2) - 4)
' Reset filename in Properties
With ActiveDocument
.BuiltInDocumentProperties(wdPropertyTitle) = strNewFilename_Short3
End With

' Remove all highlighting in document
Dim StoryRange As Range
For Each StoryRange In ActiveDocument.StoryRanges
StoryRange.HighlightColorIndex = wdNoHighlight
Next StoryRange

' Lock the document to allow user to tab through fields
ActiveDocument.Protect Type:=wdAllowOnlyFormFields
' Save and close new file
ActiveDocument.Close savechanges:=wdSaveChanges
' Update next active document
Next objDoc

Any suggestions on how to get the code to removing the highlighting to work?

fumei
01-15-2007, 11:50 AM
Dim StoryRange As Range
For Each StoryRange In ActiveDocument.StoryRanges
StoryRange.HighlightColorIndex = wdNoHighlight
Next StoryRange works for me.

1. Please be specific about what is happening. Are you saying the code runs with no errors, but nothing happens with the highlighting? Are you saying you are getting some error when the code runs. What happens when you step through the code?

2. As you are not asking questions regarding the other parts of the code, posting it - for now - just adds stuff to look at. You are not posting all of it - as there are variables and actioons obviously going on outside of the posted chunk. So keep it to what is the problem.

3. As stated, the code for removing highlighting should work, and does for me, by itself. I would suggest you be a bit more careful with your names.

You are using "StoryRange" as a name for a Range. then you use that range object to apply to the ranges of ActiveDocument.StoryRanges. If I may put it another way:

For Each StoryRange(which is not a StoryRange, but a Range) In ActiveDocument.StoryRanges

Have you tried to put just the highlight removing code into a document (with highlighting) and run it? Does it work?

clhare
01-15-2007, 12:48 PM
Sorry for the confusion. This is part of the code from my other post today (re the "Object variable or with block variable not set"). So, I do get an error, but not related to the highlighting.

All the actions in the code seem to be working except the highlighting. I did try running just the code for removing the highlighting as a separate macro and it worked fine. It's only when I added the code to the existing macro that the highlighting doesn't get removed any more.

mileski
01-15-2007, 02:00 PM
If you use the function "Application.ScreenUpdating" as in the previous post, try commenting the lines out where the function exists.

And see what happens.

fumei
01-15-2007, 02:25 PM
You may also want to add a line Application.ScreenRefresh at the end of each loop.

clhare
01-17-2007, 06:51 AM
Awesome! I added the code to refresh the screen at the end of the loop and now all the highlighting is removed.

Thank you so much!

fumei
01-17-2007, 10:57 AM
Actually, the highlighting WAS removed. Remember you are using a Range to do this, not Selection. And that is the correct (or "better") way to do it. Using range is better than Selection for most things. However, Range actions do not always change the user interface. Sometimes they do, sometimes they don't. Refresh simply makes sure the screen shows the current status.

Range actions MOST of the time do in fact show immediately. However, when working with Ranges, if it does not seem to, the rule of thumb is toss in a Refresh.

clhare
01-17-2007, 01:12 PM
Thank you so much for your help! I will remember to use Refresh in future.