Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-19-2014, 08:17 AM
jpb103's Avatar
jpb103 jpb103 is offline Retrieving Rich Text from a RTF Text box in a User Form Windows 7 64bit Retrieving Rich Text from a RTF Text box in a User Form Office 2007
Advanced Beginner
Retrieving Rich Text from a RTF Text box in a User Form
 
Join Date: May 2014
Location: Thunder Bay, Ontario
Posts: 58
jpb103 is on a distinguished road
Default Retrieving Rich Text from a RTF Text box in a User Form

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?
Reply With Quote
  #2  
Old 06-19-2014, 10:52 AM
NobodysPerfect NobodysPerfect is offline Retrieving Rich Text from a RTF Text box in a User Form Windows 8 Retrieving Rich Text from a RTF Text box in a User Form Office 2010 32bit
Competent Performer
 
Join Date: Jan 2014
Location: Germany
Posts: 136
NobodysPerfect is on a distinguished road
Default

Hi,

I think you could use "FormattedText"

Code:
Set rngSource = YourRichTextField
YourTable(Cell x, y).Range.FormattedText = rSource
HTH
NP
Reply With Quote
  #3  
Old 06-19-2014, 11:30 AM
jpb103's Avatar
jpb103 jpb103 is offline Retrieving Rich Text from a RTF Text box in a User Form Windows 7 64bit Retrieving Rich Text from a RTF Text box in a User Form Office 2007
Advanced Beginner
Retrieving Rich Text from a RTF Text box in a User Form
 
Join Date: May 2014
Location: Thunder Bay, Ontario
Posts: 58
jpb103 is on a distinguished road
Default

I get a type mismatch when I try this code.
Code:
        Set oRng = DescriptionBox.TextRTF
        oRow.Cells(1).Range.FormattedText = oRng
Where DescriptionBox is my RTF text box and oRow.Cells(1).Range.Text is where I'd like to put the contents of said RTF text box.
Reply With Quote
  #4  
Old 06-19-2014, 12:18 PM
Cosmo Cosmo is offline Retrieving Rich Text from a RTF Text box in a User Form Windows Vista Retrieving Rich Text from a RTF Text box in a User Form Office 2007
Competent Performer
 
Join Date: Mar 2012
Posts: 240
Cosmo is on a distinguished road
Default

Quote:
Originally Posted by jpb103 View Post
I get a type mismatch when I try this code.
Code:
        Set oRng = DescriptionBox.TextRTF
        oRow.Cells(1).Range.FormattedText = oRng
Where DescriptionBox is my RTF text box and oRow.Cells(1).Range.Text is where I'd like to put the contents of said RTF text box.
This might work:

Code:
 
oRow.Cells(1).Range.FormattedText  = DescriptionBox.TextRTF
Reply With Quote
  #5  
Old 06-19-2014, 12:20 PM
jpb103's Avatar
jpb103 jpb103 is offline Retrieving Rich Text from a RTF Text box in a User Form Windows 7 64bit Retrieving Rich Text from a RTF Text box in a User Form Office 2007
Advanced Beginner
Retrieving Rich Text from a RTF Text box in a User Form
 
Join Date: May 2014
Location: Thunder Bay, Ontario
Posts: 58
jpb103 is on a distinguished road
Default Same thing

Quote:
Originally Posted by Cosmo View Post
This might work:

Code:
 
oRow.Cells(1).Range.FormattedText  = DescriptionBox.TextRTF
This code is logically identical to the code segment I've just provided. I have made some progress, though not necessarily in the right direction. The following code does not produce a type-mismatch error, but it does the same thing it did initially (it dumps all the formatting info into the cell, which looks stupid)
Code:
oRow.Cells(1).Range.FormattedText.Text = Descriptionbox.TextRTF
When the user puts something like:
  • This is an example
into the rich text box, it dumps this:
{\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 }

into the cell.

Last edited by jpb103; 06-19-2014 at 12:25 PM. Reason: Added code
Reply With Quote
  #6  
Old 06-19-2014, 12:29 PM
Cosmo Cosmo is offline Retrieving Rich Text from a RTF Text box in a User Form Windows Vista Retrieving Rich Text from a RTF Text box in a User Form Office 2007
Competent Performer
 
Join Date: Mar 2012
Posts: 240
Cosmo is on a distinguished road
Default

Quote:
Originally Posted by jpb103 View Post
This code is logically identical to the code segment I've just provided.
No, it's not.

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.
Reply With Quote
  #7  
Old 06-19-2014, 12:33 PM
jpb103's Avatar
jpb103 jpb103 is offline Retrieving Rich Text from a RTF Text box in a User Form Windows 7 64bit Retrieving Rich Text from a RTF Text box in a User Form Office 2007
Advanced Beginner
Retrieving Rich Text from a RTF Text box in a User Form
 
Join Date: May 2014
Location: Thunder Bay, Ontario
Posts: 58
jpb103 is on a distinguished road
Default

Code:
Dim oRng as Range
Set oRng = DescriptionBox.TextRTF
oRow.Cells(1).Range.FormattedText = oRng
Code:
oRow.Cells(1).Range.FormattedText = DescriptionBox.TextRTF
These two code segments perform exactly the same operation. Setting oRng is redundant to the logic.
Reply With Quote
  #8  
Old 06-19-2014, 12:41 PM
Cosmo Cosmo is offline Retrieving Rich Text from a RTF Text box in a User Form Windows Vista Retrieving Rich Text from a RTF Text box in a User Form Office 2007
Competent Performer
 
Join Date: Mar 2012
Posts: 240
Cosmo is on a distinguished road
Default

Quote:
Originally Posted by jpb103 View Post
Code:
Dim oRng as Range
Set oRng = DescriptionBox.TextRTF
oRow.Cells(1).Range.FormattedText = oRng
Code:
oRow.Cells(1).Range.FormattedText = DescriptionBox.TextRTF
These two code segments perform exactly the same operation. Setting oRng is redundant to the logic.
DescriptionBox.TextRTF is a String
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).
Reply With Quote
  #9  
Old 06-19-2014, 12:53 PM
jpb103's Avatar
jpb103 jpb103 is offline Retrieving Rich Text from a RTF Text box in a User Form Windows 7 64bit Retrieving Rich Text from a RTF Text box in a User Form Office 2007
Advanced Beginner
Retrieving Rich Text from a RTF Text box in a User Form
 
Join Date: May 2014
Location: Thunder Bay, Ontario
Posts: 58
jpb103 is on a distinguished road
Default

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.
Reply With Quote
  #10  
Old 06-19-2014, 01:26 PM
Cosmo Cosmo is offline Retrieving Rich Text from a RTF Text box in a User Form Windows Vista Retrieving Rich Text from a RTF Text box in a User Form Office 2007
Competent Performer
 
Join Date: Mar 2012
Posts: 240
Cosmo is on a distinguished road
Default

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.
Reply With Quote
  #11  
Old 06-23-2014, 05:16 AM
jpb103's Avatar
jpb103 jpb103 is offline Retrieving Rich Text from a RTF Text box in a User Form Windows 7 64bit Retrieving Rich Text from a RTF Text box in a User Form Office 2007
Advanced Beginner
Retrieving Rich Text from a RTF Text box in a User Form
 
Join Date: May 2014
Location: Thunder Bay, Ontario
Posts: 58
jpb103 is on a distinguished road
Default

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.
Reply With Quote
  #12  
Old 06-23-2014, 05:32 AM
NobodysPerfect NobodysPerfect is offline Retrieving Rich Text from a RTF Text box in a User Form Windows 8 Retrieving Rich Text from a RTF Text box in a User Form Office 2010 32bit
Competent Performer
 
Join Date: Jan 2014
Location: Germany
Posts: 136
NobodysPerfect is on a distinguished road
Default

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

Reply With Quote
  #13  
Old 06-23-2014, 06:24 AM
Cosmo Cosmo is offline Retrieving Rich Text from a RTF Text box in a User Form Windows Vista Retrieving Rich Text from a RTF Text box in a User Form Office 2007
Competent Performer
 
Join Date: Mar 2012
Posts: 240
Cosmo is on a distinguished road
Default

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
Reply With Quote
  #14  
Old 06-23-2014, 08:04 AM
jpb103's Avatar
jpb103 jpb103 is offline Retrieving Rich Text from a RTF Text box in a User Form Windows 7 64bit Retrieving Rich Text from a RTF Text box in a User Form Office 2007
Advanced Beginner
Retrieving Rich Text from a RTF Text box in a User Form
 
Join Date: May 2014
Location: Thunder Bay, Ontario
Posts: 58
jpb103 is on a distinguished road
Default

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.
Reply With Quote
  #15  
Old 06-23-2014, 08:45 AM
jpb103's Avatar
jpb103 jpb103 is offline Retrieving Rich Text from a RTF Text box in a User Form Windows 7 64bit Retrieving Rich Text from a RTF Text box in a User Form Office 2007
Advanced Beginner
Retrieving Rich Text from a RTF Text box in a User Form
 
Join Date: May 2014
Location: Thunder Bay, Ontario
Posts: 58
jpb103 is on a distinguished road
Default

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//////////////////////////////////////////////////////////////
Special Thanks to Cosmo and NP for your help!

Last edited by jpb103; 06-23-2014 at 08:47 AM. Reason: To give thanks
Reply With Quote
Reply

Tags
rich text, tables, userforms

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Retrieving Rich Text from a RTF Text box in a User Form Rich text/Plain text Content Controls in Template michael.fisher5 Word 9 11-19-2014 06:36 AM
Retrieving Rich Text from a RTF Text box in a User Form Rich Text Content Control - Allow User Formatting keithacochrane Word 1 05-28-2012 05:06 PM
Retrieving Rich Text from a RTF Text box in a User Form 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

Other Forums: Access Forums

All times are GMT -7. The time now is 01:14 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft