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?
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?