|
本帖最后由 any1009 于 2024-10-25 16:22 编辑
有一个文件aaaaa.xlsm,加入了指定时间内无操作自动弹出对话框,若指定时间内仍无操作,就自动不保存关闭的功能。具体代码如下(设备限定,无法上传附件):
1.模块一:
- Public dingshirr(1 To 1, 1 To 2)
- Declare PtrSafe Function MessageBoxTimeout Lib "user32" Alias "MessageBoxTimeoutA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal uType As Long, ByVal wLanguageId As Long, ByVal dwTimeout As Long) As Long'对话框定时关闭
- Sub jishi()
- Call qxdingshi
- jishiguanbi = TimeValue("00:00:10")
- jishiyanchang = TimeValue("00:01:00")
- dingshirr(1, 1) = Now + jishiguanbi
- dingshirr(1, 2) = dingshirr(1, 1) + jishiyanchang
- Call tjdingshi
- End Sub
- Sub qxdingshi()
- On Error Resume Next ' 防止没有设置定时事件时产生错误
- Application.OnTime dingshirr(1, 1), "closewb", dingshirr(1, 2), False
- On Error GoTo 0 ' 重置错误处理
- End Sub
- Sub tjdingshi()
- Application.OnTime dingshirr(1, 1), "closewb", dingshirr(1, 2), True
- End Sub
- Sub closewb()
- Dim xRet As Long '定义对话框
- autoclosetime = 15000
- miaoshu = autoclosetime / 1000
- '以下是对话框定时关闭
- msgboxstr = "您已长时间未进行任何操作!"
- msgboxstr = msgboxstr & vbCrLf & vbCrLf & "请选择是否继续使用?"
- msgboxstr = msgboxstr & vbCrLf & vbCrLf & "是:可以继续使用 否:自动不保存关闭"
- msgboxstr = msgboxstr & vbCrLf & vbCrLf & "此对话框将在 " & miaoshu & " 秒后自动关闭,aaaaa表也将自动不保存关闭。"
- msgboxstr = msgboxstr & vbCrLf & vbCrLf & "注意:是不保存关闭!!!"
- msgboxstr = msgboxstr & vbCrLf & "注意:是不保存关闭!!!"
- msgboxstr = msgboxstr & vbCrLf & "注意:是不保存关闭!!!"
- xRet = MessageBoxTimeout(0, msgboxstr, "友情提示", vbYesNo + vbInformation, 1, autoclosetime)
- Select Case xRet
- Case 32000
- Debug.Print "超时自动关闭"
- Call qxdingshi
- ThisWorkbook.Close SaveChanges:=False
- Case vbYes
- Debug.Print "选择""是""按钮"
- Call jishi
- Case vbNo
- Debug.Print "选择""否""按钮"
- Call qxdingshi
- ThisWorkbook.Close SaveChanges:=False
- End Select
- '以上是对话框定时关闭
- End Sub
复制代码 2.thisworkbook:
- Private Sub Workbook_BeforeClose(Cancel As Boolean)
- Call qxdingshi
- End Sub
- Private Sub Workbook_Open()
- Call jishi
- End Sub
- Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
- Call jishi
- End Sub
- Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
- Call jishi
- End Sub
复制代码 现在问题是,手动打开关闭aaaaa.xlsm,都没有问题,但是在其他文件内用vba打开aaaaa.xlsm这张表再关闭,ontime仍然会继续运行(即程序打开关闭aaaaa.xlsm后到设定时间还是会再次打开aaaaa.xlsm内ontime指定的程序)
3.其他文件打开代码:
- Sub testtt()
- Set wb1 = Workbooks.Open("D:\aaaaa.xlsm", ReadOnly:=True)
- With wb1.Sheets("Sheet1")
- x = .[A1].Value
- End With
- wb1.Close 0
- Set wb1 = Nothing
- MsgBox x
- End Sub
复制代码
现在我是将aaaaa.xlsm内open的那段删掉,但这样对于aaaaa.xlsm来说,打开无操作功能就不完善了。
求大神解惑,这个该怎么破?
需要文件指定时间无操作自动关闭或对话框定时自动关闭功能的小伙伴,自行复制上面的代码就好,我都做了注释。
谢谢!~
|
评分
-
1
查看全部评分
-
|