Getting RTF Text off the clipboard.

Need to get RTF text off the clipboard? Use the IDataObject interface to see if there is RTF data on the clipboard. In the sample below, copy some RTF data onto the clipboard from another application then simply click anywhere on this form to see the actual RTF string data.

Option Strict On
Option
 Explicit On

Imports
 System
Imports System.Windows.Forms
Imports System.Drawing
Imports Microsoft.VisualBasic

Public Class Form1
    
Inherits Form

    
Public Sub New()
        
Me.Text = "Clipboard Test"
        AddHandler Me.Click, AddressOf MouseClick
    
End Sub

    Public Sub MouseClick(byval sender As objectbyval e As EventArgs)
        
If Not Clipboard.GetDataObject() Is Nothing
            Dim dobj As IDataObject = Clipboard.GetDataObject()
            
If dobj.GetDataPresent(DataFormats.RTF) then
                Dim rtfobj As Object = dobj.GetData(DataFormats.RTF)
                MsgBox(rtfobj.ToString())
            
End if
        End if
    End Sub

    <STAThread()> _
    
Shared Sub Main()
        Application.Run(
New Form1())
    
End Sub
End
 Class

No Comments