0

很抱歉提出这个看似简单的问题,但我已经阅读了许多关于如何通过 VBA 打开不可见 Excel 窗口的建议解决方案,但没有一个专门针对从 Access 执行此操作,也没有任何建议的解决方案适用于我的特殊情况。

我正在尝试从 Access 打开一个 Excel 会话,以刷新另一个 (PowerPoint) 文档中的图表引用的一些数据。

这是我正在使用的代码:

'refresh the excel tables that drive the charts
DoCmd.OpenQuery ("qryCreateMemberCountByType")
DoCmd.OpenQuery ("qryCreatePaymentsByType")
Dim AppExcel As Object
Dim wb As Object
Set AppExcel = CreateObject("Excel.Application")
AppExcel.Visible = False 'this doesn't work
AppExcel.DisplayAlerts = False
With AppExcel.Workbooks.Open("C:\MyPath\Template.xlsx")
    '.Windows(1).Visible = False 'this doesn't work
    .RefreshAll
    .Save
    .Close
End With
AppExcel.Quit
Set AppExcel = Nothing

我尝试过的多种方法包括:

AppExcel.Visible = False

.Windows(1).Visible = False

似乎没有什么能阻止 Excel 打开一个可见窗口(尽管我试图通过代码关闭它,但它一直处于打开状态直到手动关闭)。

注意 - 对于 PowerPoint,我能够非常优雅地做到这一点:

With AppPPT.Presentations.Open("C:\MyPath\Template.pptx", WithWindow:=False)

更新:

我发现 Excel 应用程序实际上是在无形地打开;稍后,当我在 PowerPoint 中刷新引用 Excel 中的表格的图表时,它会变得可见:

For Each Slide In .Slides
    For Each shp In Slide.Shapes
        'refresh charts
        If shp.HasChart Then
           shp.Chart.ChartData.Activate 'this line results in a visible Excel session
           shp.Chart.Refresh 'this line also results in a visible Excel session
        End If
    Next shp
Next Slide

关于如何在刷新图表时防止 Excel 变得可见的任何想法都会很棒!

4

1 回答 1

1

整个案子看起来相当有趣。只是为了确保我们在同一步骤上,你可以让你的代码像这个一样小:

Public Sub TestMe()
    Dim appExcel As Object
    Set appExcel = CreateObject("Excel.Application")
    appExcel.Visible = False 'this is a bit useless, as it is the default, but still
End Sub

并检查它是否按预期工作?然后考虑添加额外的行以使其模仿您的原始代码。

于 2018-02-26T17:02:29.710 回答