Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Copy Text From RichTextBox

Posted on 2003-10-21
10
Medium Priority
?
250 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 60 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 60 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

722 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