Solved

Autoscrolling TextBox in Outlook VBA

Posted on 2004-10-25
1,239 Views
Last Modified: 2012-08-14
I seem to be having trouble getting my textbox to autoscroll to the bottom and keep up with text.  Basically my textbox is a status window that displays information about current activities happening in the background and I would like it to remain at the bottom at the most current line.  I also want a max limit on the number of lines displayed in the window.  I have searched and tried many solutions previously posted.  Currently I have:

Const MAXCHARS = 10000

Sub Post (strNewText as String)
   With txtInfo
      If Len(strNewTxt) + Len(.Text) > MAXCHARS Then
         .Text = Mid$(.Text, InStr(100 + Len(strNewText), .Text, vbCrLf) + 2)
      End If
      .SelStart = Len(.Text)
      .SelText = strNewText
   End With
End Sub

That posts the information I need to the window properly and maintains the limit, but the scrollbar does not update and remain at the bottom of the window.  If I click in the textbox it updates and jumps to the bottom.  I tried setting focus to the textbox at the end of the post sub and also attempted a form repaint but neither gets the scrollbar to update properly.

Another solution I saw recommended SendMessage to send a scroll message to the scrollbar but when I try to use Form1.hWnd in Outlook vba I get an undeclared member.  Is there something I am missing or someway to get this darn textbox to autoscroll?

0
Question by:cdthurman
    12 Comments
     

    Author Comment

    by:cdthurman
    Updated the points a little since it seems to be more difficult than I originally thought.
    0
     
    LVL 5

    Expert Comment

    by:gary_j
    if clicking in the text box does the trick, try calling the txtInfo_click event from the "post" sub routine.
    0
     
    LVL 32

    Accepted Solution

    by:
    You may consider using a ListBox instead...

    With ListBox1

    If .ListCount > 100 Then 'max 100 lines
    .RemoveItem (1)
    End If

    .AddItem strNewText
    .ListIndex = .ListCount - 1
    .Selected(.ListCount - 1) = False

    End With
    0
     
    LVL 16

    Expert Comment

    by:jimbobmcgee
    In a VBA.TextBox there are properties called CurLine and LineCount (they're not in the VB6 textbox -- I don't know why).  I would expect

            TextBox1.CurLine = TextBox1.LineCount

    to do the job for you...

    HTH

    J.
    0
     
    LVL 16

    Expert Comment

    by:jimbobmcgee
    But I think Erick37's ListBox idea is probably better for you...

    ;)

    J.
    0
     

    Author Comment

    by:cdthurman
    CurLine does not work either.  ListBox works fine but you cannot select/copy text from a list box.  Still trying to decide if that is needed or not.  Still hoping there is a way to get it to work with a TextBox.

    0
     
    LVL 32

    Expert Comment

    by:Erick37
    A RichTextBox control will work, but you have to be licensed to use it (VB6 installed).
    0
     

    Author Comment

    by:cdthurman
    That's really not an option either.  Needs to be installed on quite a few machines and I don't have the rights to make registry changes to register DLLs for RichTextBox.
    0
     
    LVL 16

    Expert Comment

    by:jimbobmcgee
    You can select/copy text from the listbox, you just need to fiddle about a bit:

    Alongside your listbox (lstLog), place a 'Copy' commandbutton (cmdCopyLog).  Then, in the code for that button:

           Sub cmdCopyLog_Click()

                 Clipboard.Clear
                 Clipboard.SetText lstLog.Selected

    HTH

    J.
    0
     
    LVL 16

    Expert Comment

    by:jimbobmcgee
    Oh, and don't forget to put End Sub at the end of it...

    J.
    0
     
    LVL 8

    Expert Comment

    by:Ashutosh Vyas
    TextBox1.SelStart = Len(TextBox.Text)

    Would that do???

    0
     

    Author Comment

    by:cdthurman
    I asked the users and they said copy paste wasn't really an issue so going with the listbox solution.  I wish there was a way to do is using a plain ol' textbox but VBA doesn't want to play nice in Outlook.

    Awarding points to Erick37

    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    933 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now