简体   繁体   English

加载项工作簿中的Application.OnTime_Open

[英]Application.OnTime in Add-In Workbook_Open

I've built a custom Excel add-in and I'm currently trying to figure out a way to prompt users via VBA when new versions of the add-in are available. 我已经构建了一个自定义Excel加载项,并且当前正在尝试找到一种方法,以便在新版本的加载项可用时通过VBA提示用户。

I tried just using the workbook_open event to check for the latest version and then prompt the user with a userform, but I discovered that when Excel loads an add-in that trigger a userform, Excel stops loading the workbook the user actually tried to open and reloads the add-in. 我尝试仅使用workbook_open事件检查最新版本,然后使用用户窗体提示用户,但是我发现,当Excel加载触发用户窗体的加载项时,Excel停止加载用户实际尝试打开的工作簿,并且重新加载该加载项。 So while the userform works like I wanted, the user gets a blank (read no sheets) Excel shell with a loaded add-in. 因此,当用户窗体按我希望的方式工作时,用户将获得一个空白(不读取工作表)带有加载项的Excel Shell。

So I considered using Application.OnTime to postpone the VBA until after the add-in and target file were both open. 因此,我考虑使用Application.OnTime将VBA推迟到加载项和目标文件都打开之后。 I got the impression both here and here that this is possible, but I am only able to make it work in an .xlsm file and not a .xlam file. 我在这里这里都感到这是可能的,但是我只能使其在.xlsm文件而不是.xlam文件中工作。

Here's the code I'm testing with: 这是我正在测试的代码:

Sub Workbook_Open()
    Application.OnTime Now() + TimeValue("00:00:05"), "Test_Addin.xlam!Versioning.Notify_User"
End Sub

And in a regular code module: 并在常规代码模块中:

Sub Notify_User()
    MsgBox "Hello"
End Sub

So, my question: Am I doing something wrong here? 所以,我的问题是: 我在这里做错了吗?

I'm wondering if there's something about how an add-in is loaded/designed that keeps it from allowing this type of action to be performed. 我想知道是否有关于如何加载/设计加载项的事情,以防止它允许执行此类操作。

Alternatively, is there a different way to do this that you can think of? 或者,您可以想到其他方法吗?

I read an interesting blog post (and comments) by Dick Kusleika on this topic, but it sounded like some people put a version check in each sub procedure... I have a lot of procedures, so this doesn't sound like a good alternative, although I may have to resort to it. 我读了Dick Kusleika撰写的有关该主题的有趣的博客文章 (和评论),但听起来有些人在每个子过程中都进行了版本检查……我有很多过程,所以听起来并不好。另一种选择,尽管我可能不得不求助于它。

Well, time is of the essence so I resorted to the least desirable option: a check at the beginning of each procedure. 好吧,时间至关重要,因此我诉诸最不希望的选择:在每个过程的开始进行检查。

To the interested parties, here's how I did it: 对于感兴趣的各方,这是我的做法:

Somewhere towards the beginning of each sub procedure I put this line of code: 在每个子过程开始的某个地方,我放置了以下代码行:

If Version_Check Then Call Notify_User

And in my Versioning module I put this function and procedure: 在我的Versioning模块中,我输入了以下功能和过程:

Function Version_Check() As Boolean
    Installed = FileDateTime(ThisWorkbook.FullName)
    Available = FileDateTime("\\NetworkDrive\Test_AddIn.xlam")
    If Installed < Available Then Version_Check = True
End Function

Sub Notify_User()
    Update_Check.Show
End Sub

So each time a procedure is run this code checks for a version on our corporate network with a modified datetime greater than the datetime of the installed add-in. 因此,每次运行过程时,此代码都会检查公司网络上的版本,该版本的修改后的日期时间大于已安装的加载项的日期时间。

I'm still very open to alternative ways of checking for new versions so feel free to post an answer. 我仍然非常乐于接受其他检查新版本的方法,请随时发布答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM