Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2002
    Location
    Rickmansworth UK
    Posts
    1

    Unhappy Unanswered: Copy to clipboard function in VBA

    Hi

    I'm trying to create a function where the text fields in a form can be copied to the clipboard.
    I know the VB code for this but can't get it to work in VBA.

    Any suggestions?

    Thanks

  2. #2
    Join Date
    Apr 2002
    Posts
    139
    Hai Nick,

    Try your luck with "DoCmd.RunCommand acCmdCopy", after you have selected your text. Available in Access 97 at least.

    RunCommand has a long list of interesting functionality, check it out!

    hth
    Arco

  3. #3
    Join Date
    Sep 2002
    Location
    Boston, MA
    Posts
    8
    Originally posted by marion
    Try your luck with "DoCmd.RunCommand acCmdCopy", after you have selected your text. Available in Access 97 at least.
    Is it possible to copy a string in VB to the clipboard? I don't have the text selected; just stored in a string variable.

    Thanks!
    Jenn

  4. #4
    Join Date
    May 2003
    Location
    Dallas
    Posts
    817
    Provided Answers: 5
    I have a button on a form called cmdClip

    I dim mystring as string
    mystring = forms!formname!fieldname


    i run some other code and then I call the function on the following line

    ClipBoard_SetData (mystring)


    prior to doing this create a module called modClip

    and place all the followig code in the module

    Option Compare Database
    Option Explicit
    Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) _
    As Long
    Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) _
    As Long
    Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, _
    ByVal dwBytes As Long) As Long
    Declare Function CloseClipboard Lib "User32" () As Long
    Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) _
    As Long
    Declare Function EmptyClipboard Lib "User32" () As Long
    Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, _
    ByVal lpString2 As Any) As Long
    Declare Function SetClipboardData Lib "User32" (ByVal wFormat _
    As Long, ByVal hMem As Long) As Long

    Public Const GHND = &H42
    Public Const CF_TEXT = 1
    Public Const MAXSIZE = 4096


    Function ClipBoard_SetData(mystring As String)
    Dim hGlobalMemory As Long, lpGlobalMemory As Long
    Dim hClipMemory As Long, X As Long

    hGlobalMemory = GlobalAlloc(GHND, Len(mystring) + 1)

    lpGlobalMemory = GlobalLock(hGlobalMemory)

    lpGlobalMemory = lstrcpy(lpGlobalMemory, mystring)

    If GlobalUnlock(hGlobalMemory) <> 0 Then
    MsgBox "Could not unlock memory location. Copy aborted."
    GoTo OutOfHere2
    End If

    ' Open the Clipboard to copy data to.
    If OpenClipboard(0&) = 0 Then
    MsgBox "Could not open the Clipboard. Copy aborted."
    Exit Function
    End If

    ' Clear the Clipboard.
    X = EmptyClipboard()

    ' Copy the data to the Clipboard.
    hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)

    OutOfHere2:

    If CloseClipboard() = 0 Then
    MsgBox "Could not close Clipboard."
    End If

    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •