我為 Excel 創建了拖放表單,以便使用樹視圖控制元件(代碼如下)捕獲檔案位置的鏈接。它按預期作業,但是問題是在我制作表單后ShowModal = False(因為用戶可能想要移動 Excel 視窗以到達要拖動的檔案),在它運行它的例行程式后,會彈出錯誤訊息,通知“檔案格式是無效”(下面的螢屏)或通知檔案可能已損壞或不安全(下面的第二個螢屏)。


據我了解,這是因為 Excel 認為檔案被拖放到作業表上并嘗試打開它(它很可能是 .pdf 檔案)。
除了制作表格之外,有沒有辦法防止這種情況Modal?據我了解,應該以某種方式阻止錯誤訊息,或者 Excel 根本不應該嘗試打開檔案,并且這樣做可以完全避免訊息(最佳情況)。
拖放功能的代碼:
Private Sub TreeView1_OLEDragDrop(Data As MSComctlLib.DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single)
'for capturing draged file path
'VBA does not have normal native functionality to do that so it is solved by using treeview widget unconventionaly
Dim LinkToPass As String
LinkToPass = Data.Files(1)
MsgBox "Thank you! Link captured.", vbInformation, "Link captured"
'Pass information to another form, where user enters all other data required
If formLoaded("NewEntry_agreement") Then
NewEntry_agreement.LinkToFile.Caption = LinkToPass
End If
CloseBtt_Click 'just call close button Sub with Unload Me inside
End Sub
編輯:關于替代訊息的附加資訊和螢屏截圖。還使目標更加明確 - 阻止訊息或阻止 Excel 嘗試打開檔案,并通過這樣做阻止錯誤訊息。
uj5u.com熱心網友回復:
單擊表單以切換模態/非模態
' Adapted from Stephen Bullen's ModelessForm.xls 1998 example
Private Declare PtrSafe Function EnableWindow Lib "user32" (ByVal hwnd As LongPtr, ByVal fEnable As Long) As Long
' click the form to toggle modal/modeless
Private Sub UserForm_Click()
Static lMode As Long
lMode = IIf(lMode = 0, 1, 0)
EnableWindow Application.hwnd, lMode
Me.Caption = IIf(lMode, "Modeless", "Modal")
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
' ensure the app window is reset (maybe trap the state and reset if/as necessary)
EnableWindow Application.hwnd, 1
End Sub
uj5u.com熱心網友回復:
我理解您的描述,但我認為沒有簡單的解決方案。如果這行得通,最簡單的方法是 -
Call DragAcceptFiles(FindWindow("ThunderDFrame", Me.Caption), 0)
.. 但不幸的是它沒有。嘗試以相同的方式禁用 xlApp.Hwnd 也不會接受丟棄的檔案。也許需要禁用其他一堆視窗中的一個(?),我只嘗試過我提到的那些。
簡而言之,您可能會研究兩種不同的方法 -
添加一個按鈕供用戶在啟用拖動檔案操作之前切換無模式/模式。我手頭沒有代碼,但它絕對有可能,盡管不受支持。
而不是 treeview 的 OLE-DD 設定回呼與 CallWindowProc 以捕獲 WM_DROPFILES 訊息,使用 DragQueryFile 獲取檔案,并防止 Excel 使用 DragFinish 接收訊息。您需要一個視窗,這可能是表單的第一個也是唯一一個直接子視窗。最好添加一個視窗控制元件,例如 Frame(盡管它不直接公開它的“hwnd”,因此需要大量的 API 作業才能獲得它)。那里有很多一般示例,我已經采用了這種方法 - 但不幸的是有幾個問題,我沒有任何足夠可靠的東西我想發布!
這不會是您正在尋找的答案,但它可能是您將要得到的最好的答案!雖然我很樂意犯錯:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/398228.html
