简体   繁体   中英

Insert highlighted sentence at bookmark in MS Word?

So I have a userform that I use to populate a template with text at certain bookmarks. To make it easy to see what text has been inserted, I would like it to be highlighted with yellow. Is there a convenient way to do this for every text that is inserted without typing it, selecting it and then highlighting it? As an example, this is what part of my code looks like atm:

With ActiveDocument
   Options.DefaultHighlightColorIndex = wdYellow
   '[highlight=yellow].Bookmarks("Modtager").Range.Text = TxtModtager.Value[/highlight]
  .Bookmarks("Modtager").Range.Text = TxtModtager.Value
  .Bookmarks("KSnr1").Range.Text = txtKSnr.Value
  .Bookmarks("KSnr2").Range.Text = txtKSnr.Value

The first line doesn't seem to do anything - even without the option turned on new text is still not highlighted.

The second line is something I found at another site but had to be commented as it's not working.

The last three lines insert the actual text and I guess you could choose to select the bookmark first and then Selection.TypeText Text:="Whatever value I need", followed by selecting the new phrase again (how?) and choose .HighlightColorIndex = wdYellow.

There should be a better way though, any suggestions? :)

This works for me:

SetBkmkText "Modtager", TxtModtager.Value
SetBkmkText "KSnr1", txtKSnr.Value
SetBkmkText "KSnr2", txtKSnr.Value


Sub SetBkmkText(bkmk as String, NewText as String)
    With ActiveDocument.Bookmarks(bkmk).Range
        .Text = NewText 
        .HighlightColorIndex = wdYellow
    End With
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM