Log in

View Full Version : Textbox refresh



glencoe
11-04-2011, 08:24 AM
I am running a macro in Word, doing in the background many modifications to the opened document. At the same time, the macro is updating a textbox displayed in the foreground window, to show all modifications done. This textbox acts a bit like a "progress window" when installing a new application (you may sometimes display the list of files that are being installed).

To send new information to the textbox, I am using the method
Textbox.value = Textbox.value & newtext

with newtext being a public string of characters to add.

I also add the line
Progression.TextBox1.SelStart = Len(Progression.TextBox1.Text)
to force the textbox to scroll to the end and hopefully refresh its display.

The problem that I have is that the textbox doesn't always refresh properly, or should I say, I know it is refreshing, but it actually "hangs" as if it was not responding and new text doesn't show up. It is only when the macro is done that all text displays properly and that I can check the textbox has been updated as it should have.

I have the feeling that this happens when the textbox receives lots of updates in a short period of time.
I tried to use the Repaint function, with no luck.
Any suggestion?

Tommy
11-04-2011, 10:40 AM
I suggest that you do after the


Textbox.value = Textbox.value & newtext
DoEvents


This will allow the operating system to finish what it is doing before you do something else.

glencoe
11-04-2011, 10:48 AM
Tommy, I think you saved my code! You rock, it seems to be doing the trick! :-)
I just need to add some code to scroll the textbox to the end everytime text is added. It's not scrolling...

I thought that the following line would do it, but it doesn't:
Progression.TextBox1.SelStart = Len(Progression.TextBox1.Text)

glencoe
11-04-2011, 11:00 AM
I updated the line to
Progression.TextBox1.SelStart = Len(Progression.TextBox1.Value)
and everything is working perfectly now...

Thanks all for your help! :-)

Tommy
11-04-2011, 11:03 AM
I tested with the below code and it "scrolled" just fine, or the text I just added appeared at the end of the text string and I could read it.

So the only guesses I can do is guess that the Progression is either misspelled or the textbox is the incorrect one.
Private Sub UserForm_Click()
cnt = cnt + 1
TextBox1.Text = TextBox1.Text & " more text " & CStr(cnt)
TextBox1.SelStart = Len(TextBox1.Text)
End Sub

glencoe
11-04-2011, 11:11 AM
No, my guess is that I add new text with the .value and not .text feature. I can't remember the exact reason why I didn't use .text, but I think it was not working... I may have not done it right! ;-)
But whatever the reason is, I also guess you need to match the "add text" and "scroll to the end" codes using either .text or .value, but you can't mix both, otherwise it won't work!
Now, you are probably more expert than me, so you can probably confirm whether my guess is correct or not!