PDA

View Full Version : [SOLVED:] Find instances of certain word within a textbox and make bold



HTSCF Fareha
02-17-2021, 09:04 AM
I'm hoping that someone might be able to help. The submacro needs to accept a word from the user, then search TextBox3 for instances of that word, then make them bold.

(I'm trying to modify code that I have used in Excel)


Private Sub btnSearchText_Click()

' Provide option to search for specific words and make them bold

Dim sFind As String
Dim rTextBox As Range
Dim rng As Range
Dim lCount As Long
Dim iLen As Integer
Dim iFind As Integer
Dim iStart As Integer

On Error Resume Next
Set rng = ActiveDocument.UsedRange. _
TextBox3
On Error GoTo ErrHandler
If rng Is Nothing Then

MsgBox "There is no text", vbExclamation + vbOKOnly, "Triage Hub"

GoTo ExitHandler
End If

sFind = Application.InputBox("What do you want to make BOLD?", "Triage Hub", "", Type:=2)

If sFind = "" Then

MsgBox "No text was requested", vbExclamation + vbOKOnly, "Triage Hub"

GoTo ExitHandler
End If

iLen = Len(sFind)
lCount = 0

For Each rTextBox In rng
With rTextBox
iFind = InStr(.Value, sFind)
Do While iFind > 0
.Characters(iFind, iLen).Font.Bold = True
lCount = lCount + 1
iStart = iFind + iLen
iFind = InStr(iStart, .Value, sFind)
Loop
End With
Next

If lCount = 0 Then

MsgBox "There were no occurrences of" & _
vbCrLf & "' " & sFind & " '" & _
vbCrLf & "to make bold.", vbExclamation + vbOKOnly, "Triage Hub"

ElseIf lCount = 1 Then

MsgBox "One occurrence of" & _
vbCrLf & "' " & sFind & " '" & _
vbCrLf & "was made bold.", vbInformation + vbOKOnly, "Triage Hub"

Else

MsgBox lCount & " occurrences of" & _
vbCrLf & "' " & sFind & " '" & _
vbCrLf & "were made bold.", vbInformation + vbOKOnly, "Triage Hub"

End If

ExitHandler:
Set rCell = Nothing
Set rng = Nothing
Exit Sub

ErrHandler:
MsgBox Err.Description
Resume ExitHandler

End Sub

gmayor
02-18-2021, 12:28 AM
As you have probably gathered Word and Excel VBA have differences. :) The following will do what you require with 'Text Box 3' The second macro is to confirm the names of the text boxes.

Private Sub btnSearchText_Click()
'Graham Mayor - https://www.gmayor.com - Last updated - 18 Feb 2021
' Provide option to search for specific words in a text box and make them bold
Dim sFind As String
Dim oShape As Shape
Dim bShape As Boolean
Dim oRng As Range
Dim lCount As Long
On Error Resume Next
For Each oShape In ActiveDocument.Shapes
If oShape.Type = 17 Then
If oShape.Name = "Text Box 3" Then
bShape = True
Exit For
End If
End If
Next oShape
If bShape = False Then
MsgBox "There is no Text Box 3", vbExclamation + vbOKOnly, "Triage Hub"
GoTo ExitHandler
End If
Set oRng = oShape.TextFrame.TextRange
If oShape.TextFrame.HasText = False Then
MsgBox "There is no text in Text Box 3", vbExclamation + vbOKOnly, "Triage Hub"
GoTo ExitHandler
MsgBox oRng.Text, vbExclamation + vbOKOnly, "Triage Hub"
GoTo ExitHandler
End If
sFind = InputBox("What do you want to make BOLD?", "Triage Hub")
If sFind = "" Then
MsgBox "No text was requested", vbExclamation + vbOKOnly, "Triage Hub"
GoTo ExitHandler
End If
lCount = 0
With oRng.Find
Do While .Execute(findText:=sFind)
oRng.Font.Bold = True
lCount = lCount + 1
oRng.Collapse 0
Loop
End With
If lCount = 0 Then
MsgBox "There were no occurrences of" & _
vbCrLf & "' " & sFind & " '" & _
vbCrLf & "to make bold.", vbExclamation + vbOKOnly, "Triage Hub"
ElseIf lCount = 1 Then
MsgBox "One occurrence of" & _
vbCrLf & "' " & sFind & " '" & _
vbCrLf & "was made bold.", vbInformation + vbOKOnly, "Triage Hub"
Else
MsgBox lCount & " occurrences of" & _
vbCrLf & "' " & sFind & " '" & _
vbCrLf & "were made bold.", vbInformation + vbOKOnly, "Triage Hub"
End If
ExitHandler:
Set oRng = Nothing
Set oShape = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub


Sub test()
Dim oShape As Shape
For Each oShape In ActiveDocument.Shapes
oShape.Select
MsgBox oShape.Type & vbCr & oShape.Name
Next oShape
End Sub

HTSCF Fareha
02-18-2021, 05:24 AM
Thanks for looking at this one, Graham!

I have indeed noticed that there are lots of differences between VBA for Word and Excel. From a layperson's point of view I can understand that certain things will be specific to one or the other i.e. cells = Excel (or tables in Word), paragraphs = Word, vLookup = Excel, but the way that one formulates the code would appear on the face of it to be different?


My bad, I didn't explain properly that the TextBox3 was actually on the UserForm. :blush

At the moment I'm getting the "There is no Text Box 3" message, when clicking the button on the UserForm.

Thanks.
Steve

gmayor
02-18-2021, 08:04 AM
Userform textboxes cannot adopt text formatting - they are as their name suggests text boxes. You can output the text entered into a userform text box to the document and format that, but if you have such a userform in Word, then why not add another text box instead of using the input box, and why not add a label to the userform to display the count. e.g. based on your earlier code something like the following. This writes the value of TextBox3 to the cursor position in the document, then formats the word from TextBox4 in the inserted text as bold. While this is included as a possibility it's not an approach I would advocate without knowing more about what you are trying to do. In any case I wouldn't have a button on the form to do this. I would include the formatting in the output from the userform as a whole.



Private Sub btnSearchText_Click()
' Provide option to search for specific words in a text box and make them bold
Dim oRng As Range, oFind As Range
Dim lCount As Long
If TextBox3.Text = "" Then
MsgBox "There is no text in TextBox 3", vbExclamation + vbOKOnly, "Triage Hub"
TextBox3.SetFocus
GoTo ExitHandler
End If
If TextBox4.Text = "" Then
MsgBox "There is no text in TextBox 4", vbExclamation + vbOKOnly, "Triage Hub"
TextBox4.SetFocus
GoTo ExitHandler
End If


Set oRng = Selection.Range
oRng.Text = TextBox3.Text
oRng.Select
Set oFind = Selection.Range


lCount = 0
With oFind.Find
Do While .Execute(findText:=TextBox4.Text)
If Not oFind.InRange(oRng) Then Exit Do
oFind.Font.Bold = True
lCount = lCount + 1
oRng.Collapse 0
Loop
End With
If lCount = 0 Then
Label1.Caption = "There were no occurrences of" & _
vbCrLf & "' " & TextBox4.Text & " '" & _
vbCrLf & "to make bold."
ElseIf lCount = 1 Then
Label1.Caption = "One occurrence of" & _
vbCrLf & "' " & TextBox4.Text & " '" & _
vbCrLf & "was made bold."
Else
Label1.Caption = lCount & " occurrences of" & _
vbCrLf & "' " & TextBox4.Text & " '" & _
vbCrLf & "were made bold."
End If
ExitHandler:
Set oRng = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub

HTSCF Fareha
02-18-2021, 11:05 AM
You are absolutely right, Graham. Thinking about it, the counting instances thing was a hangover from the Excel code and is not relevant for this Word template.

Searching for multiple words to make bold might be required (possibly using a comma to separate each word?). Although there will also be instances when no words are required to be made bold at all.

I've added a separate Textbox for entering the words and removed the button and placed the code inside the 'Enter' button code, so that this can run in one operation when the UserForm has been filled out as required by the inputter.

I've uploaded my document too, which I should've done in the first instance.

HTSCF Fareha
03-06-2021, 03:06 PM
Still been trying to get this to work. Tried to implement a Split function to utilise a comma as a separator for multiple word searching in TextBox9.

Getting a compile error: argument not optional.

HTSCF Fareha
03-13-2021, 01:24 PM
Stripped out most of the extra code so that I could focus on the Split function.

Getting an 'argument not optional' here :-


With oFind.Find
Do While .Execute(findText:=TextBox9.Text)
If Not oFind.InRange = Split(oRng, ",") Then Exit Do
oFind.Font.Bold = True

oRng.Collapse 0
Loop
End With

Need to keep in mind that although requiring the option to find one or more words, there will be occassions when there will be none to find.

gmayor
03-13-2021, 10:47 PM
The code is a bit of a mishmash of techniques. Frankly I would replace all the bookmarks with content controls - easy using https://www.gmayor.com/insert_content_control_addin.htm (http://www.gmayor.com/insert_content_control_addin.htm)
I have done that in your document. It is then a simple matter to fill the controls from your assorted userform controls.
If you want to search for the items in your list then you need to loop through the list (see attached).
I would recommend that you save the document as a template and create new documents from it.

HTSCF Fareha
03-15-2021, 09:23 AM
Graham, firstly very many thanks for the help and guidance.

I'm still learning and always keen to adopt and try new techniques. I certainly appreciate the professional guidance from all MS MVP programmers!

I agree that my original form was a mishmash and was intending it to end up using Content Controls. My first foray into VBA was using Bookmarks, although #gmaxey soon put me right on that score and nudged me into the right direction with Content Control. As you mention on your website about Content Control, MS don't make it exactly easy to implement, despite its clear advantages over bookmarks.

I'll certainly be checking out your content control plugin. I'm assuming that you place your cursor at the intended location on the document and then fill in the relevant boxes?

Thank you once again!

Steve