来自MS Access的PowerPoint自动化....查询图表?

时间:2009-05-17 12:48:04

标签: excel ms-access automation powerpoint powerpoint-vba

嘿伙计们!我想知道是否有人可以帮助我...

我有一个Access数据库,用于跟踪我工作的指标和“数字紧缩”,用于构建ppt演示文稿的数据。我通常每个月大约需要40 ppt,而且它们是98%的排行榜。

现在,我一次运行一个查询(使用SQL语句),获取结果数据并将其复制并粘贴到excel模板中(我只是在这个“模板”中创建了一个模拟表,这样图表已经是构建并格式化),然后将图表作为图片复制到ppt模板中。

所以有很多手工作品,只要我在一个月的时间内完成这些操作,就不会那么糟糕。

所以.....我怎么能首先在Access中使用VBA在同一个数据集/表上运行多个查询(我必须按季度,按月,按地区,按州,按网站进行销售。 。并且所有这些都是Top5聚合,因此图表的原因),然后将结果数据发送到特定的excel工作簿,同时定义什么进入什么单元格范围???

如果我将所有数据都输入excel,并准备好图表,那么是否有一些VBA将从excel(activeworksheet)获取图表并将它们作为四边形视图布局中的图片粘贴到powerpoint中?

我可以使用Access to PowerPoint方法做同样的事情,并一起切出excel吗?

我充其量只是新手!非常感谢任何和所有的帮助,提示,建议!

3 个答案:

答案 0 :(得分:4)

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

如果你在一个组中生成图表,即你在一个组中设置一个图形,那么就会产生一个“问题” - 所以当你运行报告时,你会得到很多图形。

抓住每个图并将它们放入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

答案 1 :(得分:1)

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

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

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

答案 2 :(得分:0)

你要问的是很多工作:

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