0

I have a user interface where the user enters some information. Then they press a button for a report to be generated. The user however cannot have left certain files open while the report is generated. If the user leaves a file open, I want an error message to pop up to inform them of this. And then I want a way for the user to fix the problem (close the file) without having to restart the program as they already entered information.

I have the MsgBox pop up:

Do While IsFileOpen(wfileName)
    MsgBox "Please close the Weights Machine Data Excel File '" & getFileName(wfileName) & "' to proceed", vbInformation, "Error"

Loop

The problem is that the user cannot do any thing once the error message happens. I know that MsgBox is modal, but is there a way to get around that. If not I want the user to return to the point before they pressed the generateReport Button, so they do not have to retype any of the information they inputted. How do I go about solving this?

4

2 回答 2

3

我的回答可能更适合作为评论而不是答案 - 但我还没有足够的声誉......无论如何

为什么不直接IsFileOpen(wfileName)在 generateReport 按钮单击事件中调用。如果是真的,请显示您的消息,然后退出 Sub。

于 2014-08-15T02:48:12.370 回答
1

MessageBox 结果可以被捕获为长整型值。因此,您可以使用它们来捕获用户输入,然后通过调用其他过程或发送到GoTo语句等方式相应地路由您的代码。

Sub Example()
Dim mb as VbMsgBoxResult
ShowAgain:
mb = MsgBox("Do you want to see another message box?", vbYesNo) 

If mb = vbYes Then 
    GoTo ShowAgain
Else:
    'Do something else
End If



End Sub

在您的情况下,您可能会这样做:

Dim mb as vbMsgBoxResult

mb = MsgBox("Please close the Weights Machine Data Excel File '" & getFileName(wfileName) & "' to proceed", vbOkCancel, "Error")

If mb = vbOk Then 
    Workbooks(getFileName(wfileName)).Save
    Workbooks(getFileName(wfileName)).Close
Else:
    'Do something else...

End If

我不完全确定你的意思是什么:

我希望用户在按下 generateReport 按钮之前返回到该点,这样他们就不必重新输入他们输入的任何信息。我该如何解决这个问题?但是除非按钮(以及到目前为止用户“输入”的所有信息)位于 UserForm 上或存储在 Class 对象或其他一些持久性内存中,否则很可能无法轻松完成。

简单地保存/关闭文件就足够了吗?这应该允许程序继续运行,而不会出现文件已经打开时会发生的错误。

于 2014-08-15T02:46:36.400 回答