Some help on break

D

Designingsally

I have posted previous wanted to know how to set line break I have finally
managed to write a code which serves my purpose. But i m unable to resolve 2
issue. I hope some help comes along.
1. If the Note is already as
Note
Hello

Macro is checking that again and giving line break. I don’t want that to
happen
Eg 2
Note
hi
2. Notice the word after Note, which is “h,†I want that in Upper case
always. I m trying oRng.Words.First.Characters.First.Case = wdUpperCase
but I still end up getting that in small letter. If its in small caps it can
ignore, which the macros does.

Equivalent code and what happens

If a sentence goes like this Note i m having fun. The macro changes as
Note
i m having fun

Code



Sub FindAndReplace()
'
' FindAndReplace Macro

Dim oRng As Range
Set oRng = ActiveDocument.Range
Dim sRep As String
Dim sFindText As String
Dim sRepText As String
Selection.FInd.ClearFormatting
Selection.FInd.Replacement.ClearFormatting
With Selection.FInd
.Text = "Note"
.Replacement.Text = "Note ^l"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
If oRng.Words.Last.Characters.First.Case <> wdUpperCase Then
oRng.Font.bold = False
oRng.Words.First.bold = True
oRng.Words.First.Characters.First.Case = wdUpperCase
End If

End With

Selection.FInd.Execute Replace:=wdReplaceAll
oRng.Words.Last.Characters.First.Case = wdUpperCase
' Go To Top
Selection.HomeKey Unit:=wdStory

End Sub
 
P

Pesach Shelnitz

Hi Sally,

Your macro inserts the line breaks in a replace-all operation. If you want
to do anything in the vicinity of the place where you insert a line break,
you need to insert the line breaks in a loop and stop for each one. Also, as
you have noted yourself, your search-and-replace code for inserting the line
break inserts a line break after Note even if there already is a line break.

Your macro also creates a Range object, but performs the search-and-replace
operation with the Selection object. The Range object is only used to format
the first word in the document because you don't have any code to move the
range to a different location.

If you change the search string to "Note " (the word Note followed by a
space) and the replacement string to "Note^l" (the word Note followed by a
line break without a space), a second line break will only be inserted when
the word Note and is followed by a space and then a line break. You can add
additional code to check for this situation and delete the duplicated line
break.

Then if you use your Range object instead of the Selection object and
perform replace-one replacements in a loop, you can extend the Range object
after each replacement to format the next word.

With these changes your code would be as follows.

Set oRng = ActiveDocument.Range
With oRng.Find
..Text = "Note "
..Replacement.Text = "Note^l"
..Forward = True
..Format = False
..MatchCase = False
..MatchWholeWord = False
..MatchWildcards = False
..MatchSoundsLike = False
..MatchAllWordForms = False
..Forward = True
..Wrap = wdFindStop
Do While .Execute(Replace:=wdReplaceOne) = True
oRng.MoveEnd Unit:=wdWord, Count:=1
oRng.Words.Last.Characters.First.Case = wdUpperCase
oRng.Collapse Direction:=wdCollapseEnd
Loop
End With

You can add the code to make the letter that is capitalized bold in the loop
if that is what you want to happen.
 

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