Macro to Copy WordBasic String to ClipboardLast reviewed: July 30, 1997Article ID: Q95969 |
The information in this article applies to:
SUMMARYThis article contains a Word for Windows macro that copies a WordBasic string to the Windows Clipboard. WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.
MORE INFORMATIONNOTE: Below are the required API calls that must be made to Windows. Make sure you use the list appropriate to your Word version or your system may hang or become unusable. Word for Windows NT, Word 7.0 for Windows 95 Declare statements (32-bit Word)
Declare Function GetFocus Lib "user32"() As Long Declare Function OpenClipboard Lib "user32"(hwnd As Long) As Long Declare Function CloseClipboard Lib "user32"() As Long Declare Function EmptyClipboard Lib "user32" As Long Declare Function SetClipboardData Lib "user32"(wFormat As Long, hData AsLong) As Long Declare Function CopyStrtoLp Lib "Kernel32" Alias "lstrcpy"(lpDest As Long,lpScr As String) As Long Declare Function GlobalAlloc Lib "Kernel32"(wFlags As Long, dwBytes AsLong) As Long Declare Function GlobalLock Lib "Kernel32"(hL As Long) As Long Declare Sub GlobalUnlock Lib "Kernel32"(hU As Long) Declare Function GlobalFree Lib "Kernel32"(hF As Long) As LongWord 2.0x, Word 6.0x for Windows 3.1x Declare Statements (16-bit Word)
Declare Function GetFocus Lib "user"() As Integer Declare Function OpenClipboard Lib "user"(hwnd As Integer) As Integer Declare Function CloseClipboard Lib "user"() As Integer Declare Function SetClipBoardData Lib "user"(uFormat As Integer, hData AsInteger) As Integer Declare Function EmptyClipboard Lib "user" As Integer Declare Function CopyStrtoLp Lib "kernel"(lpDest As Long, lpScr As String)As Long Alias "lstrcpy" Declare Function GlobalAlloc Lib "Kernel"(wFlags As Long, dwBytes AsInteger) As Integer Declare Function GlobalLock Lib "kernel"(h As Integer) As Long Declare Sub GlobalUnlock Lib "kernel"(h As Integer) Declare Function GlobalFree Lib "kernel"(h As Integer) As Integer Example
Sub MAINIf SetClipBoard("This is a test of the national broadcasting system.") Then EditPaste End SubFunction SetClipBoard(szText$) '* Get the handle to the active Window.hwnd = GetFocus
'* Add some NULL terminating characters to the String.szText$ = szText$ + Chr$(0) + Chr$(0)
'* Allocate some global memory and copy the string to this memory.nSize = Len(szText$) hData = GlobalAlloc(2, nSize) hMem = GlobalLock(hData) hMem = CopyStrtoLp(hMem, szText$)
'* Unlock the memory handle, the clipboard must receive an unlockedhandle. GlobalUnlock(hData)
'* Open the clipboardIf(OpenClipboard(hwnd) <> 0) Then '* Clear current contents, add hData, Close Clipboard. n = EmptyClipboard n = SetClipBoardData(1, hData) '* the clipboard must be closed before any one else can access it. n = CloseClipboardElse n = 0 '* Clipboard failed, free memory n2 = GlobalFree(hData)End If SetClipBoard = n End Function
REFERENCES"Programming Windows 3.1," 3rd edition, by Charles Petzold, Microsoft Press, Copyright 1992, Chapter 16 Kbcategory: kbusage kbmacro KBSubcategory: |
Additional query words: 7.0 6.0 6.0a 6.0c 2.0 2.0a 2.0a-CD 2.0b
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |