Highlighting Sections

K

Kerry

I am using Word XP

How can I highlight all the text in just the current
section?

Thanks very much.
 
H

Helmut Weber

Hi Kerry,
in principle like this:
Dim s As Integer
Dim oRng As Range
selection.collapse
Set oRng = Selection.Range
oRng.Start = 0
oRng.End = Selection.Range.Start
s = oRng.Sections.Count ' the section the cursor is in
' so far so good
' either
ActiveDocument.Sections(s).Range.Select
Selection.Range.HighlightColorIndex = wdBlue
' or
ActiveDocument.Sections(s).Range.HighlightColorIndex = wdYellow
 
K

Kerry

Hi Helmut
Many thanks for the code, however, using your example to
start me off I am finding that all of my sections are
being coloured blue.

Any ideas - many thanks.

Kerry
 
K

Kerry

Sincere apologies, I have accidentally removed my section
breaks!! I think it must be nearly home time!!

I need to insert some text at the top of the section but
when I test my command I replace the entire text inthe
section rather than insert extra text - please see code
below:- Many thanks.

Sub IndSecTOC
Dim s As Integer
Dim oRng As Range
Selection.Collapse
Set oRng = Selection.Range
oRng.Start = 0
oRng.End = Selection.Range.Start
s = oRng.Sections.Count
ActiveDocument.Sections(s).Range.Select
Secbk = "toc \h \z \t ""section sub heading,2""
\b ""section" & s
'this bit of code is not correct but what should it be???
Selection.Range.Fields.Add Range:=oRng,
Type:=wdFieldEmpty, Text:=Secbk & """,
PreserveFormatting:=False"
Selection.Fields.Update
end sub
 
H

Helmut Weber

Hi Kerry,
just one of many and may be more elegant ways,
to insert a field at the start of the selection:
Option Explicit
Dim oDcm As Document
Dim oRng As Range
---
Sub Test444()
Dim s As Integer ' sections
Set oDcm = ActiveDocument
Selection.Collapse
Set oRng = Selection.Range
oRng.Start = 0
oRng.End = Selection.Range.Start
s = oRng.Sections.Count
ActiveDocument.Sections(s).Range.Select
Selection.Collapse ' !
Selection.Fields.Add _
Range:=Selection.Range, _
Type:=wdFieldEmpty, _
Text:="DOCPROPERTY LastSavedBy "
' or the field of your choice
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top