Link to home
Start Free TrialLog in
Avatar of joex
joex

asked on

Clipboard - set data for Word and Notepad

The following comes from using vb.net, but the issue may be common to VB:

When using the following to make rtf available to paste into word/wordpad:

    Clipboard.SetDataObject(New DataObject(DataFormats.Rtf, entireFile))

The text cannot be pasted into notepad.

Please indicate whether there is a way to copy text to the clipboard so that it can be pasted into both Word and notepad.   Perhaps it is possible to invoke SetDataObject for RTF and text.

Thanks.
Avatar of JackOfPH
JackOfPH
Flag of Philippines image

The following method is run in an application. It places a persistent copy of the selected text data in the text box on the system Clipboard. This code assumes button1, textBox1, and textBox2 have been created and placed on a form.

Visual Basic  Copy Code
Private Sub button1_Click(sender As Object, e As System.EventArgs)
    ' Takes the selected text from a text box and puts it on the clipboard.
    If textBox1.SelectedText <> "" Then
        Clipboard.SetDataObject(textBox1.SelectedText, True)
    Else
        textBox2.Text = "No text selected in textBox1"
    End If
End Sub 'button1_Click

 
In a different application, the following method retrieves the text from the system Clipboard and pastes the text into textBox2. This code assumes button2 and textBox2 have been created and placed on a form.

Visual Basic  Copy Code
Private Sub button2_Click(sender As Object, e As System.EventArgs)
    ' Declares an IDataObject to hold the data returned from the clipboard.
    ' Retrieves the data from the clipboard.
    Dim iData As IDataObject = Clipboard.GetDataObject()
   
    ' Determines whether the data is in a format you can use.
    If iData.GetDataPresent(DataFormats.Text) Then
        ' Yes it is, so display it in a text box.
        textBox2.Text = CType(iData.GetData(DataFormats.Text), String)
    Else
        ' No it is not.
        textBox2.Text = "Could not retrieve data off the clipboard."
    End If
End Sub 'button2_Click

 
Above code is from the msdn...
The Clipboard can be used to store data, such as text and images. Because the Clipboard is shared by all processes, it can be used to transfer data between them. The My.Computer.Clipboard object allows you to easily access the Clipboard and to read and write to it. The SetAudio, SetData, SetFileDropDownList, SetImage, and SetText methods allow you to place data on the Clipboard.

Security Note  
Because the Clipboard can be accessed by other users, do not use it to store sensitive information, such as passwords or confidential data.
 

To write text to the Clipboard
Use the My.Computer.Clipboard.SetText method to write text to the Clipboard. The following code writes the string "This is a test string" to the Clipboard.

Visual Basic  Copy Code
My.Computer.Clipboard.SetText("This is a test string.")

 


To write text to the Clipboard in a specific format
Use the My.Computer.Clipboard.SetText method to write text to the Clipboard, including the type of TextDataFormat. The following code writes the string "This is a test string" to the Clipboard as RTF text.

Visual Basic  Copy Code
My.Computer.Clipboard.SetText("This is a test string.", _
System.Windows.Forms.TextDataFormat.Rtf)

 


To write data to the Clipboard
Use the My.Computer.Clipboard.SetData method to write data to the Clipboard. This example writes the DataObject dataChunk to the Clipboard in the custom format specialFormat.

Visual Basic  Copy Code
My.Computer.Clipboard.SetData("specialFormat", dataChunk)

 
Avatar of joex
joex

ASKER

It is interesting that when text is copied from either Word or a web page, it can usually be pasted into either wordpad or notepad.

Perhaps the above answers whether it is possible for the clipboard to contain data in two formats, but it is not yet clear.

After executing either or both of the following, Please indicate what would appear in Word and Notepad after pasting into each:

My.Computer.Clipboard.SetText("This is not rtf text", System.Windows.Forms.TextDataFormat.Rtf)
My.Computer.Clipboard.SetText("This is plain text.")

Thanks





Avatar of joex

ASKER

Unfortunately, VB 6 is not installed on my workstation in order to test the above.
The code that I gave is from vb 2005. and it works well for me...
Avatar of joex

ASKER

It is still unclear that when actual RTF such as the sample at bottom, that the following enables a user to paste into both Word and Notepad.

My.Computer.Clipboard.SetText(__REPLACE_WITH_ACTUAL_RTF__, System.Windows.Forms.TextDataFormat.Rtf)

The paste menu entry is disabled when a similar approach is used in vb.net.

Thanks.

{\rtf1\ansi\ansicpg1252\uc1\deff0\stshfdbch0\stshfloch0\stshfhich0\stshfbi0\deflang1033\deflangfe1033{\fonttbl{\f0\froman\fcharset0\fprq2{\*\panose 02020603050405020304}Times New Roman;}{\f44\froman\fcharset238\fprq2 Times New Roman CE;}
{\f45\froman\fcharset204\fprq2 Times New Roman Cyr;}{\f47\froman\fcharset161\fprq2 Times New Roman Greek;}{\f48\froman\fcharset162\fprq2 Times New Roman Tur;}{\f49\froman\fcharset177\fprq2 Times New Roman (Hebrew);}
{\f50\froman\fcharset178\fprq2 Times New Roman (Arabic);}{\f51\froman\fcharset186\fprq2 Times New Roman Baltic;}{\f52\froman\fcharset163\fprq2 Times New Roman (Vietnamese);}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;
\red0\green255\blue0;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;
\red128\green128\blue128;\red192\green192\blue192;\red255\green102\blue0;\red51\green102\blue255;\red0\green204\blue255;\red51\green153\blue102;\red255\green204\blue0;}{\stylesheet{\ql \li0\ri0\widctlpar\aspalpha\aspnum\faauto\adjustright\rin0\lin0\itap0
\fs24\lang1033\langfe1033\cgrid\langnp1033\langfenp1033 \snext0 Normal;}{\*\cs10 \additive \ssemihidden Default Paragraph Font;}{\*
\ts11\tsrowd\trftsWidthB3\trpaddl108\trpaddr108\trpaddfl3\trpaddft3\trpaddfb3\trpaddfr3\tscellwidthfts0\tsvertalt\tsbrdrt\tsbrdrl\tsbrdrb\tsbrdrr\tsbrdrdgl\tsbrdrdgr\tsbrdrh\tsbrdrv
\ql \li0\ri0\widctlpar\aspalpha\aspnum\faauto\adjustright\rin0\lin0\itap0 \fs20\lang1024\langfe1024\cgrid\langnp1024\langfenp1024 \snext11 \ssemihidden Normal Table;}}{\*\latentstyles\lsdstimax156\lsdlockeddef0}{\*\rsidtbl \rsid72612\rsid73597\rsid74820
\rsid87657\rsid141602\rsid142091\rsid160941\rsid205548\rsid226523\rsid273327\rsid339628\rsid525625\rsid621591\rsid659059\rsid736182\rsid799865\rsid805218\rsid933553\rsid983672\rsid987205\rsid997701\rsid1064811\rsid1066379\rsid1068917\rsid1070145
\rsid1182991\rsid1260524\rsid1313169\rsid1320340\rsid1332089\rsid1390843\rsid1395600\rsid1398606\rsid1518886\rsid1596734\rsid1640355\rsid1650518\rsid1656675\rsid1657937\rsid1661279\rsid1665709\rsid1717327\rsid1790794\rsid1794857\rsid1846210\rsid1864030
\rsid1975165\rsid1986711\rsid1993882\rsid1994068\rsid2038339\rsid2053022\rsid2098127\rsid2184999\rsid2305903\rsid2313762\rsid2321605\rsid2372145\rsid2387813\rsid2438495\rsid2443641\rsid2496698\rsid2556167\rsid2557668\rsid2639353\rsid2703474\rsid2827480
\rsid3021033\rsid3081958\rsid3168001\rsid3215946\rsid3230844\rsid3281795\rsid3374924\rsid3416909\rsid3493616\rsid3500160\rsid3505342\rsid3566401\rsid3619070\rsid3687113\rsid3741420\rsid3802949\rsid3810489\rsid3811619\rsid3813988\rsid3880001\rsid3889481
\rsid3946679\rsid4028641\rsid4067901\rsid4089453\rsid4132539\rsid4141469\rsid4154818\rsid4156413\rsid4158673\rsid4209677\rsid4223987\rsid4292397\rsid4337110\rsid4347677\rsid4390951\rsid4415554\rsid4422955\rsid4456983\rsid4473990\rsid4597594\rsid4608549
\rsid4731317\rsid4861579\rsid4863039\rsid4870189\rsid4916291\rsid4946343\rsid4982216\rsid4991785\rsid5065862\rsid5134275\rsid5209488\rsid5249029\rsid5261794\rsid5273739\rsid5316510\rsid5451859\rsid5458743\rsid5509964\rsid5521875\rsid5571358\rsid5579516
\rsid5587706\rsid5587969\rsid5595154\rsid5652974\rsid5707970\rsid5715584\rsid5717333\rsid5795387\rsid5832726\rsid5973356\rsid6095011\rsid6115298\rsid6122406\rsid6185002\rsid6245703\rsid6301622\rsid6357258\rsid6362893\rsid6382413\rsid6452264\rsid6498160
\rsid6507655\rsid6513830\rsid6568821\rsid6572685\rsid6577203\rsid6751883\rsid6764250\rsid6765771\rsid6815980\rsid6830927\rsid6886283\rsid6959365\rsid6966065\rsid6969326\rsid6972026\rsid7029018\rsid7106049\rsid7144984\rsid7152741\rsid7153564\rsid7161424
\rsid7171883\rsid7217823\rsid7227678\rsid7284330\rsid7362135\rsid7481775\rsid7491697\rsid7541096\rsid7556382\rsid7682625\rsid7799784\rsid7830996\rsid7878979\rsid7897007\rsid7946654\rsid7960046\rsid8006909\rsid8063384\rsid8141955\rsid8154790\rsid8262833
\rsid8285784\rsid8288129\rsid8336354\rsid8341851\rsid8391226\rsid8415419\rsid8591821\rsid8665475\rsid8666462\rsid8668734\rsid8670718\rsid8681076\rsid8732371\rsid8853893\rsid8856979\rsid8925429\rsid8929072\rsid9069050\rsid9071469\rsid9177248\rsid9202892
\rsid9253140\rsid9258355\rsid9262320\rsid9314425\rsid9325442\rsid9330058\rsid9445609\rsid9506986\rsid9520352\rsid9655538\rsid9656226\rsid9900647\rsid9902605\rsid10028707\rsid10035863\rsid10038018\rsid10056826\rsid10161363\rsid10243990\rsid10251501
\rsid10300354\rsid10440322\rsid10495692\rsid10635344\rsid10699386\rsid10701274\rsid10713204\rsid10815296\rsid10837185\rsid10898352\rsid10900885\rsid10902813\rsid10905590\rsid11038263\rsid11151611\rsid11171633\rsid11207601\rsid11219632\rsid11282008
\rsid11429775\rsid11474782\rsid11538539\rsid11565507\rsid11600633\rsid11761893\rsid11876262\rsid11885240\rsid11939464\rsid11941166\rsid12010711\rsid12011025\rsid12011534\rsid12062547\rsid12066963\rsid12132184\rsid12283856\rsid12284987\rsid12322174
\rsid12394436\rsid12405730\rsid12528692\rsid12533613\rsid12597165\rsid12605330\rsid12722304\rsid12782256\rsid12789627\rsid12849354\rsid12871973\rsid12921587\rsid12985109\rsid12987205\rsid13124370\rsid13134038\rsid13204936\rsid13306996\rsid13323750
\rsid13396670\rsid13445977\rsid13500642\rsid13532462\rsid13662161\rsid13898721\rsid13904590\rsid13979588\rsid14056402\rsid14103076\rsid14116242\rsid14122680\rsid14222332\rsid14290438\rsid14305734\rsid14364725\rsid14372085\rsid14435520\rsid14505102
\rsid14580184\rsid14692681\rsid14705045\rsid14712380\rsid14757170\rsid14816328\rsid14833049\rsid14834701\rsid14839263\rsid14841664\rsid14907542\rsid14961276\rsid15101214\rsid15155615\rsid15214619\rsid15222041\rsid15290168\rsid15482883\rsid15547632
\rsid15617159\rsid15628138\rsid15677351\rsid15744376\rsid15796186\rsid15805389\rsid15825697\rsid15931355\rsid15932351\rsid16003499\rsid16005392\rsid16019084\rsid16061927\rsid16083069\rsid16088541\rsid16125105\rsid16125264\rsid16284448\rsid16322916
\rsid16391264\rsid16467200\rsid16544991\rsid16610288\rsid16649139\rsid16678562\rsid16722334}{\*\generator Microsoft Word 11.0.6502;}{\info{\title addbbasdfasdfasdf}{\author tmccardle}{\operator tmccardle}{\creatim\yr2006\mo8\dy17\hr15\min51}
{\revtim\yr2006\mo8\dy18\hr12\min31}{\version7}{\edmins4}{\nofpages4}{\nofwords16}{\nofchars94}{\*\company csc}{\nofcharsws109}{\vern24579}}
\widowctrl\ftnbj\aenddoc\noxlattoyen\expshrtn\noultrlspc\dntblnsbdb\nospaceforul\hyphcaps0\formshade\horzdoc\dgmargin\dghspace180\dgvspace180\dghorigin1701\dgvorigin1984\dghshow1\dgvshow1
\jexpand\viewkind4\viewscale100\pgbrdrhead\pgbrdrfoot\splytwnine\ftnlytwnine\htmautsp\nolnhtadjtbl\useltbaln\alntblind\lytcalctblwd\lyttblrtgr\lnbrkrule\nobrkwrptbl\snaptogridincell\allowfieldendsel\wrppunct
\asianbrkrule\rsidroot6751883\newtblstyruls\nogrowautofit \fet0\sectd \linex0\endnhere\sectlinegrid360\sectdefaultcl\sftnbj {\*\pnseclvl1\pnucrm\pnstart1\pnindent720\pnhang {\pntxta .}}{\*\pnseclvl2\pnucltr\pnstart1\pnindent720\pnhang {\pntxta .}}
{\*\pnseclvl3\pndec\pnstart1\pnindent720\pnhang {\pntxta .}}{\*\pnseclvl4\pnlcltr\pnstart1\pnindent720\pnhang {\pntxta )}}{\*\pnseclvl5\pndec\pnstart1\pnindent720\pnhang {\pntxtb (}{\pntxta )}}{\*\pnseclvl6\pnlcltr\pnstart1\pnindent720\pnhang {\pntxtb (}
{\pntxta )}}{\*\pnseclvl7\pnlcrm\pnstart1\pnindent720\pnhang {\pntxtb (}{\pntxta )}}{\*\pnseclvl8\pnlcltr\pnstart1\pnindent720\pnhang {\pntxtb (}{\pntxta )}}{\*\pnseclvl9\pnlcrm\pnstart1\pnindent720\pnhang {\pntxtb (}{\pntxta )}}\pard\plain
\ql \li0\ri0\widctlpar\aspalpha\aspnum\faauto\adjustright\rin0\lin0\itap0 \fs24\lang1033\langfe1033\cgrid\langnp1033\langfenp1033 {\insrsid12871973 Page 1 }{\insrsid7799784 A}{\cf17\insrsid6751883 dd}{\cf18\insrsid6751883 bb}{\fs28\cf18\insrsid6751883
asdfasdf}{\fs28\cf19\insrsid6751883 asdf}{\fs28\cf19\insrsid9202892
\par }{\fs28\cf19\insrsid12871973 \page Page 2 - }{\fs28\cf20\insrsid12871973 asdfasdf}{\fs28\cf20\insrsid12871973\charrsid12871973
\par }{\fs28\cf19\insrsid8341851
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par }{\fs28\cf20\insrsid6765771 Line 2
\par \page }{\fs28\cf21\insrsid6765771\charrsid6765771 Page 2 \endash  line 1}{\fs28\cf21\insrsid6765771
\par }{\fs28\cf21\insrsid6765771\charrsid6765771
\par }}

ASKER CERTIFIED SOLUTION
Avatar of JackOfPH
JackOfPH
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial