【问题标题】:DragQueryFile always returns 0DragQueryFile 总是返回 0
【发布时间】:2018-02-04 18:33:33
【问题描述】:

我正在尝试在 PowerPoint 中使用 VBA 访问剪贴板中的文件,但 DragQueryFile 始终返回 0。

这里我的代码被剪断了

Private Declare PtrSafe Function IsClipboardFormatAvailable Lib "user32" (ByVal uFormat As Long) As Long
Private Declare PtrSafe Function GetClipboardData Lib "user32" (ByVal uFormat As Long) As Long
Private Declare PtrSafe Function OpenClipboard Lib "user32" ( ByVal hwnd As Long) As Long
Private Declare PtrSafe Function DragQueryFile Lib "shell32.dll" Alias "DragQueryFileA" ( ByVal HDROP As Long, ByVal UINT As Long, ByVal lpStr As String, ByVal ch As Long) As Long
Private Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
Private Const CF_HDROP As Long = 15

Private Sub test_clipboard()
Dim nHandle As Long
If IsClipboardFormatAvailable(CF_HDROP) > 0 Then
    If OpenClipboard(0) <> 0 Then
        nHandle = GetClipboardData(CF_HDROP)
        If nHandle <> 0 Then        
            ' Query number of files in clipboard -> returns always 0
            Debug.Print CStr(DragQueryFile(nHandle, GetNumOfFiles, vbNullString, 0)) 
        End If
        CloseClipboard
    End If
End If

End sub

我使用的是 Windows 10 和 Office 2013。

C# (DragQueryFile returns 0 when I try to get file count?) 也有类似的帖子,但我已经将 OpenClipboard 称为 CloseClipboard。

感谢任何帮助。

【问题讨论】:

  • 这是否有效vba-read-file-from-clipboard
  • 发现问题。它是顶部的声明。刚刚发现我必须用longptr替换long。现在它正在工作。
  • 所以你使用Office x64!这是你应该在一开始就透露的事实:(你知道pros and cons吗?

标签: vba powerpoint


【解决方案1】:

我将它与 tmp_hallenser 的评论结合使用,但认为将其布局得更清楚会更好。

应该改变这两个函数定义:

Private Declare PtrSafe Function GetClipboardData Lib "user32" (ByVal uFormat As Long) As LongPtr
Private Declare PtrSafe Function DragQueryFile Lib "shell32.dll" Alias "DragQueryFileA" ( ByVal HDROP As LongPtr, ByVal UINT As Long, ByVal lpStr As String, ByVal ch As Long) As Long

这是必要的,因为 HDROP 是一个内存地址,因此根据系统的不同大小会有所不同。

类型 LongPtr will always be the appropriate size

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-27
    • 2014-03-20
    • 2013-04-13
    • 2013-03-30
    • 2016-07-13
    • 2023-04-09
    • 2021-10-27
    • 1970-01-01
    相关资源
    最近更新 更多