Microsoft KB Archive/82111

= WordBasic Macro to Assign Clipboard Text to a String Variable =

Article ID: 82111

Article Last Modified on 11/16/2006

-

APPLIES TO


 * Microsoft Word 2.0 Standard Edition
 * Microsoft Word 2.0a
 * Microsoft Office Word 2003
 * Microsoft Word 2.0c
 * Microsoft Word 6.0 Standard Edition
 * Microsoft Word 6.0a
 * Microsoft Word 6.0c
 * Microsoft Word 95 Standard Edition
 * Microsoft Word 95a

-



This article was previously published under Q82111



SUMMARY
This function retrieves any text residing on the Clipboard. All formatting is ignored, and the Clipboard content is stored in the variable clipboard$. As with all WordBasic strings, the maximum string size is 32,768 characters or what local memory allows.



Function to Assign Clipboard Text to a String Variable
Declare Function CloseClipboard Lib &quot;user32&quot; As Long Declare Function GetClipboardData Lib &quot;user32&quot;(wFormat As Long) As Long Declare Function OpenClipboard Lib &quot;user32&quot;(hwnd As Long) As Long

Declare Function GlobalLock Lib &quot;kernel32&quot;(h As Long) As Long Declare Sub GlobalUnlock Lib &quot;kernel32&quot;(h As Long) Declare Function lstrcpyA Lib &quot;kernel32&quot;(lpszDest$, lpsz2 As Long)As Long Declare Function lstrlenA Lib &quot;kernel32&quot;(lpsz As Long) As Long ' ********************************************************************** Sub Main

hwnd = GetFocus If (OpenClipboard(hwnd) <> 0) Then h = GetClipboardData(1) If (h <> 0) Then lpStr = GlobalLock(h) Clip$ = String$(lstrlenA(lpStr), &quot;x&quot;) r = lstrcpyA(Clip$, lpStr) Clip$ = LTrim$(RTrim$(Clip$)) GlobalUnlock(h) Else Clip$ = &quot;NULL&quot; End If       r = CloseClipboard MsgBox Clip$ End Sub This function can be called from within any subroutine in a macro. It can also reside in a &quot;functions&quot; macro. You can then call this function from within another WordBasic macro. The syntax is as follows:

Within Same Macro
Sub MAIN MsgBox Clipboard$ End Sub

Within Another Macro
Sub MAIN MsgBox Global.Clipboard$ End Sub Use the following steps to create the macro:


 * 1) From the Tools menu, choose Macro.
 * 2) Type a name for the macro in the Macro Name box, and choose the Edit button.
 * 3) In the macro-editing window, type the above macro text.
 * 4) Press CTRL+F4 to close the macro-editing window.
 * 5) Choose the Yes button in response to the prompt &quot;Do you want to keep the changes to Global: xxxx?&quot;.
 * 6) From the File menu, choose Save All.
 * 7) Choose the Yes button in response to the prompt &quot;Save global, glossary and command changes?&quot;.

Information provided in this document is provided &quot;as is&quot; without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

For information about how to do this in Word 97, please see the following article(s) in the Microsoft Knowledge Base:

158436 Sample VB Code to Assign Clipboard Text to String Variable

