使用 VBA 创建电子邮件后,将注意力集中回 Excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11436313/
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-08 13:30:17  来源:igfitidea点击:

Focus back to Excel once email has been created with VBA

excelemailvba

提问by user1135218

I have a VBA macro which does the following:

我有一个 VBA 宏,它执行以下操作:

  1. Creates a report, saves it to main directory. Closes the report
  2. Creates various emails, attaches the report to each (doesn't send the email)
  3. Msgbox shows up showing that process is finished
  1. 创建报告,将其保存到主目录。关闭报告
  2. 创建各种电子邮件,将报告附加到每个电子邮件(不发送电子邮件)
  3. Msgbox 出现显示进程已完成

The thing is that, once the whole process is finished, I want the user to know it is done, but this code doesn't return the focus to excel (which I hoped would be the problem)

问题是,一旦整个过程完成,我希望用户知道它已经完成,但是这段代码不会将焦点返回到 excel(我希望这就是问题所在)

Windows(the_current_window).Activate
Worksheets(currentQuoteSheet).Select

The last email created is what stays on screen. The msgbox doesn't come up, only if I click on excel icon on the menu bar it does. So users are there just waiting to finish (when it has, in fact :( )

创建的最后一封电子邮件是留在屏幕上的内容。msgbox 不会出现,只有当我点击菜单栏上的 excel 图标时才会出现。所以用户在那里只是等待完成(当它完成时,事实上:()

Any ideas how to make sure the msgbox shows up after last email is created without users having to click on excel?

任何想法如何确保在创建最后一封电子邮件后显示 msgbox 而无需用户单击 excel?

回答by Sorceri

Try

尝试

sub doSomething()
   'do something
   AppActivate "Microsoft Excel"
End Sub

回答by Adam Law

Although this works to a degree, it doesn't activate the MSGbox to allow one to press y/n on the keyboard (you can with the mouse).

尽管这在一定程度上起作用,但它不会激活 MSGbox 以允许在键盘上按 y/n(您可以使用鼠标)。

This allows keyboard interaction if you are doing the task repetitively and don't want to grab your mouse. Tested Office 2013 (64bit) (remove PtrSafe if you are 32bit)

如果您重复执行任务并且不想抓住鼠标,这允许键盘交互。经过测试的 Office 2013(64 位)(如果您是 32 位,请删除 PtrSafe)

Private Declare PtrSafe Function MessageBox _
        Lib "User32" Alias "MessageBoxA" _
           (ByVal hWnd As Long, _
            ByVal lpText As String, _
            ByVal lpCaption As String, _
            ByVal wType As Long) _
        As Long
Sub Test
        'Call showwindow(ActiveWorkbook.Name)
        AppActivate "Excel"
        'I need this to retain the focus
        retval = MessageBox(&O0, "Do you want to process", "My Question", vbYesNo + vbSystemModal)
End Sub

回答by BruceWayne

I am on MS Office 365 and for some reason I get an Application Error when trying AppActivate "Microsoft Excel"and AppActivate "Excel", however:

我对MS Office 365和出于某种原因想,当我得到一个应用程序错误AppActivate "Microsoft Excel",并AppActivate "Excel"然而,:

AppActivate Application.Caption

Worked for me.

对我来说有效。

回答by l85m

Have you tried calling your workbook by name? Something like:

您是否尝试过按名称调用您的工作簿?就像是:

Workbooks("EXCEL WORKBOOK NAME.XLS").Activate