Finding first word in a sentence

K

kashmir_16

Hi!

I'm trying to convert my cross references (figures and tables) to lower case
by changing the field codes to \*lower. However, it should only change this
for the references that are in a sentence and not for the references that
starts the sentence.

Right now all the cross references start with an upper case letter
(<text...Figure 2.1...text>), but it should look like:
<text....figure 2.1....text>
and
<Figure 2.1....text>

I found this macro, it converts all the references to lower case, but how
can I change it so that it excludes the references that are the first words
in a sentence?

Sub AddFirstCapSwitch()
ActiveWindow.View.ShowFieldCodes = True
With Selection.Find
.ClearFormatting
.Text = "^d REF"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = False

Do While .Execute
With Selection
.MoveRight Unit:=wdCharacter, Count:=1
.MoveLeft Unit:=wdCharacter, Count:=1
.TypeText Text:="\*lower"
.Fields.Update
End With

' prepare for next loop
Selection.Collapse wdCollapseEnd
Loop
End With
ActiveWindow.View.ShowFieldCodes = False
End Sub

Thanks
Nils
 
G

Graham Mayor

See my reply to your similar question in another thread.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
K

kashmir_16

That reply did work, but it changed all the references, the cross references
that are in the beginning of a sentence should still start with an upper case
letter. Or did I miss something?
 
G

Graham Mayor

OK that's easy enougfh to fix with a couple of extra lines. The following
version will ignore references that follow '.''?' and '!'

Sub AddLowerToXRef()
Dim oSection As Section
Dim oStory As Range
Dim oRng As Range
Dim oHeader As HeaderFooter
Dim oFooter As HeaderFooter
Dim oField As Field
With ActiveDocument
For Each oStory In .StoryRanges
For Each oField In oStory.Fields
If oField.Type = wdFieldRef Then
If InStr(1, oField.Code, "_Ref") Then
oField.Select
Set oRng = Selection.Range
oRng.Start = oRng.Start - 2
If oRng.Characters(1) <> "." _
And oRng.Characters(1) <> "!" _
And oRng.Characters(1) <> "?" Then
oField.Code.InsertAfter "\*Lower"
oField.Update
End If
End If
End If
Next oField
Next oStory
For Each oSection In .Sections
For Each oHeader In oSection.Headers
If oHeader.Exists Then
For Each oField In oHeader.Range.Fields
If oField.Type = wdFieldRef Then
If InStr(1, oField.Code, "_Ref") Then
oField.Select
Set oRng = Selection.Range
oRng.Start = oRng.Start - 2
If oRng.Characters(1) <> "." _
And oRng.Characters(1) <> "!" _
And oRng.Characters(1) <> "?" Then
oField.Code.InsertAfter "\*Lower"
oField.Update
End If
End If
End If
Next oField
End If
Next oHeader
For Each oFooter In oSection.Footers
If oFooter.Exists Then
For Each oField In oFooter.Range.Fields
If oField.Type = wdFieldRef Then
If InStr(1, oField.Code, "_Ref") Then
oField.Select
Set oRng = Selection.Range
oRng.Start = oRng.Start - 2
If oRng.Characters(1) <> "." _
And oRng.Characters(1) <> "!" _
And oRng.Characters(1) <> "?" Then
oField.Code.InsertAfter "\*Lower"
oField.Update
End If
End If
End If
Next oField
End If
Next oFooter
Next oSection
End With
End Sub
 
K

kashmir_16

Thanks, I guess it works good enough, however it misses some places where
ther is an enter strike before the reference, but it's not so time consuming
for me to fix this manually.

Thanks!
 
G

Graham Mayor

The macro can be modified to catch or miss whichever entries you require.
The following will ignore the fields that follow a paragraph or manual line
break, closing straight or smart quotes and will ignore the field if it is
the first entry in any story range.

Sub AddLowerToXRef()
Dim oSection As Section
Dim oStory As Range
Dim oRng As Range
Dim oHeader As HeaderFooter
Dim oFooter As HeaderFooter
Dim oField As Field
With ActiveDocument
For Each oStory In .StoryRanges
For Each oField In oStory.Fields
If oField.Type = wdFieldRef Then
If InStr(1, oField.Code, "_Ref") Then
oField.Select
Set oRng = Selection.Range
oRng.Start = oRng.Start - 2
If oRng.Start <> oStory.Start _
And oRng.Characters(1) <> "." _
And oRng.Characters(1) <> "!" _
And oRng.Characters(1) <> "?" _
And oRng.Characters(1) <> Chr(34) _
And oRng.Characters(1) <> Chr(148) _
And oRng.Characters(2) <> Chr(13) _
And oRng.Characters(2) <> Chr(11) Then
oField.Code.InsertAfter "\*Lower"
oField.Update
End If
End If
End If
Next oField
Next oStory
For Each oSection In .Sections
For Each oHeader In oSection.Headers
If oHeader.Exists Then
For Each oField In oHeader.Range.Fields
If oField.Type = wdFieldRef Then
If InStr(1, oField.Code, "_Ref") Then
oField.Select
Set oRng = Selection.Range
oRng.Start = oRng.Start - 2
If oRng.Start <> oHeader.Range.Start _
And oRng.Characters(1) <> "." _
And oRng.Characters(1) <> "!" _
And oRng.Characters(1) <> "?" _
And oRng.Characters(1) <> Chr(34) _
And oRng.Characters(1) <> Chr(148) _
And oRng.Characters(2) <> Chr(13) _
And oRng.Characters(2) <> Chr(11) Then
oField.Code.InsertAfter "\*Lower"
oField.Update
End If
End If
End If
Next oField
End If
Next oHeader
For Each oFooter In oSection.Footers
If oFooter.Exists Then
For Each oField In oFooter.Range.Fields
If oField.Type = wdFieldRef Then
If InStr(1, oField.Code, "_Ref") Then
oField.Select
Set oRng = Selection.Range
oRng.Start = oRng.Start - 2
If oRng.Start <> oFooter.Range.Start _
And oRng.Characters(1) <> "." _
And oRng.Characters(1) <> "!" _
And oRng.Characters(1) <> "?" _
And oRng.Characters(1) <> Chr(34) _
And oRng.Characters(1) <> Chr(148) _
And oRng.Characters(2) <> Chr(13) _
And oRng.Characters(2) <> Chr(11) Then
oField.Code.InsertAfter "\*Lower"
oField.Update
End If
End If
End If
Next oField
End If
Next oFooter
Next oSection
End With
End Sub
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
K

kashmir_16

Nice, thank you!

Graham Mayor said:
The macro can be modified to catch or miss whichever entries you require.
The following will ignore the fields that follow a paragraph or manual line
break, closing straight or smart quotes and will ignore the field if it is
the first entry in any story range.

Sub AddLowerToXRef()
Dim oSection As Section
Dim oStory As Range
Dim oRng As Range
Dim oHeader As HeaderFooter
Dim oFooter As HeaderFooter
Dim oField As Field
With ActiveDocument
For Each oStory In .StoryRanges
For Each oField In oStory.Fields
If oField.Type = wdFieldRef Then
If InStr(1, oField.Code, "_Ref") Then
oField.Select
Set oRng = Selection.Range
oRng.Start = oRng.Start - 2
If oRng.Start <> oStory.Start _
And oRng.Characters(1) <> "." _
And oRng.Characters(1) <> "!" _
And oRng.Characters(1) <> "?" _
And oRng.Characters(1) <> Chr(34) _
And oRng.Characters(1) <> Chr(148) _
And oRng.Characters(2) <> Chr(13) _
And oRng.Characters(2) <> Chr(11) Then
oField.Code.InsertAfter "\*Lower"
oField.Update
End If
End If
End If
Next oField
Next oStory
For Each oSection In .Sections
For Each oHeader In oSection.Headers
If oHeader.Exists Then
For Each oField In oHeader.Range.Fields
If oField.Type = wdFieldRef Then
If InStr(1, oField.Code, "_Ref") Then
oField.Select
Set oRng = Selection.Range
oRng.Start = oRng.Start - 2
If oRng.Start <> oHeader.Range.Start _
And oRng.Characters(1) <> "." _
And oRng.Characters(1) <> "!" _
And oRng.Characters(1) <> "?" _
And oRng.Characters(1) <> Chr(34) _
And oRng.Characters(1) <> Chr(148) _
And oRng.Characters(2) <> Chr(13) _
And oRng.Characters(2) <> Chr(11) Then
oField.Code.InsertAfter "\*Lower"
oField.Update
End If
End If
End If
Next oField
End If
Next oHeader
For Each oFooter In oSection.Footers
If oFooter.Exists Then
For Each oField In oFooter.Range.Fields
If oField.Type = wdFieldRef Then
If InStr(1, oField.Code, "_Ref") Then
oField.Select
Set oRng = Selection.Range
oRng.Start = oRng.Start - 2
If oRng.Start <> oFooter.Range.Start _
And oRng.Characters(1) <> "." _
And oRng.Characters(1) <> "!" _
And oRng.Characters(1) <> "?" _
And oRng.Characters(1) <> Chr(34) _
And oRng.Characters(1) <> Chr(148) _
And oRng.Characters(2) <> Chr(13) _
And oRng.Characters(2) <> Chr(11) Then
oField.Code.InsertAfter "\*Lower"
oField.Update
End If
End If
End If
Next oField
End If
Next oFooter
Next oSection
End With
End Sub
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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