?
Solved

How to Copy text from Word to RichTextBox

Posted on 2000-03-21
19
Medium Priority
?
219 Views
Last Modified: 2012-05-04
I need to get a bunch of formatted text from a table in Word into a RichTextBox control.  This is harder than it seems.

This does not work properly:

wdTbl.Cell(iRow, iCol).Range.Copy
frmMain.rtBox(0).TextRTF = Clipboard.GetText(vbCFRTF)


It works fine except that the Carriage Returns do NOT get copied.  Well, I really need all of the text in its original format.

Is there another way to get this to work?


0
Comment
Question by:bpoff
[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
  • 7
  • 4
  • 3
  • +4
19 Comments
 
LVL 5

Expert Comment

by:AnswerTheMan
ID: 2640270
have you considered using an OLE control to display the WORD document as it is  ?
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2640306
Well, I'm nearly ashamed to mention this idea, but I honestly haven't got a clue on how to do this properly. But... if you don't get any decent solutions perhaps you could make your Word application Save As an RTF file and then get the information out by reading the RTF file.

aw... yuck. I know. Forget I ever mentioned it. ;-)
0
 
LVL 1

Author Comment

by:bpoff
ID: 2640326
AnswerTheMan,

I don't just want to display the text in the VB app.  I need to save the RTF data in a database for rapid retrieval at a later time.  Basically, someone who wants to use Word supplies me with a document in a special format, which I import into our system.


caraf_q,

Yeah, I originally tried that but was just terribly clunky and really slow.



This current way is great except for the lost carriage returns.  If I can just keep them, I'll be happy!
0
Industry Leaders: 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!

 
LVL 2

Expert Comment

by:tylerd
ID: 2640492
i know this may seem really obvious, but is your multiline property set to true?
0
 
LVL 1

Expert Comment

by:pclement
ID: 2640529
It seems that the clipboard data after the copy is vbCFText. You can use the GetData method of the Clipboard object to verify this.

You can either specify this value or omit the argument altogether and it will determine the correct type.

Kind of sneaky the way it lets you shoot yourself in the foot. ;-)

0
 
LVL 14

Expert Comment

by:mcrider
ID: 2640587
Just out of curiosity, if you manually copy the table to the clipboard by doing "Ctrl-C" and then paste the table into the RichTextBox with "Ctrl-V", does it work??


Cheers!®©
0
 
LVL 1

Author Comment

by:bpoff
ID: 2640636
pclement,

You almost had it.  Now I get the carriage returns, but the text is all the same color.  This sucks.  Why does this have to be so difficult?
0
 
LVL 1

Author Comment

by:bpoff
ID: 2640660
Yes, Ctrl-C, Ctrl-V works great.
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2640693
Try this:

   Clipboard.Clear
   wdTbl.Cell(iRow, iCol).Range.Copy
   frmMain.rtBox(0).TextRTF = "" 
   frmMain.rtBox(0).Setfocus
   SendKeys "^V"


Cheers!®©

0
 
LVL 1

Author Comment

by:bpoff
ID: 2640795
It just clears the textbox.  The SendKeys doesn't actually put anything in the box.  Dunno why, but SendKeys has always been rather erratic.
0
 

Expert Comment

by:dkrash777
ID: 2640960
bpoff,

Your original code:

wdTbl.Cell(iRow, iCol).Range.Copy
frmMain.rtBox(0).TextRTF = Clipboard.GetText(vbCFRTF)

have You tried concatenating a carriage return to the original code?

frmMain.rtBox(0).TextRTF = Clipboard.GetText(vbCFRTF) + chr$(13)

dkrash777


0
 
LVL 14

Expert Comment

by:mcrider
ID: 2641003
dkrash777,

Tips on when to comment and when to answer... If you use one of the following phrases in your response it should be a comment:

 "How about..."
 "Have you tried..."
 "Maybe..."
 "What about..."
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2641013
bleedin' newbies... ;V)
0
 
LVL 1

Author Comment

by:bpoff
ID: 2641036
The problem is not a carriage return at the END, but the 0 or more carriage returns that may appear at various points throughout the text in question.
0
 
LVL 1

Expert Comment

by:pclement
ID: 2641329
The problem is on the Word side. When the Range object is copied it's copying the Cell object and not the text within the cell. This appears to hose some of the formatting when pasted to the RTF control. The fun part was trying to get the Range to include only the text and not the Cell. You will need to modify the code for your solution, but see if the following works for you:

Dim objWord As Word.Application
Dim objWordDoc As Word.Document
Dim objWordRange As Word.Range
Dim nCharacters As Integer

Set objWord = CreateObject("Word.Application")

objWord.Documents.Open FileName:="c:\My Documents\Doc1.doc"

Set objWordDoc = objWord.ActiveDocument

nCharacters = objWordDoc.Tables(1).Cell(1, 1).Range.Characters.Count
Set objWordRange = objWordDoc.Tables(1).Cell(1, 1).Range
objWordRange.SetRange Start:=0, End:=nCharacters - 1
objWordRange.Copy

RichTextBox1.TextRTF = Clipboard.GetText(vbCFRTF)

objWordDoc.Close
objWord.Quit

Set objWordDoc = Nothing
Set objWord = Nothing

0
 
LVL 1

Author Comment

by:bpoff
ID: 2641425
Nope, this still has the missing carriage returns.  Here is my exact code:

Set wdRng = wdTbl.Cell(j, 3).Range
wdRng.SetRange Start:=wdRng.Start, End:=wdRng.End
wdRng.Copy
frmMain.rtBox(0).TextRTF = Clipboard.GetText(vbCFRTF)


Note that the table has 3 columns and I want the text from the 3rd column.  Your code starts counting from the first character of the DOCUMENT, not the CELL so I had to make a few changes...
0
 
LVL 1

Accepted Solution

by:
pclement earned 400 total points
ID: 2641586
Whoops! My bad. My SetRange method syntax is incorrect. Try this line instead:

objWordRange.SetRange Start:=objWordRange.Start, End:=objWordRange.Start + nCharacters - 1

0
 
LVL 1

Author Comment

by:bpoff
ID: 2645023
Yep, that did it.  Thanks!!!

I appreciate you (and the other folks) for keeping at it and not giving up on me!

Thanks again,

-- Brian
0
 
LVL 1

Expert Comment

by:pclement
ID: 2645440
Cool! Happy to be of assistance.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…
Suggested Courses

765 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