Workbook_Open后焦点错误的Excel窗口

Ron*_*eld 6 excel vba excel-vba excel-2016

我最近升级到Office 365/Excel 2016已导致一些不必要的行为更改.工作簿("Portfolio Appreciation")包含一个Workbook_open过程,该过程检查Workbook("Index Returns")是否打开; 如果不是,它将打开该工作簿.

使用Excel 2007,Index Returns将在后台打开并保持在那里,这是所需的行为.它将在"窗口中",并且可以在同一Excel窗口中使用功能区选项卡Arrange All上的Window选项进行查看View.

使用Excel 2016,如果它通过Workbook_Open过程Index Returns打开,则在其自己的Excel窗口中打开,并在前面结束.(它不能再在同一个Excel窗口中查看Portfolio Appreciation).

Index Returns前面的事实是问题所在.

我尝试选择并取消选择忽略使用DDE的其他应用程序; 我尝试了这个AppActivate方法(如下面的代码所示)并使用a验证MsgBox参数是否与相关的标题栏匹配.

不知道下一步该去哪里.建议表示赞赏.

另外:不 Index Returns包含宏或连接. Portfolio Appreciation除了 Workbook_Open并且确实有一个Web查询在打开时刷新(查询下载一些股票索引的东西).


Option Explicit
Private Sub Workbook_Open()
    Dim wbs As Workbooks, wb As Workbook
    Dim IndexReturns As String
    Dim re As RegExp
    Const sPat As String = "(^.*\\DATA\\).*"
    Const sRepl As String = "$1EHC\Investment Committee\indexreturns.xlsb"
    Dim sTitle As String

sTitle = Application.Caption

Set wbs = Application.Workbooks
Set re = New RegExp
With re
    .Pattern = sPat
    .Global = True
    .IgnoreCase = True
End With
IndexReturns = re.Replace(ThisWorkbook.FullName, sRepl)

For Each wb In wbs
    If wb.FullName = IndexReturns Then Exit Sub
Next wb

Application.ScreenUpdating = False

wbs.Open (IndexReturns)
Set re = Nothing

AppActivate sTitle  'sTitle contains title of thisworkbook

'The below doesn't work either
'AppActivate ThisWorkbook.Application.Caption

Application.ScreenUpdating = True

End Sub
Run Code Online (Sandbox Code Playgroud)

Ron*_*eld 1

当共产国际的代码没有改变行为时,我关注这是否是一个计时问题,直到IndexReturns代码激活另一个工作簿之后才出现活动窗口。对此进行调整的代码似乎已经解决了问题。

IndexReturns我添加了一个循环来在执行该方法之前测试窗口是否存在AppActivate。

Set wb = wbs.Open(IndexReturns)

Do
    DoEvents
Loop Until wb.Windows.Count > 0

AppActivate sTitle
Run Code Online (Sandbox Code Playgroud)

为了更好地衡量,我还使该窗口不可见,因为除了调试目的之外,我不需要访问它:

wb.Windows(1).Visible = False
Run Code Online (Sandbox Code Playgroud)

这似乎解决了Excel 2016与2007相比打开文件不同带来的问题。