VBA:从剪贴板读取文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2913373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:40:12  来源:igfitidea点击:

VBA: Read file from clipboard

filevbaclipboard

提问by ReturningTarzan

I'm trying to load a file in a VBA macro that has been copied from, say, an Explorer window.

我正在尝试在从资源管理器窗口复制的 VBA 宏中加载文件。

I can easily get the data from the clipboard using DataObject::GetFromClipboard, but the VBA interface to DataObject doesn't seem to have methods for working with any other formats than plain text. There are only GetText and SetText methods.

我可以使用 DataObject::GetFromClipboard 从剪贴板轻松获取数据,但 DataObject 的 VBA 接口似乎没有处理任何其他格式的方法,而不是纯文本。只有 GetText 和 SetText 方法。

If I can't get a file stream directly from the DataObject, the filename(s) would also do, so maybe GetText could be forced to return the name of a file placed on the clipboard?

如果我不能直接从 DataObject 获取文件流,文件名也可以,所以也许 GetText 可能被迫返回放置在剪贴板上的文件的名称?

There is very little documentation to be found for VBA anywhere. :(

几乎没有任何地方可以找到 VBA 的文档。:(

Maybe someone could point me to an API wrapper class for VBA that has this sort of functionality?

也许有人可以指出我具有这种功能的 VBA 的 API 包装器类?

回答by Alex K.

This works for me (in a module);

这对我有用(在一个模块中);

Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal uFormat As Long) As Long
Private Declare Function OpenClipboard Lib "user32" (ByVal Hwnd As Long) As Long
Private Declare Function GetClipboardData Lib "user32" (ByVal uFormat As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function DragQueryFile Lib "shell32.dll" Alias "DragQueryFileA" (ByVal drop_handle As Long, ByVal UINT As Long, ByVal lpStr As String, ByVal ch As Long) As Long

Private Const CF_HDROP As Long = 15

Public Function GetFiles(ByRef fileCount As Long) As String()
    Dim hDrop As Long, i As Long
    Dim aFiles() As String, sFileName As String * 1024

    fileCount = 0

    If Not CBool(IsClipboardFormatAvailable(CF_HDROP)) Then Exit Function
    If Not CBool(OpenClipboard(0&)) Then Exit Function

    hDrop = GetClipboardData(CF_HDROP)
    If Not CBool(hDrop) Then GoTo done

    fileCount = DragQueryFile(hDrop, -1, vbNullString, 0)

    ReDim aFiles(fileCount - 1)
    For i = 0 To fileCount - 1
        DragQueryFile hDrop, i, sFileName, Len(sFileName)
        aFiles(i) = Left$(sFileName, InStr(sFileName, vbNullChar) - 1)
    Next
    GetFiles = aFiles
done:
    CloseClipboard
End Function

Use:

用:

Sub wibble()
    Dim a() As String, fileCount As Long, i As Long
    a = GetFiles(fileCount)
    If (fileCount = 0) Then
        MsgBox "no files"
    Else
        For i = 0 To fileCount - 1
            MsgBox "found " & a(i)
        Next
    End If
End Sub

回答by Fink

Seems like a strange way to try to get at the textfile. The DataObject class is only for working with text strings to and from the clipboard.

似乎是一种尝试获取文本文件的奇怪方法。DataObject 类仅用于处理与剪贴板之间的文本字符串。

Here is a very good resource of that: http://www.cpearson.com/excel/Clipboard.aspx

这是一个非常好的资源:http: //www.cpearson.com/excel/Clipboard.aspx

If your wanting to get a file stream of a file you can look into the FileSystemObject and TextStream Classes.

如果您想获取文件的文件流,您可以查看 FileSystemObject 和 TextStream 类。

回答by Andrew

Save the files if they are in the clipboard to the destination folder.

将剪贴板中的文件保存到目标文件夹。

Public Declare PtrSafe Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Long) As Long

Public Const CF_HDROP       As Long = 15

        Public Function SaveFilesFromClipboard(DestinationFolder As String) As Boolean
            SaveFilesFromClipboard = False
            If Not CBool(IsClipboardFormatAvailable(CF_HDROP)) Then Exit Function
            CreateObject("Shell.Application").Namespace(CVar(DestinationFolder)).self.InvokeVerb "Paste"
            SaveFilesFromClipboard = True
        End Function