自动化VBScript更新Bloomberg Excel公式的问题求助
我在网上找遍了这个棘手问题的答案都没找到,只能发帖求助……大家好像都觉得这问题难,但我相信能彻底解决。
我们共用一台Bloomberg Terminal,需要结合Bloomberg Excel Add-in更新带Bloomberg公式的Excel表格数据。为了避免每天手动操作导致终端拥堵,我想用VBScript配合Windows任务计划程序实现自动化:每天早上8:45终端运行脚本,打开Excel表格,运行宏刷新Bloomberg公式,再用另一个宏发邮件通知收件人覆盖公司的最新新闻、业绩及待发布业绩情况。
已知VBScript不会自动初始化Excel插件,所以我在代码里加了检查,遍历Application.Addins集合验证Installed和IsOpen属性是否为True,也试过在VBScript和VBA里设置插件为已安装并打开,通过debug.print确认脚本运行时所有插件都启用了,和手动开Excel时状态一致。
但现在的问题是:Bloomberg公式总是在宏停止后才开始刷新。我试过加等待机制、把刷新拆成独立子程序,都没用。
我猜测原因要么是Bloomberg有阻止VBScript运行的限制(可能性大),要么是我漏了某些细节(也很可能)。
求现成的实现脚本,或者任何解决思路,万分感谢。
注:我知道可能有更好的实现方式,但我决心搞清楚问题本质,至少给自己和后来人留个参考。抱歉代码不够规范,我还比较陌生……
现有VBScript代码
'Input excel full file path ExcelFilePath = [*Actual path-file removed*] 'Input module/macro name within the Excel File MacroPath1 = "Module1.Refresh_Workbook" 'Create an instance of Excel Set ExcelApp = CreateObject("Excel.Application") 'Do you want this Excel instance to be visible? ExcelApp.Visible = True 'Prevent any app launch alerts ExcelApp.DisplayAlerts = True 'Open Excel File Set wb = ExcelApp.Workbooks.Open(ExcelFilePath) 'Execute Macro Code ExcelApp.Run MacroPath1 'Save Excel File wb.Save 'Reset Display Alerts Before Closing ExcelApp.DisplayAlerts = True 'Close Excel File wb.close 'End instance of Excel ExcelApp.Quit 'Leaves an onscreen message MsgBox "Your Automated Task sucessfully ran at " & TimeValue(Now), vbInformation
工作簿中的VBA宏 - Refresh_Workbook
Public Sub Refresh_Workbook() 'Checking to see what addIns are installed and status Dim add_ins As Variant For Each add_ins In Application.AddIns Debug.Print "Name = " & add_ins.FullName Debug.Print "Is Installed = " & add_ins.Installed Debug.Print "Is Open = " & add_ins.IsOpen Next add_ins Application.AddIns(4).Installed = True 'Using reference to refer to BBG add-in as two BloombergUI addins exist... .xla and xlam making hard to reference Application.Run "RefreshAllWorkbooks" 'Bloomberg Add-in function to refresh Debug.Print "Refreshed BBG Code. Waiting ... " Application.Wait (Now + TimeValue("00:00:15")) 'Calling email generator Call Auto_Email Debug.Print "Done" End Sub
可能的解决思路
改用Bloomberg自带的刷新等待机制:不要用固定时长的
Application.Wait,改用Bloomberg提供的刷新状态检查函数,循环等待直到刷新完成:Application.Run "RefreshAllWorkbooks" ' 循环等待刷新结束 Do While Application.Run("IsRefreshing") = True DoEvents ' 释放系统资源,避免假死 Application.Wait Now + TimeValue("00:00:01") ' 每秒检查一次 Loop也可以尝试调用带等待参数的刷新命令:
Application.Run("RefreshAllWorkbooks", True)(部分版本的Bloomberg插件支持此参数)精准定位Bloomberg插件:不要用索引
Application.AddIns(4)来加载插件,避免因插件顺序变化导致错误,改用插件名称定位:' 找到BloombergUI.xlam插件并启用 Dim bbgAddin As AddIn Set bbgAddin = Application.AddIns("BloombergUI") If Not bbgAddin Is Nothing Then bbgAddin.Installed = True ' 确保插件已加载 Do Until bbgAddin.IsOpen DoEvents Application.Wait Now + TimeValue("00:00:01") Loop End If调整任务计划程序配置:
- 确保任务在有Bloomberg终端权限的用户账户下运行
- 设置任务为「不管用户是否登录都运行」,并勾选「使用最高权限运行」
- 在任务的「触发器」中添加延迟启动(比如延迟10秒),确保Bloomberg终端服务完全初始化
VBScript中延迟启动Excel:在创建Excel实例前加入延迟,等待Bloomberg服务就绪:
' 等待10秒,确保Bloomberg终端服务初始化完成 WScript.Sleep 10000 Set ExcelApp = CreateObject("Excel.Application")避免Excel实例提前关闭:确保在刷新完全完成后再执行保存、关闭操作,不要依赖固定等待时长。
内容的提问来源于stack exchange,提问作者Vaxcel

