![]() |
|
#1
|
||||
|
||||
|
Hey fellas,
I've got a VBA userform that contains a Rich Text Box. I need to retrieve the text in this box and put it into a cell in a table on the parent word document. The problem I'm having is when I use .Text, it leaves out the formatting (for bullets and the like), and when I use .TextRTF, it just spews a bunch of garbage into the cell. I feel like I'm missing something simple, but I can't put my finger on it. Any thoughts, VBA community? |
|
#2
|
|||
|
|||
|
Hi,
I think you could use "FormattedText" Code:
Set rngSource = YourRichTextField YourTable(Cell x, y).Range.FormattedText = rSource NP |
|
#3
|
||||
|
||||
|
I get a type mismatch when I try this code.
Code:
Set oRng = DescriptionBox.TextRTF
oRow.Cells(1).Range.FormattedText = oRng
|
|
#4
|
|||
|
|||
|
Quote:
Code:
oRow.Cells(1).Range.FormattedText = DescriptionBox.TextRTF |
|
#5
|
||||
|
||||
|
Quote:
Code:
oRow.Cells(1).Range.FormattedText.Text = Descriptionbox.TextRTF
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl {\f0\fnil\fcharset0 Tahoma;}{\f1\fnil\fcharset2 Symbol;}} into the cell.
{\*\generator Riched20 12.0.6606.1000;}\viewkind4\uc1 \pard{\pntext\f1\'B7\tab}{\*\pn\pnlvlblt\pnf1\pnin dent0{\pntxtb\'B7}}\fi-360\li360\f0\fs17 This is an example\fs17 \par } Last edited by jpb103; 06-19-2014 at 12:25 PM. Reason: Added code |
|
#6
|
|||
|
|||
|
Quote:
You're setting oRng (which I assume has been defined as a Range object) to a string of formatted text. That should be your type mismatch. And in your second example, you're setting the FormattedText.Text to the RTF data, which is what you are getting. You need to set the FormattedText to the RTF string. |
|
#7
|
||||
|
||||
|
Code:
Dim oRng as Range Set oRng = DescriptionBox.TextRTF oRow.Cells(1).Range.FormattedText = oRng Code:
oRow.Cells(1).Range.FormattedText = DescriptionBox.TextRTF |
|
#8
|
|||
|
|||
|
Quote:
oRng is a Range The first cannot work, as you cannot set a range variable to a string. The second might work, assuming that the .Range.FormattedText accepts a string of formatted text (which is what you see). |
|
#9
|
||||
|
||||
|
FormattedText returns a range. This is why the two code segments are logically identical; both cause the same error, try to do the same thing and have the same result.
|
|
#10
|
|||
|
|||
|
The only other solution I can think of is to save the string retrieved from DescriptionBox.TextRTF to a text file with .rtf extension, then opening that file in word to get the formatted text. That should work.
|
|
#11
|
||||
|
||||
|
What about a command to select all the text in the RTF box? I could probably do a copy/paste, but I don't know the command to select all the text in the RTF box.
|
|
#12
|
|||
|
|||
|
Hi,
with Copy & Paste you will have exactly the same result. The string copied will be: "{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl {\f0\fnil\fcharset0 Tahoma;}{\f1\fnil\fcharset2 Symbol;}} {\*\generator Riched20 12.0.6606.1000;}\viewkind4\uc1 \pard{\pntext\f1\'B7\tab}{\*\pn\pnlvlblt\pnf1\pnin dent0{\pntxtb\'B7}}\fi-360\li360\f0\fs17 This is an example\fs17 \par }" NP |
|
#13
|
|||
|
|||
|
Saving the text to a .rtf file looks like it works to me. You can then open the temp file (invisible if necessary), copy the range to get the formatted text, then delete the temp file.
Code:
Private Function testSaveRTF()
Dim filePath As String
Dim s As String
s = "{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl {\f0\fnil\fcharset0 Tahoma;}{\f1\fnil\fcharset2 Symbol;}}{\*\generator Riched20 12.0.6606.1000;}\viewkind4\uc1\pard{\pntext\f1\'B7\tab}{\*\pn\pnlvlblt\pnf1\pnin dent0{\pntxtb\'B7}}\fi-360\li360\f0\fs17 This is an example\fs17\par }"
filePath = ActiveDocument.Path + "\TempFile.rtf"
Call SaveTextToFile(s, filePath)
End Function
Public Function SaveTextToFile(sText As String, _
Optional FileFullPath As String, _
Optional Overwrite As Boolean = False) As Boolean
SaveTextToFile = True
On Error GoTo ErrorHandler
Dim iFileNumber As Integer
iFileNumber = FreeFile
If Overwrite Then
Open FileFullPath For Output As #iFileNumber
Else
Open FileFullPath For Append As #iFileNumber
End If
Print #iFileNumber, sText
SaveTextToFile = True
Close #iFileNumber
Exit Function
ErrorHandler:
SaveTextToFile = False
Close #iFileNumber
End Function
|
|
#14
|
||||
|
||||
|
It appears that the solution Cosmo proposed earlier does work. It consists of saving the text from the RTF box as its own .rtf file, opening it, select all, copy, paste into cell, close and then delete the .rtf file.
The last problem I have is with the SetFocus method. It doesn't seem to work for RTF text boxes. Does anyone know why, or have another way to set focus? Currently I am setting the focus to the RTF text box in the initialization of the UserForm in which it resides. |
|
#15
|
||||
|
||||
|
OK, I figured it out. The solution? Don't use SetFocus at all! I have no idea why SetFocus fails so miserably at its only intended function, but there it is. Final code follows:
Code:
'///////////////////////////////////////////////////////////////////////////////////
'////////////This function inserts the form data into the table/////////////////////
'///////////////////////////////////////////////////////////////////////////////////
Private Sub OKButton_Click()
Dim oTbl As Word.Table
Dim oRow As Row
Dim oRng As Word.Range
Dim oCtr As InlineShape
Dim dRTF As Word.Documents
'Declare variables
Select Case True
Case DescriptionBox = vbNullString
'Check for empty description box
MsgBox "You must enter a description"
Case PriorityCombo = vbNullString
'Check for empty priority box
MsgBox "You must select the priority."
Case Else
'Input is good, add to table
Set oTbl = ActiveDocument.Tables(1)
Set oRow = oTbl.Rows.Add
'Add new row for data
DescriptionBox.SaveFile ("Temp.rtf")
Documents.Open FileName:="\\Server\EMPNum$\G\Temp.rtf", _
ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto, XMLTransform:=""
Selection.WholeStory
Selection.Copy
oRow.Cells(1).Range.PasteAndFormat (wdPasteDefault)
oRow.Cells(2).Range.Text = PriorityCombo.Text
Set oCtr = oRow.Range.Cells(3).Range.InlineShapes.AddOLEControl(ClassType:="Forms.CheckBox.1")
oCtr.OLEFormat.Object.Caption = ""
oCtr.OLEFormat.Object.Width = 14
'Populate columns
Unload Me
'Unload form
Windows("Temp.rtf [Compatibility Mode]").Close
DeleteFile ("Temp.rtf")
End Select
End Sub
'//////////////////END//////////////////////////////////////////////////////////////
'///////////////////////////////////////////////////////////////////////////////////
'////////////////////Programmed by: John P. Brunetta////////////////////////////////
'///////////////////////Position: Summer Student////////////////////////////////////
'/////////////This function unloads the user form, frmNewItem///////////////////////
'///////////////////////////////////////////////////////////////////////////////////
Private Sub CancelButton_Click()
Unload Me
'Unload form
End Sub
'//////////////////END//////////////////////////////////////////////////////////////
'///////////////////////////////////////////////////////////////////////////////////
'//////////This function initializes the contents of the Priority combobox//////////
'///////////////////////////////////////////////////////////////////////////////////
Private Sub UserForm_Initialize()
PriorityCombo.Clear
PriorityCombo.AddItem "SMT"
PriorityCombo.AddItem "Monthly"
PriorityCombo.AddItem "Newsletter"
DescriptionBox.SelBullet = True
End Sub
'//////////////////END//////////////////////////////////////////////////////////////
'///////////////////////////////////////////////////////////////////////////////////
'/////////This function checks if a file with the name passed exists////////////////
'///////////////////////////////////////////////////////////////////////////////////
Function FileExists(ByVal FileToTest As String) As Boolean
FileExists = (Dir(FileToTest) <> "")
End Function
'//////////////////END//////////////////////////////////////////////////////////////
'///////////////////////////////////////////////////////////////////////////////////
'///////////////This function deletes a file if it exists///////////////////////////
'///////////////////////////////////////////////////////////////////////////////////
Sub DeleteFile(ByVal FileToDelete As String)
If FileExists(FileToDelete) Then 'See above
SetAttr FileToDelete, vbNormal
Kill FileToDelete
End If
End Sub
'//////////////////END//////////////////////////////////////////////////////////////
Last edited by jpb103; 06-23-2014 at 08:47 AM. Reason: To give thanks |
|
| Tags |
| rich text, tables, userforms |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Rich text/Plain text Content Controls in Template
|
michael.fisher5 | Word | 9 | 11-19-2014 06:36 AM |
Rich Text Content Control - Allow User Formatting
|
keithacochrane | Word | 1 | 05-28-2012 05:06 PM |
How to I make text Bold in a User Form -Visual Basic
|
gurp99 | Word VBA | 11 | 03-12-2012 04:05 PM |
| My plain text post got converted to rich text in a reply, how to convert it back? | david.karr | Outlook | 0 | 01-05-2012 09:46 AM |
| Templates: automatic text generation from Rich Text content control | Chickenmunga | Word | 0 | 10-01-2008 11:16 AM |