0

I'm trying to make a more functional search application to use on Excel. The idea is to be able to perform a search of a user-specified set of key words in cells that contain target text that can have up to 600 words. The application would locate all cells with at least one match and displays the entire content of the cell in a textbox (in this case textbox7 in UserForm1) and would highlight the matching words in orange colored text for quick identification of the context of the key words within the target text. For some reason I get an error message reading "Run-time error '438': Object doesn't support this property or method" and it highlights the line in my code reading: "With sh.TextBox". Below I've included a copy of the code with the section where this error is occurring. From what I can see it looks like the error is the result of the way I've dimensioned variable "sh" (i.e as msforms.TextBox) Ultimately what I need to achieve is something that will allow the use of "Characters" object which is a crucial part of this application because it locates the key words that have been found in the target text so that the font color can be changed to orange (and bold text). I would be very grateful if someone can point out what the issue is and how I can go about resolving it. Thanks in advance.

    Private Sub CommandButton2_Click()
    'code below displays all email items found with at least one word match
    Dim Position As Long
    Dim EmlCharacterLength As Long
    Dim KeyWordCharacterLength As Long
    Dim sh As msforms.TextBox
    Set sh = UserForm1.TextBox7
    SlotCount2 = 0
    If RA1Counter < NmbrItemsFound Then
        Do While ResultsArray1(1, RA1Counter) = ResultsArray1(1, RA1Counter + 1)
            RA1Counter = RA1Counter + 1
        Loop
        EmailCntr = EmailCntr + 1
        TextBox2.Value = EmailCntr & " of " & NmbrEmails
        TextBox3.Value = CorroArray(5, ResultsArray1(1, RA1Counter))
        TextBox4.Value = CorroArray(2, ResultsArray1(1, RA1Counter))
        TextBox5.Value = CorroArray(3, ResultsArray1(1, RA1Counter))
        TextBox6.Value = CorroArray(0, ResultsArray1(1, RA1Counter))
        TextBox7.Value = CorroArray(4, ResultsArray1(1, RA1Counter))
        EmlCharacterLength = Len(CorroArray(4, ResultsArray1(1, RA1Counter)))
        Do While SlotCount2 < NbrKeyWords + 1
            Position = 1
            KeyWordCharacterLength = Len(SplitKeyWords(SlotCount2))
            Do While Position < EmlCharacterLength
                Position = InStr(Position, CorroArray(4, ResultsArray1(1, RA1Counter)), SplitKeyWords(SlotCount2), 1)
                With sh.TextBox
                    .Characters(Start:=Position, Length:=KeyWordCharacterLength).Font.Bold = True
                    .Characters(Start:=Position, Length:=KeyWordCharacterLength).Font.Color = RGB(255, 102, 0)
                End With
                Position = Position + KeyWordCharacterLength
            Loop
            SlotCount2 = SlotCount2 + 1
        Loop
        RA1Counter = RA1Counter + 1
    Else
        RA1Counter = 1
        EmailCntr = 0
    End If
        
        
End Sub
Davesexcel
  • 6,896
  • 2
  • 27
  • 42
Tony Catton
  • 211
  • 1
  • 2
  • 4
  • 1
    You already set the `sh` variable `Set sh = UserForm1.TextBox7` therefore `sh.TextBox` does not help you. – Davesexcel Aug 27 '22 at 09:07
  • What happens if you `Dim sh as Object`? Then look in a Watch window in the debugger to see what type object it is. As @Davesexcel says you, `sh` is probably already a TextBox object. However it is not clear from the documentation that it will support different formatting of sections of the string. – DS_London Aug 27 '22 at 09:15
  • 2
    The problem is that the TextBox (`MSForms.TextBox`) object does not have a Characters Property or Method - it is not possible to format the font of individual segments of the text contained in a TextBox. At a guess, you are trying to treat the TextBox like a Shape in which you can do this using its TextFrame etc ... but a TextBox is not a Shape ... AFAIK there is no way to do what you want in a VBA UserForm (without possibly resorting to a 3rd party library ... but that's a whole other question) – JohnM Aug 27 '22 at 10:22
  • Looks like you're spot on @JohnM, there doesn't appear to be a way of changing attributes of specific characters or words in a string contained in a userform text box. I'll look into using a shape for this instead. Thanks for your input. – Tony Catton Aug 27 '22 at 11:05
  • Another possible alternative is to use a RichTextBox Control, but it comes in a discrete library (which may only work on 32-bit Office) ... I've never used it so can't make any commitments about whether it is available on your device (or to download), will work or allows you to use per-character/word formatting, but include the link here just on the off-chance you want to look into it further: https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-basic-6/aa261653(v=vs.60) – JohnM Aug 27 '22 at 11:32
  • Perhaps you were confusing the userform textbox with the worksheet textbox. They're easy to confuse... woulda been nice if they were designed with more obviously-different names. A textbox added to a worksheet can be [handled like a shape](https://stackoverflow.com/a/36074863/8112776) and individual characters can be styled via it's [TextFrame](https://learn.microsoft.com/office/vba/api/excel.textframe), but this is not the case with [userform textboxes](https://sitestory.dk/excel_vba/textboxes-userforms.htm). – ashleedawg Aug 27 '22 at 11:58
  • Perhaps somewhat relevant, I asked/answered a detailed post a while back when I was having trouble differentiating the two types of controls (and programmatic manipulation of them): [**Differences between Excel's Form Controls & ActiveX Controls**](https://stackoverflow.com/a/50144021/8112776).... you may or may not find the overview helpful. ‍♂️ – ashleedawg Aug 27 '22 at 12:05

0 Answers0