Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #4  
Old 11-09-2020, 04:21 PM
Guessed's Avatar
Guessed Guessed is offline Insert Comments from List for Whole Document and for Only Selected Portion of Document Windows 10 Insert Comments from List for Whole Document and for Only Selected Portion of Document 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

I added some structure to both your docs to make the coding more forgiving. In the DocToCheck, I bookmarked the range where your list is. In the PrefsDoc I added tables to restrict the two separate loops more easily. Then the following code should do what you asked.
Code:
Sub Highlight()

  Application.ScreenUpdating = False
  
  Dim strCheckDoc As String, docRef As Document, docCurrent As Document
  Dim wrdRef As String, wrdPara As Paragraph, strKeyword As String, strRule As String
  Dim cmtRuleComment As Comment, tbl1 As Table, tbl2 As Table, aRow As Row, aRng As Range
  
  Set docCurrent = ActiveDocument
  strCheckDoc = docCurrent.Path & "\PreferencesDocument1.docx"
  Debug.Print strCheckDoc
  Set docRef = Documents.Open(strCheckDoc, ReadOnly:=True, Visible:=False)
  If docRef.Tables.Count > 1 Then
    Set tbl1 = docRef.Tables(1)
    Set tbl2 = docRef.Tables(2)
  End If
  
  For Each aRow In tbl1.Rows
    strKeyword = Split(Trim(aRow.Range.Cells(1).Range.Text), vbCr)(0)
    strRule = Split(Trim(aRow.Cells(2).Range.Text), vbCr)(0)
    If strKeyword <> "" Then
      Set aRng = docCurrent.Range
      With aRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Forward = True
        .Format = True
        .MatchWholeWord = True
        .MatchCase = False
        .MatchWildcards = False
        .Text = strKeyword
        Do While .Execute
          aRng.HighlightColorIndex = wdTurquoise
          If strRule <> "" Then
            Set cmtRuleComment = docCurrent.Comments.Add(Range:=aRng, Text:=strRule)
            cmtRuleComment.Author = UCase("WordCheck")
            cmtRuleComment.Initial = UCase("WC")
          End If
        Loop
      End With
    End If
  Next aRow
  
  For Each aRow In tbl2.Rows
    strKeyword = Split(Trim(aRow.Range.Cells(1).Range.Text), vbCr)(0)
    strRule = Split(Trim(aRow.Cells(2).Range.Text), vbCr)(0)
    If strKeyword <> "" Then
      Set aRng = docCurrent.Bookmarks("ListArea").Range
      With aRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Forward = True
        .Format = True
        .MatchWholeWord = True
        .MatchCase = False
        .MatchWildcards = False
        .Text = strKeyword
        Do While .Execute
          aRng.HighlightColorIndex = wdYellow
          If strRule <> "" Then
            Set cmtRuleComment = docCurrent.Comments.Add(Range:=aRng, Text:=strRule)
            cmtRuleComment.Author = UCase("WordCheck")
            cmtRuleComment.Initial = UCase("WC")
          End If
        Loop
      End With
    End If
  Next aRow
  
  docRef.Close
  docCurrent.Activate
  
  Application.ScreenUpdating = True
  
  Exit Sub

End Sub
Attached Files
File Type: docx DocumentToCheck1.docx (14.3 KB, 7 views)
File Type: docx PreferencesDocument1.docx (12.6 KB, 8 views)
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reveal portion of document based on dropdown selection chappeja Word VBA 1 03-27-2019 08:36 PM
Insert Comments from List for Whole Document and for Only Selected Portion of Document Renumber document at annex portion of document jeffreybrown Word 3 02-14-2019 05:53 AM
How do you add XML Schema Tags to Certain portion of a Word Document? Anishgirijan Word VBA 0 02-08-2017 10:38 PM
Insert Comments from List for Whole Document and for Only Selected Portion of Document How can I link a portion of a word document to another word document and have it auto update? __Data Word 1 06-30-2015 11:03 PM
Save as PDF only saves portion of document pandro Word 0 10-08-2014 06:54 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:31 AM.


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