PDA

View Full Version : Copy from RichTextBox into a Word Document



bdang007
03-26-2009, 10:43 PM
Hey Guys,
Anyone know how to copy from the RichTextBox into a Word document and keep it's formatting?
FYI - You can not straight copy the .Text or .TextRF into the Word document.

I've tried to copy it into the clipboard but I wasn't able to and in theory I don't think it would work since the only way that I know how to get the RichTextBox into the clipboard is using ObjectData - which only supports strings!

Any ideas?

bdang007
03-27-2009, 12:00 AM
Ok looks like I've solved it hehehe post here if you want solution - its so simple!
hint - copy and paste.

Wekkel
03-28-2009, 05:11 AM
Why not share the code you found working with others?

bdang007
03-28-2009, 08:58 PM
well I said post here if anyone wanted the solution. Turns out the code didn't work after all! What I originally did was select everything in richtextbox using rtb.selstart = 0 and rtb.sellength = len(rtb.text) and then using selection.copy and paste it into the document. But I should have known that selection only works for word documents. I couldn't figure out how to go directly from rtb to word document.

The only way I could do it was to save the richtextbox into a rtf file, open it using word, select all and copy then paste it in the word document and then close and delete the file. A long way to do it but I couldn't figure out how to do it any order way. If anyone knows a better way, would be great to post it.

fumei
03-30-2009, 09:48 AM
It would really help if you actually posted code.

fumei
03-30-2009, 09:58 AM
Also, could you explain:

"But I should have known that selection only works for word documents. "

Are you not working with word documents? I do not understand the use of "only".

bdang007
03-30-2009, 05:05 PM
From my understanding the selection method only works when used in a word document to copy/paste etc etc.

What I was trying to do, as stated above, was to select everything in the richtextbox by using selstart and sellength methods for the RTB and then using the selection method to copy and paste it into the word document. However did this not work and I think it didn't work because the selection method can not be used in conjunction with richtextbox. Hope that makes sense.

Onto to code:

Private Sub cmdInsert_Click()
Dim BMRange As Range
Dim filename As String
Dim wrdDoc As Word.Document
Dim wrdApp As Word.Application
filename = "C:\temp3.rtf"
Set BMRange = ActiveDocument.Bookmarks("MedicalSummary").Range
'Save what is in the RTB, open the file, copy all, paste in BMRange, delete file.
RTextBox.SaveFile (filename)
Set wrdApp = New Word.Application
Set wrdDoc = Word.Application.Documents.Open(filename, , False)
Selection.WholeStory
Selection.Copy
BMRange.Select
Selection.PasteAndFormat (wdPasteDefault)
wrdDoc.Close
wrdApp.Quit
Set wrdDoc = Nothing
Set wrdApp = Nothing
Kill filename
ActiveDocument.Bookmarks.Add "MedicalSummary", BMRange
End Sub

fumei
04-01-2009, 09:11 AM
1. It always helps if you tell us where (what application) you are running this from. It seems it is NOT Word, as you are making a Word instance.

2. This appears to be a commandbutton procedure. OK. WHERE is this commandbutton being run from? It looks like a userform, but is it?

3. On the other hand, you have this code:

Set BMRange = ActiveDocument.Bookmarks("MedicalSummary").Range

and it is used before you make the instance of Word with Set wrdApp = New Word.Application.

As the instruction (Set BMRange = ActiveDocument.Bookmarks("MedicalSummary").Range) has to have an instance of Word - and an ActiveDocument - to execute...WHY are you even making another instance of Word???? There is absolutely no need to make another instance of Word if you are already have one. Word VBA can handle multiple document quite easily.