Microsoft KB Archive/95969

{|
 * width="100%"|

Macro to Copy WordBasic String to Clipboard

 * }

Q95969

-

The information in this article applies to:


 * Microsoft Word for Windows, versions 2.0, 2.0a, 2.0a-CD, 2.0b, 2.0c, 6.0, 6.0a, 6.0c
 * Microsoft Word for Windows, version 7.0

-

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 &quot;as is&quot; 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 &quot;user32&quot; As Long Declare Function OpenClipboard Lib &quot;user32&quot;(hwnd As Long) As Long Declare Function CloseClipboard Lib &quot;user32&quot; As Long Declare Function EmptyClipboard Lib &quot;user32&quot; As Long Declare Function SetClipboardData Lib &quot;user32&quot;(wFormat As Long, hData As Long) As Long Declare Function CopyStrtoLp Lib &quot;Kernel32&quot; Alias &quot;lstrcpy&quot;(lpDest As Long, lpScr As String) As Long Declare Function GlobalAlloc Lib &quot;Kernel32&quot;(wFlags As Long, dwBytes As Long) As Long Declare Function GlobalLock Lib &quot;Kernel32&quot;(hL As Long) As Long Declare Sub GlobalUnlock Lib &quot;Kernel32&quot;(hU As Long) Declare Function GlobalFree Lib &quot;Kernel32&quot;(hF As Long) As Long

Word 2.0x, Word 6.0x for Windows 3.1x Declare Statements (16-bit Word)
Declare Function GetFocus Lib &quot;user&quot; As Integer Declare Function OpenClipboard Lib &quot;user&quot;(hwnd As Integer) As Integer Declare Function CloseClipboard Lib &quot;user&quot; As Integer Declare Function SetClipBoardData Lib &quot;user&quot;(uFormat As Integer, hData As Integer) As Integer Declare Function EmptyClipboard Lib &quot;user&quot; As Integer Declare Function CopyStrtoLp Lib &quot;kernel&quot;(lpDest As Long, lpScr As String) As Long Alias &quot;lstrcpy&quot; Declare Function GlobalAlloc Lib &quot;Kernel&quot;(wFlags As Long, dwBytes As Integer) As Integer Declare Function GlobalLock Lib &quot;kernel&quot;(h As Integer) As Long Declare Sub GlobalUnlock Lib &quot;kernel&quot;(h As Integer) Declare Function GlobalFree Lib &quot;kernel&quot;(h As Integer) As Integer

Example
Sub MAIN If SetClipBoard(&quot;This is a test of the national broadcasting system.&quot;) 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