PDA

View Full Version : Selecting a Word in a Paragraph



heedaf
07-14-2017, 02:14 PM
I need to determine if a word in a paragraph is bolded or not. My idea is to select (highlight) the word in the paragraph and check its bold status. I need the correct way of doing the following (I know the range of numbers of where the word is located in the paragraph).


wApp.ActiveDocument.paragraphs(i).range(10,15).select

Can anyone tell me the correct way of doing it would be?

gmaxey
07-14-2017, 04:41 PM
You don't have to select anything:


ub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 7/14/2017
Dim oRng As Range
Dim i and Long
i = 1
Set oRng = ActiveDocument.Paragraphs(i).Range
oRng.Start = 10: oRng.End = 15
Select Case oRng.Font.Bold
Case 0: MsgBox "Not bold"
Case -1: MsgBox "Bold"
Case Else: MsgBox "Mixed bag"
End Select
lbl_Exit:
Exit Sub
End Sub

heedaf
07-14-2017, 04:55 PM
Thank you for the reply. I've tried that and it doesn't give me the paragraph I've selected but uses data from the very first paragraph.

gmaxey
07-14-2017, 05:10 PM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 7/14/2017
Dim oRng As Range

Set oRng = Selection.Paragraphs(1).Range
oRng.Start = 10: oRng.End = 15
Select Case oRng.Font.Bold
Case 0: MsgBox "Not bold"
Case -1: MsgBox "Bold"
Case Else: MsgBox "Mixed bag"
End Select
lbl_Exit:
Exit Sub
End Sub


I was confused. Considering all of the posts and advice that you have offered here the past two days, I assumed that you already know how to write code.

heedaf
07-14-2017, 07:08 PM
How does saying that it doesn't work mean that I don't know how to code?

gmaxey
07-14-2017, 07:27 PM
Your saying that is doesn't work when in the code written for you ...

i = 1
Set oRng = ActiveDocument.Paragraphs(i).Range

... was an indicator. You were the one who introduced, in your initial post, the undeclared and undefined variable i. You said nothing about selecting a paragraph or what i was or should be. In order for it to be something, I set it = 1 and therefore the range evaluated for bold font in my first reply is in paragraph 1.

But to show that none of us are perfect, I did fail to actually test the revised code. Which you are correct in pointing out that it doesn't work:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 7/14/2017
Dim oRng As Range

Set oRng = Selection.Paragraphs(1).Range
oRng.Start = Selection.Paragraphs(1).Range.Start + 10
oRng.End = Selection.Paragraphs(1).Range.Start + 15
oRng.Select
Select Case oRng.Font.Bold
Case 0: MsgBox "Not bold"
Case -1: MsgBox "Bold"
Case Else: MsgBox "Mixed bag"
End Select
lbl_Exit:
Exit Sub
End Sub

heedaf
07-14-2017, 08:30 PM
Greg, you are awesome. I looked all day to try and find a solution. For some reason the use of "Selection" didn't work for me but ActiveDocument did, any idea why (it did work with "1" but gave an error on anything larger)? I included the modified code that worked for me.


Sub test()
'
' test Macro
' 'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 7/14/2017
Dim oRng As Range
i = 2
Set oRng = ActiveDocument.Paragraphs(i).Range
oRng.Select
oRng.Start = ActiveDocument.Paragraphs(i).Range.Start + 10
oRng.End = ActiveDocument.Paragraphs(i).Range.Start + 15
oRng.Select
Select Case oRng.Font.Bold
Case 0: MsgBox "Not bold"
Case -1: MsgBox "Bold"
Case Else: MsgBox "Mixed bag"
End Select
lbl_Exit:
Exit Sub
End Sub

gmayor
07-14-2017, 08:55 PM
Selection wouldn't have worked unless the whole document was selected. There can only ever be one paragraph related to a point selection. As you are using ranges, there is no need to select at all unless you want to check that the correct range is being evaluated.
The following should be simpler

Sub test2()

Dim oRng As Range
Dim i As Integer 'variable was not declared
i = 2
Set oRng = ActiveDocument.Paragraphs(i).Range
oRng.Start = oRng.Start + 10
oRng.End = oRng.Start + 5 'start has moved so only 5 chars after new start
'oRng.Select 'This line is not required, but will show which text is being checked
Select Case oRng.Font.Bold
Case 0: MsgBox "Not bold"
Case -1: MsgBox "Bold"
Case Else: MsgBox "Mixed bag"
End Select
lbl_Exit:
Exit Sub
End Sub

gmaxey
07-15-2017, 06:12 AM
Graham,

Not really understanding the precise goal of the OP, I did make a dog's breakfast out of this by not adequately testing what I posted. Thanks for jumping in!

heedaf,

If "... use of Selection didn't work for me ..." means:

Set oRng = Selection.Paragraphs(#).Range where # is some variable number and the error is "The requested member of the collection does not exists" then as Graham points out, a single point selection (i.e., the cursor flickering in a paragraph) or any selection that doesn't span 2 or more paragraphs can only have 1 paragraph.

If you want to evaluate a selected paragraph you can use:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 7/14/2017
Dim oRng As Range
Set oRng = Selection.Paragraphs(1).Range
oRng.Start = oRng.Start + 10
oRng.End = oRng.Start + 5
Select Case oRng.Font.Bold
Case 0: MsgBox "Not bold"
Case -1: MsgBox "Bold"
Case Else: MsgBox "Mixed bag"
End Select
lbl_Exit:
Exit Sub
End Sub

If you want to evaluated all paragraphs you can use:


Sub test2()
Dim oRng As Range
Dim lngIndex As Long
For lngIndex = 1 To ActiveDocument.Paragraphs.Count
Set oRng = ActiveDocument.Paragraphs(lngIndex).Range
oRng.Start = oRng.Start + 10
oRng.End = oRng.Start + 5
Select Case oRng.Font.Bold
Case 0: MsgBox "Paragrah " & lngIndex & " is Not bold"
Case -1: MsgBox "Paragrah " & lngIndex & " is Bold"
Case Else: MsgBox "Paragrah " & lngIndex & " is a mixed bag"
End Select
Next lngIndex
lbl_Exit:
Exit Sub
End Sub

heedaf
07-18-2017, 01:05 PM
In the previous question I new where the word was but now I would like to test each word in the paragraph for different formating options (strike through). With the range is there a way of cycling through the words in a paragraph?

gmaxey
07-18-2017, 01:31 PM
So what kind of code is it that you know how to write?


Sub ScratchMacro()
Dim oWord As Range
Dim oRng As Range
Set oRng = Selection.Paragraphs(1).Range
For Each oWord In oRng.Words
Select Case oWord.Font.StrikeThrough
Case 0: MsgBox "Not strkethrough"
Case -1: MsgBox "Strikethrough"
Case Else: MsgBox "Mixed bag"
End Select
Next
lbl_Exit:
Exit Sub
End Sub

heedaf
07-18-2017, 01:39 PM
Thank you! Mostly Access. Is it common in this forum to make people feel bad about their coding skills?

gmaxey
07-18-2017, 01:46 PM
No it is not common but it is also uncommon to see folks come into this forum tooting their horn about coding skills and then follow up with basic questions.

heedaf
07-18-2017, 01:58 PM
I don't recall ever saying I was the master coder and I was just trying to help someone with a concept. Had I known the expert was going to add a comment I wouldn't have bothered. I did state that anything can be done with VBA but I didn't imply that "I" could do anything in VBA. I greatly appreciate your help but being insulting isn't appreciated.