Solved

How to Copy text from Word to RichTextBox

Posted on 2000-03-21
19
210 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

919 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now