gpt4 book ai didi

vba - DragQueryFile 总是返回 0

转载 作者:行者123 更新时间:2023-12-05 07:40:42 25 4
gpt4 key购买 nike

我尝试在 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。

感谢任何帮助。

最佳答案

我根据 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 .

关于vba - DragQueryFile 总是返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45902903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com