简体   繁体   中英

Excel's automating a TFS update using VBA won't work in Workbook_Open function

I am trying to automate a workbook so that when it is opened it will automatically update the TFS data and send an email. I can update the TFS data successfully using a macro (found code on this blog ) however when I run the code from the Workbook_Open event it errors out with "Run-time error '-2147467259 (80004005)' Method 'execute' of object '_CommandBarButton' failed". I looked at How do I resolve a VBA 'Method Execute of a [TFS Excel Add-in CommandBarButton] failed' Run-time error , but that solution doesn't work for me. This actually makes sense to me because the workbook hasn't actually connected to TFS yet - that happens AFTER the Workbook_Open completes.

My question is, is there an event after the TFS Database connection is made that I can call my code from or is there a way to force the TFS connection first? I have tried doing Application.Wait and Sleep, but that just delays the TFS Database connection.

Thanks!

I have tried doing Application.Wait and Sleep, but that just delays the TFS Database connection.

That's because these methods essentially pause the current (one and only) thread; they won't let any other (non-VBA) code run in the meantime.

the workbook hasn't actually connected to TFS yet - that happens AFTER the Workbook_Open completes.

Sounds like you need to defer execution then. Move the CommandButton.Execute code to another procedure, and then use Application.OnTime to schedule the execution of that procedure, say, 5 seconds later - assuming you pulled the offending code to some UpdateTFS procedure:

Application.OnTime DateTime.DateAdd("s", 5, DateTime.Now), "UpdateTFS"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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