Consulting

Results 1 to 7 of 7

Thread: WORD VBA - Please help me Bold specific text selected from ListBox

  1. #1
    VBAX Regular pk247's Avatar
    Joined
    Feb 2014
    Posts
    64
    Location

    WORD VBA - Please help me Bold specific text selected from ListBox

    Hi Everyone,

    I've tried a few things to try get this code to work without any manual intervention but I can't quite make it work. I found and adapted it from Greg Maxey's extremely helpful website. Would someone be kind enough to show me how to do this:

    1/ The selection made from Listbox1 returns as text into my Word doc (this part works great - thanks Greg Maxey!!)
    2/ Any text from the selection that contains text within between two ¦ signs would be bolded
    3/ the ¦ would be removed from the selection

    e.g. With selection range ¦This text here¦ would become ¦This text here¦ which would end up This text here

    Therefore the bold identifiers ¦...¦ would be removed from the document altogether

    I have thought about running code at the end of the "call UserForm" module but that would mean applying above requirements to the whole document and I really would prefer to steer away from doing that if possible.

    Hopefully this is quite an easy thing for someone to help me with please? Or if you can point me in the right direction that would be a great help!

    HTML Code:
    Private Sub CommandButton1_Click()
    Dim i As Long
    Dim STYLE As String
    Dim oRng As Word.Range
      STYLE = ""
        
      For i = 1 To ListBox1.ColumnCount
        Select Case True
          'Build the combo display
          Case i = ListBox1.ColumnCount - 1
            STYLE = STYLE & ListBox1.Column(i - 1) & " "
          Case i = ListBox1.ColumnCount
            
            STYLE = vbTab & ListBox1.Column(1) & vbCr & vbTab & _
                     ListBox1.Column(2) & vbCr & vbTab & _
                     ListBox1.Column(3) & vbCr & vbTab & _
                     ListBox1.Column(4) & vbCr & vbTab & _
                     ListBox1.Column(5) & vbCr & vbTab & _
                     ListBox1.Column(6) & vbCr & vbTab & _
                     ListBox1.Column(7) & vbCr & vbTab & _
                     ListBox1.Column(8) & vbCr & vbTab & _
                     ListBox1.Column(9) & vbCr & vbTab & _
                     ListBox1.Column(10) & vbCr & vbTab & _
                     ListBox1.Column(11) & vbCr & vbTab & _
                     ListBox1.Column(12) & vbCr & vbTab
            Case Else
            STYLE = STYLE & ListBox1.Column(i - 1) & vbCr '& vbTab
        End Select
      Next i
      Set oRng = Selection.Range
      
     
      oRng.Text = STYLE
        
    
     'I believe it is around here that my range would be coded to identify words within ¦ identifiers   
      
      Me.Hide
    lbl_Exit:
      Exit Sub
    End Sub
    Many thanks!

    Paul, Ireland


  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,359
    Location
    Set oRng = Selection.Range
    With oRng.Find
       .MatchWildcards = True
       .Text = "^0166*^0166"
        While .Execute
          oRng.Characters.First.Select 'Added for test
          oRng.Characters.First.Delete
          oRng.Characters.Last.Delete
          oRng.Font.Bold = True
          oRng.Collapse wdCollapseEnd
       Wend
    End With
    Last edited by gmaxey; 01-08-2016 at 04:29 AM.
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular pk247's Avatar
    Joined
    Feb 2014
    Posts
    64
    Location
    Thank you very much Greg!

    The bold is working but for the life of me I can't figure why the ¦ delete isn't working. This is how the outcome looks. Maybe my sourcedoc set up does not conform to what you had coded?

    P:
    ¦<!Custom Question!> [XXX-C-X]¦
    RB ENT ¦<entered value>¦ RB YN ¦[XXX-C-X]¦
    W:
    ¦<!Custom Question!> [XXX-C-X]¦
    RAD BT ¦[XXX-C-X]¦
    ¦<!Option1!> [XXX-C-X]¦
    ¦<!Option2!>¦
    ¦<!OptionX!>¦
    [TEXTBOX] ¦[XXX-C-X]¦

    The sourcedoc is a word table and the different lines of text are in a separate column each. Is there maybe a small tweak required?

    Thanks for all your help so far though!

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,359
    Location
    Step through the revised code and see what is being selected at the first character. That is what is being deleted.
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Regular pk247's Avatar
    Joined
    Feb 2014
    Posts
    64
    Location
    Thanks for getting back to me Greg. The step into is bunching the individual lines together, then splits them out, then bolds the ¦...¦ text, then ends.

    It's almost as if it needs to do the bold then remove ¦ at each juncture. Would you maybe know how to just delete all instances of ¦ within oRng?

    You're help is very much appreciated here

    Paul,

    Ireland
    Last edited by pk247; 01-08-2016 at 05:48 AM. Reason: Clarification

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,359
    Location
    Paul,

    If I put your example text in a document, select it and run the macro the result is:


    <!Custom Question!> [XXX-C-X]
    RB ENT <entered value>RBYN [XXX-C-X]
    W:
    <!Custom Question!> [XXX-C-X]
    RAD BT [XXX-C-X]
    <!Option1!> [XXX-C-X]
    <!Option2!>
    <!OptionX!>
    [TEXTBOX] [XXX-C-X]



    The process is pretty simply 1)Find your symbol, anything between it and the next instance the symbol
    2) Delete the first characters (the opening symbol) and the last characters (the closing symbol)
    3) Bold the rest

    Seems your issue is that you have not defined the Selection so that it includes all of the text you want processed.
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    VBAX Regular pk247's Avatar
    Joined
    Feb 2014
    Posts
    64
    Location
    Thanks for the guidance Greg. I'll hit the drawing board again and see what I come up with. If I get to the bottom of it I'll post back.

    Take care,

    Paul

Tags for this Thread

Posting Permissions

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