MOHAMMED NUMAN
MOHAMMED NUMAN

Reputation: 183

Word macros are painfully slow

Iam just starting with using the word macros. I have a problem to identify all cross references and hyperlinks in the word document. Iam using a macro to solve this problem

The macro i have written is.

For Each fld In ActiveDocument.Fields
If fld.Type = wdFieldRef Or fld.Type = wdFieldHyperlink Then
    fld.Select
    For Each ch In Selection.Characters
        ch.HighlightColorIndex = wdYellow
    Next
End If
Next

This is the macro I have written, it is working as expected but it is too slow that i cannot use it.

There must be an efficient way to solve this problem. If yes please let me know-how.Any reference to sites so that I can refer would also be ok.

Thanks

Upvotes: 0

Views: 841

Answers (1)

macropod
macropod

Reputation: 13515

Disable screen updating and avoid selecting anything wherever possible. For example:

Sub Demo()
Application.ScreenUpdating = False
Dim Fld As Field
For Each Fld In ActiveDocument.Fields
  With Fld
    Select Case .Type
      Case wdFieldRef, wdFieldPageRef, wdFieldHyperlink
        .Result.HighlightColorIndex = wdYellow
    End Select
  End With
Next
Application.ScreenUpdating = True
End Sub

Upvotes: 2

Related Questions