Results 1 to 4 of 4
  1. #1
    Josha is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2019
    Location
    Victoria, Australia
    Posts
    42

    Create a button that copies a predefined string of text to the clipboard


    I want to create a button that copies a predefined string of text to the clipboard so that my users can click it and then use Ctrl V to paste that predefined text into a field. Is this possible? Im using Access for Office 365 MSO (16.0.11929.20436) 32 bit

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    paste this code into a module,

    usage: CopyToClipboard txtBox
    or
    CopyToClipboard "my String"


    Code:
    Option Compare Database
    Option Explicit
    
    
    Public Const GHND = &H42
    Public Const CF_TEXT = 1
    Public Const MAXSIZE = 4096
        #If VBA7 Then
            Declare PtrSafe Function GlobalUnlock Lib "kernel32" (ByVal hMem As LongPtr) As LongPtr
            Declare PtrSafe Function GlobalLock Lib "kernel32" (ByVal hMem As LongPtr) As LongPtr
            Declare PtrSafe Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As LongPtr) As LongPtr
            Declare PtrSafe Function CloseClipboard Lib "User32" () As Long
            Declare PtrSafe Function OpenClipboard Lib "User32" (ByVal hwnd As LongPtr) As LongPtr
            Declare PtrSafe Function EmptyClipboard Lib "User32" () As Long
            Declare PtrSafe Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As LongPtr
            Declare PtrSafe Function SetClipboardData Lib "User32" (ByVal wFormat As Long, ByVal hMem As LongPtr) As LongPtr
        #Else
            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
        #End If
        
    
    
    Sub CopyToClipboard(pvMyString As String)
            #If VBA7 Then
                Dim hGlobalMemory As LongPtr
                Dim hClipMemory   As LongPtr
                Dim lpGlobalMemory    As LongPtr
            #Else
                Dim hGlobalMemory As Long
                Dim hClipMemory   As Long
                Dim lpGlobalMemory    As Long
            #End If
    
    
            Dim x                 As Long
    
    
            ' Allocate moveable global memory.
           '-------------------------------------------
           hGlobalMemory = GlobalAlloc(GHND, Len(pvMyString) + 1)
    
    
            ' Lock the block to get a far pointer
           ' to this memory.
           lpGlobalMemory = GlobalLock(hGlobalMemory)
    
    
            ' Copy the string to this global memory.
           lpGlobalMemory = lstrcpy(lpGlobalMemory, pvMyString)
    
    
            ' Unlock the memory.
           If GlobalUnlock(hGlobalMemory) <> 0 Then
                MsgBox "Could not unlock memory location. Copy aborted."
                GoTo PrepareToClose
            End If
    
    
            ' Open the Clipboard to copy data to.
           If OpenClipboard(0&) = 0 Then
                MsgBox "Could not open the Clipboard. Copy aborted."
                Exit Sub
            End If
    
    
            ' Clear the Clipboard.
           x = EmptyClipboard()
    
    
            ' Copy the data to the Clipboard.
           hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)
    
    
    PrepareToClose:
    
    
            If CloseClipboard() = 0 Then
                MsgBox "Could not close Clipboard."
            End If
    End Sub

  3. #3
    Josha is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2019
    Location
    Victoria, Australia
    Posts
    42
    I meant to reply to this message years ago! It did in fact work

    Thank you for your help

  4. #4
    sxschech is offline Novice
    Windows 10 Office 365
    Join Date
    Dec 2021
    Posts
    5
    Here is another version having a few less lines of code along with a paste function too.

    Found here: https://bytecomb.com/copy-and-paste-in-vba/

    Code:
    Const DATAOBJECT_BINDING As String = "new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}"
    
    Public Sub CopytoClip(ByVal Expression As String)
        With CreateObject(DATAOBJECT_BINDING)
            .SetText Expression
            .PutInClipboard
        End With
    End Sub
    
    
    Public Function PasteFromClip() As String
        With CreateObject(DATAOBJECT_BINDING)
            .GetFromClipboard
            PasteFromClip = .GetText
        End With
    End Function
    In code on form example:
    CopyToClip (Me.txtNewNumber)

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 14
    Last Post: 09-18-2017, 11:14 AM
  2. create a unique text string
    By Jen0dorf in forum Access
    Replies: 4
    Last Post: 05-14-2016, 09:07 AM
  3. Replies: 6
    Last Post: 04-14-2015, 02:34 PM
  4. Letter Database: How to manage predefined text in a report.
    By lostblue7w in forum Database Design
    Replies: 3
    Last Post: 02-11-2013, 11:42 AM
  5. VBA to update to predefined text where is null
    By webisti in forum Programming
    Replies: 1
    Last Post: 12-05-2012, 02:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums