我最近升级到Office 365 / Excel 2016已导致一些不必要的行为更改。工作簿(" Portfolio Appreciation")包含一个Workbook_open过程,该过程检查工作簿("索引返回")是否打开;如果不是,它将打开该工作簿。
使用Excel 2007,Index Returns
将在后台打开并保留在那里,这是所需的行为。这将是一个窗口"并且可以使用Arrange All
功能区的Window
标签上的View
选项在同一Excel窗口中查看。
使用Excel 2016,如果它被Workbook_Open过程打开,Index Returns
将在其自己的Excel窗口中打开,并在前面结束。 (它不能再在Portfolio Appreciation
)的同一Excel窗口中查看。
Index Returns
在前面的事实是问题所在。
我尝试选择并取消选择忽略使用DDE的其他应用程序;我尝试了AppActivate
方法(如下面的代码所示),并使用MsgBox
验证参数是否与相关标题栏匹配。
不确定下一步该怎么做。建议表示赞赏。
另外:Index Returns
不包含任何宏或连接。 Portfolio Appreciation
不包含除Workbook_Open
以外的任何宏,并且确实有一个网页查询在打开时会刷新(查询会下载一些股票索引)。
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
答案 0 :(得分:1)
当Comintern的代码没有改变行为时,我关注的是这是否是一个时间问题,IndexReturns
在代码激活其他工作簿之前没有活动窗口。为此进行调整的代码似乎解决了这个问题。
在执行IndexReturns
方法之前,我添加了一个循环来测试是否存在AppActivate
的窗口。
Set wb = wbs.Open(IndexReturns)
Do
DoEvents
Loop Until wb.Windows.Count > 0
AppActivate sTitle
为了更好的衡量,我还使该窗口不可见,因为除了调试之外我不需要访问它:
wb.Windows(1).Visible = False
与2007年相比,这似乎解决了Excel 2016打开文件所带来的问题。
答案 1 :(得分:0)
我显然无法在您的环境中进行测试,但我会尝试绕过Excel正在执行的操作并使用BringWindowToTop或SetForegroundWindow而不是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