vba 让 Excel 窗口回到前台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23536277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:02:57  来源:igfitidea点击:

Get Excel Window back to Foreground

excelexcel-vbaoutlookoffice-2007vba

提问by user3615191

I searched the Internet from top to the root, but I couldn't find a solution. In my Excel Workbook I want to import Contacts from Outlook. This works fine without any problems. I get the GAL to the foreground by using Outlook.ActiveWindow.Activateand I minimize Outlook by using Outlook.Explorers.Item(1).WindowState = olMinimized. I know this is not really a very clean solution, but everything works fine unless there are no other Outlook Windows opened.

我从头到尾在互联网上搜索,但找不到解决方案。在我的 Excel 工作簿中,我想从 Outlook 导入联系人。这工作正常,没有任何问题。我通过使用将 GAL 置于前台,Outlook.ActiveWindow.Activate并使用Outlook.Explorers.Item(1).WindowState = olMinimized. 我知道这不是一个非常干净的解决方案,但除非没有打开其他 Outlook Windows,否则一切正常。

Now to my problem:
When the user has more than one opened Outlook Window (e.g. a new mail dialog, etc.) I can't bring my Excel Application back to Foreground. The import of contacts still works fine. The GAL comes to Foreground by clicking a Button.
I tried the following code snippets, but they don't really work.

现在我的问题是:
当用户打开多个 Outlook 窗口(例如新邮件对话框等)时,我无法将我的 Excel 应用程序带回前台。联系人的导入仍然正常。GAL 通过单击按钮进入前台。
我尝试了以下代码片段,但它们并没有真正起作用。

   Dim AppTitle As String
   AppTitle = Application.Caption
   '
   'some code
   '
   AppActivate AppTitle

Another attempt was this function in a module

另一个尝试是模块中的这个函数

   Public Declare Function SetForegroundWindow Lib "user32" _
   (ByVal hWnd As Long) As Long

calling it in my Button Function

在我的按钮功能中调用它

   Dim AppXL As Object
   Set AppXL = CreateObject("Excel.Application")
   '
   'some code
   '
   SetForegroundWindow AppXL.hWnd

I also tried to use this code snippet

我也尝试使用此代码片段

   Excel.Application.Visible = True
   Excel.Application.ActiveWindow.Activate

Nothing helped. The only thing happened was the blinking Excel Icon in the taskbar. I hope you can help me out with this.

没有任何帮助。唯一发生的事情是任务栏中闪烁的 Excel 图标。我希望你能帮我解决这个问题。

回答by Rory

Assuming this code is in your Excel workbook, it would just be:

假设此代码在您的 Excel 工作簿中,它将是:

   SetForegroundWindow Application.hWnd

rather than creating a new application instance.

而不是创建一个新的应用程序实例。