View Single Post
 
Old 10-30-2016, 10:46 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,363
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Adding comments and retrieving the contents of a second column from the table will both slow down the code's execution. The main reason for the slower execution is that the Find/Replace can't be done as a single operation using wdReplaceAll when adding comments; instead each found instance must be individually processed.

A possible workaround would be to add the comments to the text in the table (i.e. using only one column), which you would also highlight, then let the macro copy each cell's content to the clipboard and use for the replacement, thus:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim FRDoc As Document, strFnd As String, RngFnd As Range, i As Long
Set FRDoc = Documents.Open("C:\FilePath\Glossary.docx", _
  ReadOnly:=True, AddToRecentFiles:=False, Visible:=False)
With ActiveDocument.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Format = True
  .Replacement.Text = "^c"
  .MatchCase = True
  .MatchWholeWord = True
  .MatchWildcards = False
  .Wrap = wdFindContinue
  For i = 1 To FRDoc.Tables(1).Rows.Count
    Set RngFnd = FRDoc.Tables(1).Cell(i, 1).Range
    With RngFnd
      .End = .End - 1
      .Copy
      strFnd = .Text
    End With
    .Text = strFnd
    .Execute Replace:=wdReplaceAll
  Next
End With
Application.ScreenUpdating = True
End Sub
The drawback with this approach is that the format of the replacement will be the same as whatever is in the table.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote