How to Copy and Paste DDE Links Using CF_LINK in Visual Basic

Last reviewed: June 21, 1995
Article ID: Q106238
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0

SUMMARY

Visual Basic programs can provide an Edit Copy command that puts text onto the Clipboard along with the information needed to make a DDE link to that text. Likewise, you can provide an Edit Paste command that extracts the text and an Edit Paste Link command that extracts the DDE link information, and then initiates a DDE conversation to the source text. You can implement these clipboard operations using the GetText and SetText format CF_LINK (&HBF00) with a string of the format:

   Application|Topic!Item

The Visual Basic manuals and Help menu do not describe how to use the Clipboard format CF_LINK.

NOTE: Visual Basic only supports Clipboard DDE operations for text.

MORE INFORMATION

When a Visual Basic DDE source program uses SetText with format CF_LINK, the format of the string is exe|topic!item.

  • exe is the value of App.EXEName.
  • topic is a form's LinkTopic value.
  • item is the Name of a control.

For example:

   Project1|Form1!Text1

Step-by-Step Example

  1. Start Visual Basic or from the File menu, choose Open Project (ALT, F, O) if Visual Basic is already running. Form1 is created.

  2. Change the form property LinkMode to 1 - Source.

  3. Place a text box named Text1 on the form.

  4. From the Window menu, choose Menu Design and create the following menu structure.

       Caption     Name        Indent Level
       ------------------------------------
       Edit        mEdit         0
       Copy        mCopy         1
       Paste       mPaste        1
       Paste Link  mPasteLink    1
    
    

  5. Enter the following code in the general declarations section:

    Const CF_TEXT = 1 Const CF_LINK = &HBF00

  6. Enter the following code into the form:

       Sub mEdit_Click ()
          mCopy.Enabled = Text1.SelLength > 0
          mPaste.Enabled = Clipboard.GetFormat(CF_TEXT)
          mPasteLink.Enabled = Clipboard.GetFormat(CF_LINK)
       End Sub
    
       Sub mCopy_Click ()
          Clipboard.Clear
          Clipboard.SetText Text1.SelText, CF_TEXT
          Clipboard.SetText "Project1|Form1!Text1", CF_LINK
       End Sub
    
       Sub mPaste_Click ()
          Text1.LinkMode = 0  ' discontinue previous link
          Text1.SelText = Clipboard.GetText(CF_TEXT)
       End Sub
    
       Sub mPasteLink_Click ()
          Dim topic As String  ' app|topic!item
          Dim bang As Integer  ' index of ! within topic
    
          topic = Clipboard.GetText(CF_LINK)
          bang = InStr(topic, "!")
          If bang <> 0 Then
             Text1.LinkMode = 0
             Text1.LinkTopic = Mid$(topic, 1, bang - 1)
             Text1.LinkItem = Mid$(topic, bang + 1)
             On Error Resume Next
             Text1.LinkMode = 1  ' automatic
             If Err <> 0 Then
                MsgBox "Cannot paste link"
             End If
          End If
       End Sub
    
    

  7. From the File menu, choose Make EXE and click OK. This creates PROJECT1.EXE.

  8. Launch PROJECT1.EXE twice so that two instances are running. Position them so that you can see them both at the same time. In the first instance, select the text in the text box, then from the Edit menu choose copy. Switch to the second instance and from the Edit menu choose Paste Link. Switch back to the first instance and change the contents of the text box. These changes appear immediately in the second instance.


Additional reference words: docerr 3.00
KBCategory: kbinterop kbprg kbcode
KBSubCategory: IAPDDE


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.