How to Copy text from Word to RichTextBox

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?


LVL 1
bpoffAsked:
Who is Participating?
 
pclementConnect With a Mentor Commented:
Whoops! My bad. My SetRange method syntax is incorrect. Try this line instead:

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

0
 
AnswerTheManCommented:
have you considered using an OLE control to display the WORD document as it is  ?
0
 
caraf_gCommented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
bpoffAuthor Commented:
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
 
tylerdCommented:
i know this may seem really obvious, but is your multiline property set to true?
0
 
pclementCommented:
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
 
mcriderCommented:
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
 
bpoffAuthor Commented:
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
 
bpoffAuthor Commented:
Yes, Ctrl-C, Ctrl-V works great.
0
 
mcriderCommented:
Try this:

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


Cheers!®©

0
 
bpoffAuthor Commented:
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
 
dkrash777Commented:
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
 
mcriderCommented:
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
 
caraf_gCommented:
bleedin' newbies... ;V)
0
 
bpoffAuthor Commented:
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
 
pclementCommented:
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
 
bpoffAuthor Commented:
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
 
bpoffAuthor Commented:
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
 
pclementCommented:
Cool! Happy to be of assistance.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.