View Single Post
 
Old 06-23-2014, 06:24 AM
Cosmo Cosmo is offline Windows Vista 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