![]() 'The correct version for (at least for Windows 7 / 64-bit is this: ![]() 'Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long 'NOTE: the lstrCpy declaration you get from the VB6 API Viewer is WRONG. Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long Private Declare Function GetClipBoardData Lib "user32" Alias "GetClipboardData" (ByVal wFormat As Long) As Long 'Public Declare Function GetClipboardData Lib "user32" Alias "GetClipboardDataA" (ByVal wFormat As Long) As Long 'Note that we do not use the GetClipboardDataA declaration Private Declare Function RegisterClipboardFormat Lib "user32" Alias "RegisterClipboardFormatA" (ByVal lpString As String) As Long Private Declare Function GetClipboardFormatName Lib "user32" Alias "GetClipboardFormatNameA" (ByVal wFormat As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long Private Declare Function EnumClipboardFormats Lib "user32" (ByVal wFormat As Long) As Long Private Declare Function Empt圜lipboard Lib "user32" () As Long Private Declare Function CloseClipboard Lib "user32" () As Long Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long In your VBA project, insert a new class called vbaClipboard, and paste in the following: Option Explicit I have built on the work of several others whose names I unfortunately did not record. The Test macros in the following code block illustrate how to use the vbaClipboard object. "Rich Text Format" is correct for Word 20, and probably all earlier versions. It is important to use the same format name that Word uses when it manipulates the clipboard. SetClipboardText(sMyRTFTextString, "Rich Text Format"). SetClipboardText("Hello, world!", "CF_TEXT") For Rich Text Format, use For built-in formats, use the CF_WHATEVER contsant name. SetClipboardText(ByVal aText As String, ByVal aClipboardFormatName As String) - Stuffs aText into the clipboard using the specified clipboard format name. If you aren't sure what formats are available, use theĬlipboardFormatsAvailable property to find out. Returns the clipboard contents for the specified format number. GetClipboardText(ByVal aClipboardFormatNumber As Long) As String (for built-in clipboard formats) and the custom format number for custom formats that have been registered (by whatever put the data on the clipboard). This tells you what formats are currently on the clipboard, returning both the CF_WHATEVER value VbaClipboard.ClipboardFormatsAvailable - returns a collection of ClipBoardFormat objects (each of which has a. I wrote a VBA class called vbaClipboard with a simple interface: The DataObject is incapable of doing this - and VBA does not provide native support to the Windows clipboard. So the requirement is to preserve the clipboard contents before using the clipboard within the macro, then restore the clipboard contents afterwards. The clipboard, the user will discover, after running your macro, that a precious piece of text the user had copied to the clipboard earlier on has vanished. This copies the range into the Windows clipboard, clobbering whatever was there before. For example, your macro may do a Range.Copy. A macro that mangles the user's clipboard is rude. I have spent dozens of hours Googling this, and although there are some partial examples that sort of work (sometimes), I was unable to find what I really needed. ![]() I have been struggling with clipboard operations from Word VBA.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |