简体   繁体   English

链接到xlsm工作簿时错误触发了Workbook_Open事件

[英]Workbook_Open event erroneously triggered when linking to xlsm workbook

I am running a [Edit] read-only (3rd party!) [/Edit] SQL database, an xlsm, an xls (saved copy of the xlsm, just without macros), and an mdb (Access database). 我正在运行[编辑]只读(第三方!)[/ Edit] SQL数据库,xlsm,xls(xlsm的已保存副本,没有宏)和mdb(Access数据库)。

They are linked as follows: 它们的链接如下:

  • The xlsm gets data from the SQL database every 5 minutes by Application.OnTime code, triggered on Workbook_Open. xlsm每隔5分钟通过Application.OnTime代码从SQL数据库获取数据,该代码在Workbook_Open上触发。
  • The xlsm then saves itself as a 2003 xls. 然后,xlsm将自身保存为2003 xls。
  • Since the code modules still exist at that moment (the workbook is not yet closed), I can open the Access mdb and refresh the Excel link to a table in the 2003 data, then close Access. 由于那时代码模块仍然存在(工作簿尚未关闭),因此我可以打开Access mdb并刷新到2003数据中的表的Excel链接,然后关闭Access。

  • I then reopen the xlsm, triggering a new Application.Ontime instance. 然后,我重新打开xlsm,触发一个新的Application.Ontime实例。

  • I finally close the 2003 xls workbook. 我终于关闭了2003 xls工作簿。

This sequence runs during office hours and at the first run after 5pm sets a timer to start the process again in the morning. 此序列在办公时间运行,在下午5点之后的第一次运行中,设置了一个计时器,以在早晨再次启动该过程。

My problem is whenever I open a file with a connection or link to the xlsm, the Workbook_Open event seems to trigger and I end up with the 2003 xls open on the 'client' computers. 我的问题是,每当我打开一个具有xlsm的连接或链接的文件时,Workbook_Open事件似乎都会触发,最后我在“客户端”计算机上打开了2003 xls。 I know it's not just a leftover Application.OnTime on mine (the PC I originally created the files on) because now it's in use and other clients who have never opened the xlsm get the 2003 file randomly popping up when they're using Excel. 我知道这不仅仅是我剩下的Application.OnTime(我最初在上面创建文件的PC),因为现在它正在使用中,而其他从未打开过xlsm的客户端在使用Excel时会随机弹出2003文件。

If that's not enough to trigger someone to know what's going on, I'll happily post code - but I am hoping someone has seen this before or knows of this rookie mistake and can simply give me a line I'm missing. 如果那还不足以触发某人知道发生了什么,我会很高兴地发布代码-但我希望有人以前见过此消息或知道此菜鸟错误,并可以给我一条我缺少的行。

Thanks so much for your help! 非常感谢你的帮助!

I would think the reason the XLS pops up on your clients is because Excel has to open the linked XLSM in order to access the data, and as such, Workbook_Open gets triggered. 我认为XLS在您的客户端上弹出的原因是Excel必须打开链接的XLSM才能访问数据,并因此触发Workbook_Open

A few alternatives to the suggestions you have in the comments; 您可以在注释中提供一些建议;

  • Check Environ("Username") before executing Application.OnTime so that it will only run if the file is opened by specific users. 在执行Application.OnTime之前,请检查Environ("Username") ,以使其仅在特定用户打开文件时才运行。
  • Use a launcher-file to open the XLSM in "admin mode" - you can do this by using the launcher-file to set a Global -scope variable, and then have the XLSM check this variable before running Application.OnTime . 使用启动程序文件以“管理员模式”打开XLSM-您可以通过使用启动程序文件设置Global scope变量来执行此操作,然后让XLSM在运行Application.OnTime之前检查此变量。
  • Make the XLSM test for some dummy-file on the local computer - one that you easily can create by hand on the computer you need regular access to the XLSM on. 对本地计算机上的某些伪文件进行XLSM测试-您可以轻松地在需要定期访问XLSM的计算机上手动创建一个伪文件。 For example the XLSM can look for C:\\Data\\xlsm-enabler.txt or whatever works, and only run the relevant macro if the file exists. 例如,XLSM可以查找C:\\Data\\xlsm-enabler.txt或任何可行的方法,并且仅在文件存在时运行相关的宏。
  • Similar to the above, use SaveSetting to set a registry key on the "admin" machine(s) and check against this inside the XLSM. 与上述类似,使用SaveSetting在“ admin”计算机上设置注册表项,并在XLSM中对此进行检查。
  • Set your XLSM up to accept parameters , and use such parameters to open the file in "admin mode". 将XLSM设置为接受参数 ,并使用这些参数以“管理员模式”打开文件。 This method is outlined several other places @ SO, like here . 这种方法在@ SO的其他几个地方都有概述,例如这里
  • This is more of a hunch - you might be able to leverage the Workbook.ReadOnly property in your XLSM as a conditional. 这更多的是直觉-您可能可以利用XLSM中的Workbook.ReadOnly属性作为条件。 This assumes that Excel opens the XLSM in readonly-mode when accessing it via data connections, which I don't know whether is actually the case. 假设Excel通过数据连接访问XLSM时以只读模式打开XLSM,我不知道是否确实如此。

An easy solution would be to have all other files link to the .xls file, instead of the .xlsm macro enabled file. 一个简单的解决方案是将所有其他文件链接到.xls文件,而不是启用.xlsm宏的文件。 Downside to this approach would be that you have to (manually) update all your references. 这种方法的缺点是您必须(手动)更新所有参考。

It would be way easier to do everything in Access. 在Access中执行所有操作会更容易。 Then if you need to represent the data in Excel you can have a Workbook with a link to the Access table. 然后,如果您需要在Excel中表示数据,则可以有一个带有访问表链接的工作簿。 This removes a step and it uses the right tool for the job. 这消除了一个步骤,并且使用了正确的工具进行作业。 Access is for linking to database tables. 访问用于链接到数据库表。 Excel is for processing data views. Excel用于处理数据视图。

SQL > Access > Excel

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

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