Solved

How to Copy text from Word to RichTextBox

Posted on 2000-03-21
19
209 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…

705 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

18 Experts available now in Live!

Get 1:1 Help Now