3

我有一个 Access 数据库来跟踪指标和“数字计算”数据来构建 PowerPoint 演示文稿。我每个月做大约 40 次演示,它们是 98% 的图表。

我一次运行一个查询(使用 SQL 语句),获取结果数据并将其复制到 Excel 模板中(我在这个“模板”中制作了一个模拟表,因此图表已经构建并格式化),然后将图表复制为将图片转换为 PowerPoint 模板。

所以有很多手工工作。

如何在同一个数据集/表上使用 VBA 在 Access 中运行多个查询(我必须按季度、按月、按地区、按州、按站点进行销售,所有这些都是 Top5 聚合,因此图表的原因),然后将生成的数据发送到特定的 Excel 工作簿,同时定义什么进入哪个单元格范围?

如果我将所有数据输入 Excel,并准备好图表,是否有一些 VBA 可以从 Excel(活动工作表)中获取图表并将它们作为四视图布局中的图片粘贴到 PowerPoint 中?

我可以用 Access to PowerPoint 方法做同样的事情并剪掉 Excel 吗?

我充其量是个新手。

4

3 回答 3

4

您根本不需要使用 Excel!在报告中使用 MS Access Charts 和一些 VBA 代码将它们直接放入 Powerpoint。这里已经有一个例子

一个“陷阱”是如果您在组中生成图表,即您使用组内的图表设计报告 - 因此当您运行报告时,您将创建大量图表。

获取这些图表中的每一个并将它们放入 Powerpoint 有点棘手,但这里有一些代码可以处理它。这适用于 Access 2003

'Loop through all the controls in this report and pickout all the graphs
For Each c In pReport.Controls

    'Graphs initially appear to be in an Object Frame
    If TypeOf c Is ObjectFrame Then

        'Check the Class of the object to make sure its a Chart
        If Left$(c.Class, 13) = "MSGraph.Chart" Then

            'Check if this graph must be cloned (required if the graph is in a group in the MS Access report)
            If Not IsGraphToBeCloned(pReport.Name, c.ControlName) Then

                InsertGraphToPptSlide c, "", pReport.Name
            Else
                InsertGraphGroupToPpt pReport.Name, c
            End If
        End If
    End If
Next

这将找到报告中的所有图表,如果图表在一个组中,那么我们调用 InsertGraphGroupToPPt 函数。

这里的诀窍是我们知道我们多次拥有相同的基本图 - 但填充了不同的数据。因此,在 Powerpoint 中,您需要将基础图粘贴到 powerpoint 幻灯片中 n 次 - 其中 n 是组数,然后更新图查询属性

例如

Function UpdateGraphInPowerpoint(sql As String, OrigGraph As ObjectFrame, Groups As dao.Recordset, GroupName As String, ReportName As String) As Boolean


    //Copyright Innova Associates Ltd, 2009
    On Error GoTo ERR_CGFF
    On Error GoTo ERR_CGFF

    Dim oDataSheet As DataSheet
    Dim Graph As Graph.Chart
    Dim lRowCnt, lColCnt, lValue As Long, CGFF_FldCnt As Integer
    Dim CGFF_Rs As dao.Recordset
    Dim CGFF_field As dao.Field
    Dim CGFF_PwrPntloaded As Boolean
    Dim lheight, lwidth, LLeft, lTop As Single
    Dim slidenum As Integer
    Dim GraphSQL As String
    Dim lGrpPos As Long

    'Loop thru groups
    Do While Not Groups.EOF

        'We want content to be added to the end of the presentation - so find out how many slides we already have
        slidenum = gPwrPntPres.Slides.Count
        OrigGraph.Action = acOLECopy            'Copy to clipboard
        slidenum = slidenum + 1                 'Increment the Ppt slide number
        gPwrPntPres.Slides.Add slidenum, ppLayoutTitleOnly   'Add a Ppt slide

        'On Error Resume Next    'Ignore errors related to Graph caption
        gPwrPntPres.Slides(slidenum).Shapes(1).TextFrame.TextRange.Text = ReportName & vbCrLf & "(" & Groups.Fields(0).Value & ")" 'Set slide title to match graph title
        gPwrPntPres.Slides(slidenum).Shapes(1).TextFrame.TextRange.Font.Size = 16

        gPwrPntPres.Slides(slidenum).Shapes.Paste  'Paste graph into ppt from clipboard

        Set Graph = gPwrPntPres.Slides(slidenum).Shapes(2).OLEFormat.Object

        Set oDataSheet = Graph.Application.DataSheet    ' Set the reference to the datasheet collection.
        oDataSheet.Cells.Clear                          ' Clear the datasheet.

        GraphSQL = Replace(sql, "<%WHERE%>", " where " & GroupName & " = '" & Groups.Fields(0).Value & "'")
        Set CGFF_Rs = ExecQuery(GraphSQL)


        CGFF_FldCnt = 1
        ' Loop through the fields collection and get the field names.
        For Each CGFF_field In CGFF_Rs.Fields
            oDataSheet.Cells(1, CGFF_FldCnt).Value = CGFF_Rs.Fields(CGFF_FldCnt - 1).Name
           CGFF_FldCnt = CGFF_FldCnt + 1
        Next CGFF_field

        lRowCnt = 2
        ' Loop through the recordset.
        Do While Not CGFF_Rs.EOF

            CGFF_FldCnt = 1
            ' Put the values for the fields in the datasheet.
            For Each CGFF_field In CGFF_Rs.Fields
               oDataSheet.Cells(lRowCnt, CGFF_FldCnt).Value = IIf(IsNull(CGFF_field.Value), "", CGFF_field.Value)
               CGFF_FldCnt = CGFF_FldCnt + 1

            Next CGFF_field

            lRowCnt = lRowCnt + 1
            CGFF_Rs.MoveNext
        Loop

        ' Update the graph.
        Graph.Application.Update

        DoEvents

        CGFF_Rs.Close
        DoEvents
        Groups.MoveNext
    Loop

    UpdateGraphInPowerpoint = True
    Exit Function


End Function
于 2009-07-29T18:59:51.040 回答
1

由于您是新手,也许您应该将任务分解为多个部分并一次一个地自动化这些部分。每一步都会带来好处(即节省时间),您可以边做边学。

由于缺乏具体信息(什么版本等),很难做出具体的建议。话虽如此,第一步可能是将 Excel 表链接到访问查询,以便电子表格可以每月自动更新,您不必将数据从 Access 剪切和粘贴到 Excel。您可以完全在 Excel 中执行此链接。

如果您使用 Excel 2007,请单击功能区中的“数据”,然后单击“来自访问”。

于 2009-05-17T14:56:47.083 回答
0

你要问的是很多工作:

通过 VBA,您必须打开 Excel(Access 中的 Excel 应用程序操作),更新您的图表(范围操作、数据更新),如果您有权限,那么我建议您将数据透视图连接到 Access 数据而不是粘贴到工作簿,但我已经在足够多的情况下这是不可能的。然后,您必须打开 PowerPoint 演示文稿并从 Excel 复制到 PowerPoint。我已经完成了所有这些工作,并且知道通过创建一个宏(通过 VBA)可以节省多少工作。这是很多代码。

于 2009-05-18T17:12:52.563 回答