使用“ThisWorkbook.Activate”设置焦点并将 ThisWorkbook 的 window 置于最前面,不起作用
Using `ThisWorkbook.Activate` to set focus and bring window of ThisWorkbook to front ,is not working
代码的用途
使用函数检查我桌面上的数字 xls 文件,如果找不到,则将 Thisworkbook
置于最前面。
问题
如果已经打开工作簿,则将 Thisworkbook 带到前面不起作用(仍在后台)并且不会引发代码错误。
问题原因
函数 Get_Highest_Numeric_Name
尽管函数本身工作正常。
我试过的
我试图用
替换 ThisWorkbook.Activate
AppActivate ((ThisWorkbook.Name) & " - Excel")
但是,我在 AppActivate 行上遇到了这个错误
Invalid procedure call or argument
而且奇怪的是,如果我 运行 来自代码 window 的代码,则不会出现之前的错误。
使用 AppActivate (ThisWorkbook.Name…) 的任何方式都不可靠,因为它需要在 windows 系统上启用此选项(文件扩展名)。
感谢任何有用的评论和答案。
Private Sub Workbook_Open()
If Len(Get_Highest_Numeric_Name("D:\Users\Waleed\Desktop\", "*.xls")) = 24 Then 'for Question on Whosebug
MsgBox "File not found", vbCritical + vbMsgBoxSetForeground, "File not found"
ThisWorkbook.Activate
Exit Sub
End If
End Sub
Function Get_Highest_Numeric_Name(strFold As String, Optional strext As String = "*.*") As String
Dim arrD, lastName As String, lngNb As Double, El
'Return all files name in an array
arrD = Split(CreateObject("wscript.shell").Exec("cmd /c dir """ & strFold & strext & """ /b").StdOut.ReadAll, vbCrLf)
If UBound(arrD) = -1 Then MsgBox "Nothing could be found in the path you supplied...": Exit Function
arrD(UBound(arrD)) = "@@##": arrD = Filter(arrD, "@@##", False) 'Remove the last (empty) element
For Each El In arrD 'iterate between the array elements
If IsNumeric(Split(El, ".")(0)) Then
'Compare the lngNb variable (initially 0) with the numeric value:
If lngNb < CDbl(Split(El, ".")(0)) Then
'addapt lngNb like the bigger number
lngNb = CDbl(Split(El, ".")(0)): lastName = El
End If
End If
Next
Get_Highest_Numeric_Name = strFold & lastName 'Build the necessary path
End Function
我绝望地尝试在代码中添加一个附加函数 API 到一个单独的模块中,以生成新的超时消息框。
无论如何,使用这个 API 解决了问题 (我不知道为什么会这样)。
注意:如果我注释 MsgBoxTimeout
行,问题错误再次出现。
Private Sub Workbook_Open()
If Len(Get_Highest_Numeric_Name("D:\Users\Waleed\Desktop\", "*.xls")) = 24 Then
Call MsgBoxTimeout(0, "File not found", "File not found", vbInformation + vbMsgBoxSetForeground, 0, 2000)
Exit Sub
End If
End Sub
'这个函数存在于一个单独的模块中
Public Declare Function MsgBoxTimeout Lib "user32" Alias "MessageBoxTimeoutA" ( _
ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, _
ByVal wType As VbMsgBoxStyle, ByVal wlange As Long, ByVal dwTimeout As Long) As Long
代码的用途
使用函数检查我桌面上的数字 xls 文件,如果找不到,则将 Thisworkbook
置于最前面。
问题
如果已经打开工作簿,则将 Thisworkbook 带到前面不起作用(仍在后台)并且不会引发代码错误。
问题原因
函数 Get_Highest_Numeric_Name
尽管函数本身工作正常。
我试过的
我试图用
AppActivate ((ThisWorkbook.Name) & " - Excel")
但是,我在 AppActivate 行上遇到了这个错误
Invalid procedure call or argument
而且奇怪的是,如果我 运行 来自代码 window 的代码,则不会出现之前的错误。
使用 AppActivate (ThisWorkbook.Name…) 的任何方式都不可靠,因为它需要在 windows 系统上启用此选项(文件扩展名)。
感谢任何有用的评论和答案。
Private Sub Workbook_Open()
If Len(Get_Highest_Numeric_Name("D:\Users\Waleed\Desktop\", "*.xls")) = 24 Then 'for Question on Whosebug
MsgBox "File not found", vbCritical + vbMsgBoxSetForeground, "File not found"
ThisWorkbook.Activate
Exit Sub
End If
End Sub
Function Get_Highest_Numeric_Name(strFold As String, Optional strext As String = "*.*") As String
Dim arrD, lastName As String, lngNb As Double, El
'Return all files name in an array
arrD = Split(CreateObject("wscript.shell").Exec("cmd /c dir """ & strFold & strext & """ /b").StdOut.ReadAll, vbCrLf)
If UBound(arrD) = -1 Then MsgBox "Nothing could be found in the path you supplied...": Exit Function
arrD(UBound(arrD)) = "@@##": arrD = Filter(arrD, "@@##", False) 'Remove the last (empty) element
For Each El In arrD 'iterate between the array elements
If IsNumeric(Split(El, ".")(0)) Then
'Compare the lngNb variable (initially 0) with the numeric value:
If lngNb < CDbl(Split(El, ".")(0)) Then
'addapt lngNb like the bigger number
lngNb = CDbl(Split(El, ".")(0)): lastName = El
End If
End If
Next
Get_Highest_Numeric_Name = strFold & lastName 'Build the necessary path
End Function
我绝望地尝试在代码中添加一个附加函数 API 到一个单独的模块中,以生成新的超时消息框。
无论如何,使用这个 API 解决了问题 (我不知道为什么会这样)。
注意:如果我注释 MsgBoxTimeout
行,问题错误再次出现。
Private Sub Workbook_Open()
If Len(Get_Highest_Numeric_Name("D:\Users\Waleed\Desktop\", "*.xls")) = 24 Then
Call MsgBoxTimeout(0, "File not found", "File not found", vbInformation + vbMsgBoxSetForeground, 0, 2000)
Exit Sub
End If
End Sub
'这个函数存在于一个单独的模块中
Public Declare Function MsgBoxTimeout Lib "user32" Alias "MessageBoxTimeoutA" ( _
ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, _
ByVal wType As VbMsgBoxStyle, ByVal wlange As Long, ByVal dwTimeout As Long) As Long