Solved

Copy Text From RichTextBox

Posted on 2003-10-21
10
248 Views
Last Modified: 2010-05-03
I am trying to make a commandbutton (Called Copy) when clicked copy text from a RichTextBox (Called RTB)
But...
A) If no text is selected it selects and copies all the text in the RTB
B) If some text is selected it coped only the selected text.
0
Comment
Question by:neutrinohunter
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 2

Assisted Solution

by:Djinn_ro
Djinn_ro earned 20 total points
ID: 9593893
??????
Use RTB.SelLength
If RTB.SelLength=0 then
   'no text is select
   YourString=RTB.Text
Else
   'nr of char of selected text
   YourString=RTB.SelText
End if
0
 

Author Comment

by:neutrinohunter
ID: 9594063
Sorry I must have been very unclear in my post.

What I meant was I am trying to copy the text from a RichTextBox
But the code I am using seems to work only for textboxes. Thus it aint copying any text.

So if someone could post a code to Copy the text aswell as select the text as Djinn_ro posted that would be gr8

Thx
0
 
LVL 4

Expert Comment

by:eric0213
ID: 9594889
If RTB.SelLength=0 then
   'no text is select
   YourString=RTB.Text
Else
   'nr of char of selected text
   YourString=RTB.SelText
End if

Clipboard.Clear
Clipboard.SetText = YourString

' If you accept this, give the points to Djinn_ro
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:neutrinohunter
ID: 9597581
k, but what does YourString mean?

I pasted all that in my code, but it says
Variable Not defined?
0
 
LVL 4

Expert Comment

by:eric0213
ID: 9598565
define the variable...

Dim YourString As String
0
 

Author Comment

by:neutrinohunter
ID: 9598961
I tried that.

Its says
Compile Error;
Argument not optional


? Help Please.
0
 
LVL 4

Expert Comment

by:eric0213
ID: 9598970
what argument?  what line?
0
 

Author Comment

by:neutrinohunter
ID: 9599460
This line
Clipboard.SetText = YourString
0
 
LVL 4

Accepted Solution

by:
eric0213 earned 20 total points
ID: 9599590
oops :(  sorry about that...

' can't use vbCFRTF constant because of Microsoft bug
' vbCFRTF is stored as a long, but SetText expects an Integer
' and an overflow is caused
Private Sub cmdCopyRTF_Click()
    Clipboard.Clear
   
    If RTB.SelLength = 0 Then
        Clipboard.SetText RTB.TextRTF, &HBF01
    Else
        Clipboard.SetText RTB.SelRTF, &HBF01
    End If
End Sub

Private Sub cmdCopyText_Click()
    Clipboard.Clear
   
    If RTB.SelLength = 0 Then
        Clipboard.SetText RTB.Text, vbCFText
    Else
        Clipboard.SetText RTB.SelText, vbCFText
    End If
End Sub
0
 
LVL 4

Expert Comment

by:eric0213
ID: 9599593
There's two choices... one that copies text and one that copies rtf text
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month11 days, left to enroll

632 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