How to make multiple selections bold in RichTextBox

Posted by: omanaye on 4 August 2017, 2:43 pm EST

  • Posted 4 August 2017, 2:43 pm EST

    I have an Active Report 2 version 2.00.1240.

    I am trying to make multiple selections in a given RichTextBox bold.
    It works greate for the first two, it does not work for third one (error - Automation error, Unspecified error, err.Number -2147467259) and it works for rest. When I replaced line 3 ("Energy") with line 1 ("Leadership"), it works fine, but When I run couple times, this error accured again on line 1 ("Energy").

    1. MakeTextBold "grpPersonality", "lblLeadership", "Leadership"
    2. MakeTextBold "grpPersonality", "lblPersuasive", "Persuasive"
    3. MakeTextBold "grpPersonality", "lblEnergy", "Energy"
    4. MakeTextBold "grpPersonality", "lblAchievement", "Achievement"
    5. MakeTextBold "grpPersonality", "lblSelfConfidence", "Self - Confidence"
    6. MakeTextBold "grpPersonality", "lblAnalytical", "Analytical"
    7. MakeTextBold "grpPersonality", "lblAdaptable", "Adaptable"


    Public Sub MakeTextBold(GroupName As String, RT As String, s As String)
    Dim sec As Section
    Dim i As Integer
    Dim Where

    Set sec = oRPT.Sections(GroupName)
    For i = 0 To sec.Controls.Count - 1
        If sec.Controls(i).Name = RT Then
            Where = InStr(sec.Controls(i).Text, s) ' Find string in text.
            If Where Then   ' If found,
                sec.Controls(i).SelStart = Where - 1 ' set selection start and
                sec.Controls(i).SelLength = Len(s) ' set selection length.
                sec.Controls(i).SelBold = True
            End If
        End If
    Next

    Set sec  = Nothing
    End Sub

    Is there any way to make this work?
    Thanks for any help.
    Oksana

  • Replied 4 August 2017, 2:43 pm EST

    I do not know what the line "Dim Where" does, but what line of your MakeTextBold method throws this error?
  • Replied 4 August 2017, 2:43 pm EST

    If you do not specify the data type on a Dim statement you would get a variant by default. The problem with "Dim Where" is that "Where" should be a reserved keyword. It is generally best to be explicit and give your variables unique names, so something like this would be preferred.

    Dim varWhere as Variant
  • Replied 4 August 2017, 2:43 pm EST

    Dim Where as Variant
    sec.Controls(i).SelBold = True - this method simetimes throws an error, simetimes not

  • Replied 4 August 2017, 2:43 pm EST

    It appears as though your loop is calling the SelBold method on every control in your section.  If it reaches a control that is not a RichText control, it will fail.  Check the type of the control before accessing this property.
  • Replied 4 August 2017, 2:43 pm EST

    I am checking the name of control RT. RT - RichTextBoxes
    If sec.Controls(i).Name = RT Then

    It always works fine for these two methods :
    sec.Controls(i). SelStart = Where - 1 ' set selection start and
    sec.Controls(i). SelLength = Len(s) ' set selection length.

    But it does not always work for this method:
    sec.Controls(i). SelBold = True
  • Replied 4 August 2017, 2:43 pm EST

    Where are you calling this subroutine from? Ideally you would call this from the Format event of the section which contains the control you wish to modify.

    I would also suggest that you do not use keywords as variable names, i.e. 'Where'. In addition it appears as though you are doing unnecessary looping, since you know the section and the specific control, call it directly.

    Public Sub MakeTextBold(pstrGroupName As String, pstrRT As String, pstrS As String)
       Dim lngWhere as Long

       With oRPT.Sections(pstrGroupName).Controls(pstrRT)
          lngWhere = Instr(.Text, pstrS)
          If lngWhere > 0 Then
             .SelStart = lngWhere -1
             .SelLength = Len(pstrS)
             .SelBold = True
          End If
       End With
    End Sub

  • Replied 4 August 2017, 2:43 pm EST

    Thanks W.
    Nothing is changed. 
    These two methods work fine:
       .SelStart = lngWhere -1
       .SelLength = Len(pstrS)
    Method SelBold is not working right. This method does not always generate an error.
       .SelBold = True

    Oksana
  • Replied 4 August 2017, 2:43 pm EST

    Where are you calling this subroutine from?

    Do you still have this issue if you upgrade to the latest released version?
    http://www.datadynamics.com/ShowForum.aspx?ForumID=37
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels