Macro to Copy WordBasic String to Clipboard

ID: Q95969

The information in this article applies to:

SUMMARY

This 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 INFORMATION

NOTE: 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 As
Long) 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 As
Long) 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 Long

Word 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 As
Integer) 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 As
Integer) 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 MAIN
If SetClipBoard("This is a test of the national broadcasting system.") Then EditPaste
End Sub

Function 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 unlocked
handle. GlobalUnlock(hData)

'* Open the clipboard
If(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 = CloseClipboard
Else
     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 2.0c word6 word95 wordnt winword winword2 winapi word7

Version           : 2.x 6.0 6.0a 6.0c 7.0
Platform          : WINDOWS

Last Reviewed: July 30, 1997