excel - 从 Access 将 Excel 窗口置于前台

标签 excel vba ms-access

我正在尝试从 Access 打开一个 Excel 文件,它确实可以工作,但是 Excel 窗口会在后台弹出(在 Access 窗口后面),这对用户不是很友好。这是我使用的代码:

Private Function OpenExcelAttachment()
Dim MyXL As Object
Set MyXL = CreateObject("Excel.Application")
With MyXL

   Dim FullPath As String, Name As String
   Name = "\ExcelFile.xlsx"
   FullPath = CurrentProject.Path & Name
   .Workbooks.Open FullPath
   .Visible = True

 End With

如何使 Excel 窗口出现在前台(在所有打开的窗口之上)?

谢谢!

最佳答案

我会首先检查已经打开的 Excel 实例。如果您必须允许应用程序的多个实例,那么这将更加棘手。如果您可以只使用一个 Excel 实例,那么我认为这应该可以使用 AppActivate陈述。

Private Function OpenExcelAttachment()
Dim MyXL As Object

On Error Resume Next
Set MyXL = GetObject(,"Excel.Application")
If Err.Number <> 0 Then Set MyXL = CreateObject("Excel.Application")
On Error GoTo 0

With MyXL
   Dim FullPath As String, Name As String
   Name = "\ExcelFile.xlsx"
   FullPath = CurrentProject.Path & Name
   .Workbooks.Open FullPath
   .Visible = True
End With

AppActivate "Microsoft Excel"

End Function

关于excel - 从 Access 将 Excel 窗口置于前台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25353453/

相关文章:

excel - 将格式化为日期的范围转换为文本

sql - MS Access 查询未在 Excel 中正确运行

ms-access - 以编程方式 Access "Compact and Repair"

ms-access - 有没有办法覆盖使用表单向导创建的 Access 2007 表单的自动记录更新?

excel - 使用 VBA 从表中复制和粘贴

arrays - 如何在保留其他单元格数据的同时将单元格值拆分为多行?

excel - 无法从 VBA 中的事件单元格中删除字符

excel - 编译错误: Argument not optional vba excel

excel - 将 'x hrs y min z sec' 转换为秒

python - 使用 Python/Pandas 按行获取列内容的值