Anyone know a handy way in VBA code of getting text from a string variable onto the Windows Clipboard? I want a quick-and-dirty way to concatenate name and address information from the current record in a form, add it to a string variable and then place the value on the Clipboard (so that I can switch to Word and then paste the text). This is not a mail-merge, multi-record situation. I want my users to be able to do this ‘on the fly’ for a single record. There must be a way of doing this. There’s also probably a much more elegant way of doing it!
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Getting text onto clipboard from code (Access 97/2000)
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Getting text onto clipboard from code (Access 97/2000)
- This topic has 6 replies, 6 voices, and was last updated 23 years, 3 months ago.
AuthorTopicWSmgatehouse
AskWoody LoungerJanuary 29, 2002 at 4:36 pm #366140Viewing 2 reply threadsAuthorReplies-
WBell
AskWoody_MVPJanuary 29, 2002 at 11:53 pm #567007The part you can’t really do is to add it to a string variable, as the Clipboard doesn’t know anything about VBA. The approach we usually use is OLE Automation. In that approach, the user has a button on a form that they click and that starts an instance of Word (sometimes with a special template that is a form letter), and we take the data directly from the form and paste it into the Word document, and give focus to the Word document. If you want to pursue it, there is a good description of the process in the Access Developers Handbook by Getz, et al published by Sybex. There are also several knowledge base articles that deal with OLE Automation and driving Word from Access (and vice versa). If you need further references, please post again.
-
WScharlotte
AskWoody Lounger -
WSClausParkhoi
AskWoody LoungerJanuary 30, 2002 at 5:38 pm #567191Couldn’t help it. Had to try to find a simple way of doing a copy of a string to the clipboard without heavy coding.
All there is to it is this:
Combine in another textbox on your form the text you want to copy, select the new textbox and use the RunCommand method to do the job.Now, one would probably not do that. It looks a bit clumsy, does it not?
So, why not put a bit of code in a module to perform the job. The core of it is just a few lines of code:
DoCmd.OpenForm “frmTextToClipBoard”
Forms!frmTextToClipBoard!txtData = strText
DoCmd.RunCommand acCmdCopy
DoCmd.Close acForm, “frmTextToClipBoard”frmTextToClipBoard is a simple form which has just one unbound textbox named txtData. No code.
The attached sample db skows how it can be used to combine the contents of textboxes on a form and copy to the clipboard by calling public function CopyTextToClipBoard(strText).
May be not extremely useful, but fun to do and could be used as a handy way of doing the odd copying of a combination of name, address etc. from Access to Word, Excel or whatever.
-
WSmgatehouse
AskWoody LoungerJanuary 31, 2002 at 1:57 pm #567417Great, Claus. Thanks very much. This is exactly the kind of quick and (slightly) dirty solution I was looking for. My users just want to get one name and address out of Access and into wherever they are currently working in Word (without being tied to a particular template, document, place in a document, etc.). Somehow OLE automation and firing up a new instance of Word seem like massive overkill for this purpose and could present other problems. I suppose, at the end of the day, this is just the kind of thing (simple, user-controlled and very flexible intra- or inter-application data transfer) which the Clipboard was invented to do and which helped to make Windows the flexible animal it is.
Thanks very much to all who contributed so helpfully and patiently to this thread.
-
WSrory
AskWoody LoungerJanuary 31, 2002 at 2:29 pm #567427I found this (or something very similar) in the MSDN library (I think) some time ago, which will copy a string to the clipboard:
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 fSetClipboardString(strInput As String) Dim hGlobalMemory As Long, lpGlobalMemory As Long Dim hClipMemory As Long, lngReturn As Long ' Allocate moveable global memory. '------------------------------------------- hGlobalMemory = GlobalAlloc(GHND, Len(strInput) + 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, strInput) ' Unlock the memory. If GlobalUnlock(hGlobalMemory) 0 Then MsgBox "Could not unlock memory location. Copy aborted." GoTo Graceful_exit 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. lngReturn = EmptyClipboard() ' Copy the data to the Clipboard. hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory) Graceful_exit: If CloseClipboard() = 0 Then MsgBox "Could not close Clipboard." End If End Function
Hope that helps.
-
-
WSSupport4John
AskWoody Lounger
-
Viewing 2 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
How well does your browser block trackers?
by
n0ads
2 hours, 44 minutes ago -
You can’t handle me
by
Susan Bradley
1 hour ago -
Chrome Can Now Change Your Weak Passwords for You
by
Alex5723
1 hour, 18 minutes ago -
Microsoft: Over 394,000 Windows PCs infected by Lumma malware, affects Chrome..
by
Alex5723
10 hours, 18 minutes ago -
Signal vs Microsoft’s Recall ; By Default, Signal Doesn’t Recall
by
Alex5723
10 hours, 30 minutes ago -
Internet Archive : This is where all of The Internet is stored
by
Alex5723
10 hours, 42 minutes ago -
iPhone 7 Plus and the iPhone 8 on Vantage list
by
Alex5723
10 hours, 47 minutes ago -
Lumma malware takedown
by
EyesOnWindows
22 minutes ago -
“kill switches” found in Chinese made power inverters
by
Alex5723
19 hours, 35 minutes ago -
Windows 11 – InControl vs pausing Windows updates
by
Kathy Stevens
19 hours, 29 minutes ago -
Meet Gemini in Chrome
by
Alex5723
23 hours, 34 minutes ago -
DuckDuckGo’s Duck.ai added GPT-4o mini
by
Alex5723
23 hours, 43 minutes ago -
Trump signs Take It Down Act
by
Alex5723
1 day, 7 hours ago -
Do you have a maintenance window?
by
Susan Bradley
34 minutes ago -
Freshly discovered bug in OpenPGP.js undermines whole point of encrypted comms
by
Nibbled To Death By Ducks
9 hours, 54 minutes ago -
Cox Communications and Charter Communications to merge
by
not so anon
1 day, 11 hours ago -
Help with WD usb driver on Windows 11
by
Tex265
1 day, 16 hours ago -
hibernate activation
by
e_belmont
1 day, 19 hours ago -
Red Hat Enterprise Linux 10 with AI assistant
by
Alex5723
1 day, 23 hours ago -
Windows 11 Insider Preview build 26200.5603 released to DEV
by
joep517
2 days, 2 hours ago -
Windows 11 Insider Preview build 26120.4151 (24H2) released to BETA
by
joep517
2 days, 2 hours ago -
Fixing Windows 24H2 failed KB5058411 install
by
Alex5723
22 hours, 55 minutes ago -
Out of band for Windows 10
by
Susan Bradley
2 days, 7 hours ago -
Giving UniGetUi a test run.
by
RetiredGeek
2 days, 14 hours ago -
Windows 11 Insider Preview Build 26100.4188 (24H2) released to Release Preview
by
joep517
2 days, 22 hours ago -
Microsoft is now putting quantum encryption in Windows builds
by
Alex5723
17 hours, 55 minutes ago -
Auto Time Zone Adjustment
by
wadeer
3 days, 2 hours ago -
To download Win 11 Pro 23H2 ISO.
by
Eddieloh
3 days ago -
Manage your browsing experience with Edge
by
Mary Branscombe
11 minutes ago -
Fewer vulnerabilities, larger updates
by
Susan Bradley
2 hours, 25 minutes ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.