PDA

View Full Version : [SOLVED:] Cycle through sentences help.



Kilroy
01-23-2018, 08:30 AM
I have this bit of code that will bold a sentence up to ":" This code only works on the sentence the cursor is in. I've been trying for a few hours to figure out how make cycle through every sentence. Generally I can use this macro on a regular document but there will be times I may need to run it on a table or mixed document. Any guidance appreciated.


Sub SelectSentence1()
With Selection
.StartIsActive = False
.Extend Character:=":"
End With
With Selection
.BoldRun
End With
End Sub

Kilroy
01-23-2018, 08:41 AM
I modified a bit of code I had for something else but it only works on paragraphs not sentences.


Sub BoldLine()
a1 = vbCr
B2 = ":"

ActiveDocument.range(0).Select
Selection.Find.ClearFormatting
While Selection.Find.Execute(a1)
StartReformat = Selection.Start 'end
Selection.MoveRight
Selection.Find.Execute (B2)
StopReformat = Selection.End 'Start
Selection.MoveRight

With ActiveDocument.range(StartReformat, StopReformat)
.Bold = True

End With
Wend
Set oDoc = Nothing: Set ThisDoc = Nothing
End Sub


I should I suppose that I can't go back to the first word with a capital as what I'm trying to bold almost always has more than 1 word capitalized:

Accept/Approval/Review: To verify adequacy, completeness and correctness and indicated by stamp, signature or initials and date.

Acceptance Criteria: Specified limits placed on the performance, results or other characteristics of an item, process or service defined in codes, standards, or other requirement documents.

macropod
01-23-2018, 11:33 AM
Although it has a Sentences collection, VBA has no idea what a grammatical sentence is. For example, consider the following:
Mr. Smith spent $1,234.56 at Dr. John's Grocery Store, to buy: 10.25kg of potatoes; 10kg of avocados; and 15.1kg of Mrs. Green's Mt. Pleasant macadamia nuts.
For you and me, that would count as one sentence; for VBA it counts as 5 sentences.

Accordingly, unless the sentences containing the ':' are fairly simple, you're unlikely to get the results you want that way.

For what you've posted, though, you should be able to get the desired result using a wildcard Find/Replace, where:
Find = Accept[!^13^l^t]@:
Replace= ^&
and you set the replacement font attribute to bold.

Kilroy
01-24-2018, 05:29 AM
Thanks for the reply Paul. When I run the macro it fails on the first attempt, works on the first 2 instances found on the second attempt and no changes after that no matter how many times I run it after that. I'm wondering if I'm not using the find replace properly?


Sub Macro1()
'
' Macro1 Macro
' findreplace
'
Selection.Find.ClearFormatting

Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Bold = True
With Selection.Find
.Text = "Accept[!^13^l^t]@:"
.Replacement.Text = "^&"
.Forward = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub


Even when opening the find/replace dialog box and using the "Accept[!^13^l^t]@:" in find and "^&" for replace it it functions the same way. Finds first 2 instances and that's it.

macropod
01-24-2018, 03:55 PM
The wildcard Find/Replace expression I supplied will work on all instances of what you posted on the first attempt - provided the insertion point isn't within one of those expressions. If you're not getting the desired results, that's because your content isn't the same as what you've posted here.

PS: Your code would be more efficient as:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Text = "Accept[!^13^l^t]@:"
.Format = True
.Forward = True
With .Replacement
.ClearFormatting
.Text = "^&"
.Font.Bold = True
End With
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End With
Application.ScreenUpdating = True
End Sub

Kilroy
01-25-2018, 07:08 AM
I copied a few definitions to a new document, mades sure all formatting was the same. Something odd started happening with the new code. When I run it, it still only works on the first two instances but now it is adding a copy of the bolded words in front of the original unbolded words. Also I've trying to break down the statement ""Accept[!^13^l^t]@:"" without success. I realize that it probably says everything in the sentence up to and including the ":" but what does the following mean in the above line:

! = ?
^13 = ?
^l = ?
^t = ?

Any guidance appreciated

macropod
01-25-2018, 01:39 PM
Your definitions test document only has two paragraphs that conform to your description in post #2. The rest begin with a string other than 'Accept'. The Find expression looks for strings beginning with 'Accept' and ending with ':'. To see what the wildcard Find terms are, go to: https://support.office.com/en-us/article/Find-and-replace-text-and-other-data-in-a-Word-document-c6728c16-469e-43cd-afe4-7708c6c779b7

Kilroy
01-25-2018, 02:16 PM
Lol I actually thought the "accept" in your code was a programming term. I get documents with up to 30 pages of definitions to be used for that document. They could start with anything from A-Z. Man am I silly for not noticing that. Just to give out some humour at my expense, I even searched "Accept[!^13^l^t]@:" to try and figure out what it meant. Paul thanks for the link I will read through it.

Now that I know that Accept was the starting point I changed it to "vbCr" followed by a capital letter and it works great on the test document I attached but still on the original only works on the first two instances so when I run it a second time it is still putting a bolded copy of the text in front of the unbolded text. very weird..

So, after reading portions from the link you provided does "[!^13^l^t]@:" mean Find everything up to the ":" except for a paragraph mark, a manual line break, and a tab character?



Sub Demo12()
Application.ScreenUpdating = False
With ActiveDocument.range
With .Find
.ClearFormatting
.Text = vbCr & "[A-Z][!^13^l^t]@:"
.format = True
.Forward = True
With .Replacement
.ClearFormatting
.Text = "^&"
.Font.Bold = True
End With
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End With
Application.ScreenUpdating = True
End Sub

macropod
01-25-2018, 02:43 PM
You could use:
.Text = "^13[!^13^l^t]@:"

Just be aware that, regardless of whether you use:
.Text = vbCr & "[!^13^l^t]@:"
or:
.Text = "^13[!^13^l^t]@:"
you will now be bolding the preceding paragraph break as well. To overcome that, you'd need to use something like:

Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^13[!^13^l^t]@:"
.Replacement.Text = ""
.Forward = True
.Format = False
.Wrap = wdFindStop
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
i = i + 1
.Start = .Start + 1
.Font.Bold = True
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
MsgBox i & " terms processed."
End Sub

Kilroy
01-26-2018, 07:42 AM
Works perfect Thanks for lesson Paul. Much appreciated.