Copy text to clibboard

I have a webapp that pushes email addresses to Outlook (if installed) for the user. If the user does not use Outlook I want to put the email addresses to the clipboard so they can paste it into whatever email client they are using.

I'm having an issue with the Clipboard object... is not recognizing it. Either I'm missing a reference / Imports statement or this functionality is not available for a web app.

Who is Participating?
Todd GerbertIT ConsultantCommented:
Yes, JavaScript would be the way to go.  You can also use ClientScript.RegisterScriptBlock to have some javascript added to your page on-the-fly, which will be run when the page loads on the client:

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<script runat="server">
	Private Sub SendMailButton_Click(ByVal sender As Object, ByVal e As EventArgs)
		Dim emailString As String = ";"
		Dim javaScript As String = "document.location.href='mailto:?bcc=" & emailString & "';"

		ClientScript.RegisterClientScriptBlock(Me.GetType(), "MailToScript", javaScript, True)
	End Sub

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
		<asp:Button ID="SendMailButton" runat="server" text="Send Mails" OnClick="SendMailButton_Click" />

Open in new window

Nathan BoveSoftware EngineerCommented:
In a web application, all clipboard manipulation must be done on the client.  This typically means it must be done in javascript.  Currently, there is very little support for programmatic clipboard manipulation with javascript since most users would not want to have their browser hijack their clipboard.  Internet explorer has built in support for clipboard manipulation, but none of the other browsers do.

The following article has some information about clipboard manipulation:
Todd GerbertIT ConsultantCommented:
If you create a "" URL and direct the client's browser to that URL, it should automatically open whatever their default mail client is with the "To:" field already filled out.

On an unrelated note, I'm curious as to how you manage to manipulate Outlook from a web app?  I'd seen another similar question and thought it couldn't be done (maybe it was your question)...

(Remember that everything you write in VB.Net, in a web app, runs on the server, not the client - so if you have a button on a web page and in that button's Click event handler you start Outlook, you'll be starting Outlook on the server, not on the computer being used to view your web page)
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

md0333Author Commented:
tgerbert: Well, not sure about that. I do have a question on here for that.

Check if User has Outlook

What I wanted to do was check if user had outlook... and if they did, push the email addresses to to BCC field. If not, put them to the clipboard. I'm interested in your option for creating the "mailto:" url. How would I do that? I need to put the emails in the BCC field of the users default email client.

Here is the code I (thought) I had working for the Outlook stuff...

    Protected Sub ImageForward_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageForward.Click

        Dim bInstalled As Boolean = False

            Dim TheApp As String = "Outlook.Application"

            Dim oApp As Object = CreateObject(TheApp)
            bInstalled = True
            oApp = Nothing
            bInstalled = False
            'MsgBox("Not Installed")
            'MessageBox.Show("Office Application not installed!", "Office Installed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try

        If bInstalled = True Then
            Dim emailstring As String = getEmails()
            'create popup with list of emails user can copy
        End If

    End Sub

    Private Sub EmailTest()

        Dim emailstring As String = getEmails()

        ' Create an Outlook application object instance.
        Dim oApp As Outlook._Application
        oApp = New Outlook.Application()

        ' Create a new MailItem.
        Dim oMsg As Outlook._MailItem
        oMsg = oApp.CreateItem(Outlook.OlItemType.olMailItem)
        oMsg.Subject = ""
        oMsg.Body = ""

        oMsg.BCC = emailstring

        ' Clean up
        oApp = Nothing
        oMsg = Nothing
    End Sub

    Private Function getEmails() As String

        Dim emailstring As String = ""
        Dim i As Integer = 0
        Dim chk As CheckBox = Nothing

        For Each item As GridDataItem In RadGrid1.MasterTableView.Items

            chk = CType(item("SelectColumn").Controls(0), CheckBox)

            If chk.Checked = True Then
                If i = 0 Then
                    emailstring = item("Email").Text.ToString()
                    i = 1
                    emailstring += ";" & item("Email").Text.ToString()
                End If
            End If


        getEmails = emailstring

    End Function
md0333Author Commented:

I think I got it

        Dim emailstring As String = getEmails()
        System.Diagnostics.Process.Start("mailto:?bcc=" & emailstring)

Nathan BoveSoftware EngineerCommented:
None of that code will work.  You are performing all of that processing on the server.  That will only check if the server has outlook installed, not the client.

md0333Author Commented:
OK... so then if I do this in Javascript then it should work???

document.write( "<a href='mailto:?bcc=" + emailstring+ "'></A>" );
Nathan BoveSoftware EngineerCommented:
That will work, as long as the variable "emailstring" is declared on the client.
md0333Author Commented:
This is what I ended up doing (javascript) and it looks like it's working like a charm! Thanks!!

            function InsertMailToTag() {

                var emailstring = getEmails();
                emailWindow ="about:blank", "a", "width=400,height=200,menubar=no,toolbar=no, status=no, location=no");
                with (emailWindow.document) {
                    writeln('<html><head><title>FI-Traxx</title><scr' + 'ipt>self.focus();self.moveTo(200,200);</scr' + 'ipt></head>');
                    writeln('<body bgcolor=#202020 link=#74ac42 vlink=#df7e2a alink=#df7e2a>');
                    writeln('<font face=Arial size=2 color=#ffffff>');
                    writeln('<IMG SRC="Images/fitraxxsplashdkBGMedium.png" WIDTH="170" BORDER="0" HEIGHT="47" HSPACE="10" VSPACE="5" ALIGN=left></IMG>');
                    writeln('<br><br><br><br>Click on the link below to add the selected Email addresses.<br><br>');
                    writeln("<a href='mailto:?bcc=" + emailstring + "'>FI-Traxx: Create Email Campaign</A>");


            function getEmails() {

                var grid = $find("<%=RadGrid1.ClientID %>");
                var MasterTable = grid.get_masterTableView();
                var email;

                var selectedRows = MasterTable.get_selectedItems();
                for (var i = 0; i < selectedRows.length; i++) {
                    var row = selectedRows[i];
                    var cell = MasterTable.getCellByColumnUniqueName(row, "Email")
                    if (i == 0)
                        email = cell.innerHTML
                        email += ";" + cell.innerHTML

                return email;


Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.