WordBasic Macro to Assign Clipboard Text to a String Variable

ID: Q82111

The information in this article applies to:

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.

MORE INFORMATION

Function to Assign Clipboard Text to a String Variable

Declare Function CloseClipboard Lib "user32"() As Long
Declare Function GetClipboardData Lib "user32"(wFormat As Long) As Long
Declare Function OpenClipboard Lib "user32"(hwnd As Long) As Long

Declare Function GlobalLock Lib "kernel32"(h As Long) As Long
Declare Sub  GlobalUnlock Lib "kernel32"(h As Long)
Declare Function lstrcpyA Lib "kernel32"(lpszDest$, lpsz2 As Long)As Long
Declare Function lstrlenA Lib "kernel32"(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), "x")
                r = lstrcpyA(Clip$, lpStr)
                Clip$ = LTrim$(RTrim$(Clip$))
                GlobalUnlock(h)
        Else
                Clip$ = "NULL"
        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 "functions" 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 "Do you want to

   keep the changes to Global: xxxx?".

6. From the File menu, choose Save All.

7. Choose the Yes button in response to the prompt "Save global,

   glossary and command changes?".

Information provided in this document is provided "as is" 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:

   ARTICLE-ID: Q158436
   TITLE     : Sample VB Code to Assign Clipboard Text to String Variable

REFERENCES

"Microsoft Word for Windows User's Guide," version 2.0, pages 765, 784-786

"Using WordBasic," by WexTech Systems and Microsoft, pages 173, 186

Kbcategory: kbusage kbmacro KBSubcategory: Additional query words: 2.0 2.0a 6.0 6.0a 6.0c winword2 word7 winword 7.0 word95 word6 word6

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

Last Reviewed: July 30, 1997