6

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

使用 Excel 2007,Index Returns将在后台打开并留在那里,这是所需的行为。它将在“窗口中”,并且可以在同一个 Excel 窗口中使用功能区选项卡Arrange All上的选项进行查看。WindowView

对于 Excel 2016,如果它由 Workbook_Open 过程Index Returns打开,则会在其自己的 Excel 窗口中打开,并在前面结束。(不能再在与 相同的 Excel 窗口中查看Portfolio Appreciation)。

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

我尝试选择和取消选择忽略使用 DDE 的其他应用程序的选项;我已经尝试了该AppActivate方法(如下面的代码所示)并使用 验证了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

4

2 回答 2

1

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

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

Set wb = wbs.Open(IndexReturns)

Do
    DoEvents
Loop Until wb.Windows.Count > 0

AppActivate sTitle

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

wb.Windows(1).Visible = False

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

于 2015-12-25T20:59:16.717 回答
0

我显然无法在您的环境中进行测试,但我会尝试绕过 Excel 正在做的任何事情,并使用对BringWindowToTopSetForegroundWindow的调用而不是AppActivate

#If VBA7 Then
    Public Declare PtrSafe Function BringWindowToTop Lib "user32" (ByVal _
             hwnd As LongPtr) As Boolean
    Public Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal _
             hwnd As LongPtr) As Boolean
    Public Declare PtrSafe Function FindWindow Lib "user32" Alias _
            "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName _
            As Any) As LongPtr
#Else
    Public Declare Function BringWindowToTop Lib "user32" (ByVal _
             hwnd As Long) As Boolean
    Public Declare Function SetForegroundWindow Lib "user32" (ByVal _
             hwnd As Long) As Boolean
    Public Declare Function FindWindow Lib "user32" Alias _
            "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName _
            As Any) As Long
#End If

然后...

    Dim hwnd As Long
    hwnd = FindWindow(vbEmpty, sTitle)    'sTitle contains title of thisworkbook
    BringWindowToTop hwnd     
    '...or...
    SetForegroundWindow hwnd
于 2015-12-25T18:01:38.507 回答