Description and workaround:
It appears that Outlook produces different RTF contents when using its different Copy commands. In some cases the RTF string produced by Outlook contains empty spans which is not a valid element in our implementation. To handle that case, you can subscribe to the CommandExecuting event to capture the Paste command before it was executed, strip the empty spans, and then modify the clipboard contents with a valid RTF string.
Additionally, when you try to delete the text of pasted hyperlinks using backspace, an exception will be thrown at some point (for example, after pasting the link \\server\folder1\somefile.txt and deleting the dot).
The following code snippet demonstrates how to handle deleting pasted links:
Private Sub radRichTextEditor1_CommandExecuting(sender As Object, e As CommandExecutingEventArgs) Handles radRichTextEditor1.CommandExecuting
If TypeOf e.Command Is DeleteCommand
Me.RadRichTextEditor1.RichTextBoxElement.InvalidateMeasure(true)
Me.RadRichTextEditor1.RichTextBoxElement.UpdateLayout()
End If
End Sub
The following code snippet demonstrates how to handle removing empty spans:
Private Sub radRichTextEditor1_CommandExecuting(sender As Object, e As CommandExecutingEventArgs) Handles radRichTextEditor1.CommandExecuting
If Not (TypeOf e.Command Is PasteCommand) Then
Return
End If
Dim docString As String = Nothing
Dim docObj As Object = Clipboard.GetData("Rich Text Format")
If docObj IsNot Nothing AndAlso docObj.[GetType]() = GetType(String) Then
docString = DirectCast(docObj, String)
End If
Dim document As RadDocument = Nothing
Using stream As New MemoryStream()
Dim writer As New StreamWriter(stream)
writer.Write(docString)
writer.Flush()
stream.Seek(0, SeekOrigin.Begin)
Try
document = New RtfFormatProvider().Import(stream)
Catch ex As Exception
System.Diagnostics.Debug.WriteLine("Error reading document from clipboard:" & vbLf + ex.ToString())
End Try
End Using
If document IsNot Nothing Then
Dim emptySpans As New List(Of Span)()
For Each span As var In document.EnumerateChildrenOfType(Of Span)()
If [String].IsNullOrEmpty(span.Text) Then
emptySpans.Add(span)
End If
Next
If emptySpans.Count = 0 Then
Return
End If
For Each span As var In emptySpans
span.Parent.Children.Remove(span)
Next
Dim modifiedRtf As String = New RtfFormatProvider().Export(document)
Clipboard.SetData("Rich Text Format", modifiedRtf)
End If
End Sub