devxlogo

Transfer Data from a RichTextBox Control to an Outlook Message

Transfer Data from a RichTextBox Control to an Outlook Message

When automating Outlook from a VB application, I needed to find a way to transfer data from a RichTextBox control to an Outlook message. Unfortunately using Clipboard.SetText and Clipboard.GetText did not save the formatting of the entered text. However, I found the best way to transfer formatted text from a RichTextBox control to an Outlook message was to use “Sendkeys” to simulate Ctrl + C and Ctrl + V for copying and pasting (respectively).
This code assumes a project reference to the Outlook Object Library. My RichTextBox Control is named rtfData:

 	Dim objOutlook As New Outlook.Application	Dim objOutlookMsg As Outlook.MailItem	Set objOutlookMsg = objOutlook.CreateItem(0)	objOutlookMsg.BCC = "[email protected]"        	objOutlookMsg.To = "[email protected]"       	objOutlookMsg.Subject = Form1.Text1.Text            	Clipboard.Clear            rtfData.SelStart = "0"            rtfData.SelLength = Len(rtfData.Text)            rtfData.SetFocus                        SendKeys "^c", True      'copy text            Me.Hide            objOutlookMsg.Display  'display Outlook message            SendKeys "^v", True    ' paste text            Me.Show

size=3>
The above code will paste all text from the RichTextBox control to theOutlook Message and will preserve all formatting.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist