Consulting

Results 1 to 14 of 14

Thread: Selecting a Word in a Paragraph

  1. #1
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location

    Selecting a Word in a Paragraph

    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?

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    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.

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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.
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    How does saying that it doesn't work mean that I don't know how to code?

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    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

  8. #8
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  9. #9
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  10. #10
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    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?

  11. #11
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  12. #12
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    Thank you! Mostly Access. Is it common in this forum to make people feel bad about their coding skills?

  13. #13
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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.
    Greg

    Visit my website: http://gregmaxey.com

  14. #14
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    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.
    Last edited by heedaf; 07-18-2017 at 02:08 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •