bpoff
asked on
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?
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?
have you considered using an OLE control to display the WORD document as it is ?
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. ;-)
aw... yuck. I know. Forget I ever mentioned it. ;-)
ASKER
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!
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!
i know this may seem really obvious, but is your multiline property set to true?
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. ;-)
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. ;-)
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!®©
Cheers!®©
ASKER
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?
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?
ASKER
Yes, Ctrl-C, Ctrl-V works great.
Try this:
Clipboard.Clear
wdTbl.Cell(iRow, iCol).Range.Copy
frmMain.rtBox(0).TextRTF = ""
frmMain.rtBox(0).Setfocus
SendKeys "^V"
Cheers!®©
Clipboard.Clear
wdTbl.Cell(iRow, iCol).Range.Copy
frmMain.rtBox(0).TextRTF = ""
frmMain.rtBox(0).Setfocus
SendKeys "^V"
Cheers!®©
ASKER
It just clears the textbox. The SendKeys doesn't actually put anything in the box. Dunno why, but SendKeys has always been rather erratic.
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
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)
dkrash777
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..."
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..."
bleedin' newbies... ;V)
ASKER
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.
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.Applica tion")
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
Dim objWord As Word.Application
Dim objWordDoc As Word.Document
Dim objWordRange As Word.Range
Dim nCharacters As Integer
Set objWord = CreateObject("Word.Applica
objWord.Documents.Open FileName:="c:\My Documents\Doc1.doc"
Set objWordDoc = objWord.ActiveDocument
nCharacters = objWordDoc.Tables(1).Cell(
Set objWordRange = objWordDoc.Tables(1).Cell(
objWordRange.SetRange Start:=0, End:=nCharacters - 1
objWordRange.Copy
RichTextBox1.TextRTF = Clipboard.GetText(vbCFRTF)
objWordDoc.Close
objWord.Quit
Set objWordDoc = Nothing
Set objWord = Nothing
ASKER
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...
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...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
I appreciate you (and the other folks) for keeping at it and not giving up on me!
Thanks again,
-- Brian
Cool! Happy to be of assistance.