【问题标题】:Automate Powerpoint Macro自动化 Powerpoint 宏
【发布时间】:2015-12-04 18:39:50
【问题描述】:

我有一个 PowerPoint 演示文稿,该演示文稿通过附加的 VBA 脚本填充了图片。我想自动打开演示文稿并运行宏。这是我尝试过的:

  • 将脚本转换为加载项,如图所示here:当我单击激活它时它会运行,但当我只是打开 powerpoint 时它就不会运行。
  • 下载了一个预建插件,并调用了我的脚本子 auto_open()。这行得通,但因为它是一个启用宏的文件(?),但我必须在打开文件之前打开 powerpoint 并启用插件,所以它并不比只运行宏更自动化
  • 通过 MatLab 运行 PowerPoint。我使用了我找到的here 的以下命令,这些命令可以打开 powerpoint 和我感兴趣的文件。

    • g = actxserver('PowerPoint.Application');
    • Presentation = invoke(g.Presentations,'Open','\\path\Automatic_NEdN_Template2.pptm')
    • a = invoke(Presentation.Application,'Run','Auto_Open',[])

    对于小型测试用例,它甚至似乎可以工作——我可以调用一个从文件中读取数据的 vba 函数,它会将数据返回给 matlab,但是当我尝试调用制作图片的那个时,它返回了NaN 且 PowerPoint 未填充。

理想情况下,我想双击某个东西,然后打开 PowerPoint,然后运行我的脚本。

如果有帮助,这里有一些代码 sn-ps:

    Function read_in_data_from_txt_file(strFileName As String) As String()

    Dim dataArray() As String
    Dim i As Integer

    'Const strFileName As String = "C:\H5_Samples\Plots\WeeklyPlots\zz_avgTemp.txt"
    Open strFileName For Input As #1

     ' -------- read from txt file to dataArrayay -------- '

     i = 0
     Do Until EOF(1)
        ReDim Preserve dataArray(i)
        Line Input #1, dataArray(i)
        i = i + 1
     Loop
     Close #1

    read_in_data_from_txt_file = dataArray
    End Function

这是图片的代码:

   Function Auto_Open() As String  

   Dim oSlide As Slide
   Dim oPicture As Shape
   Dim oText As Variant
   Dim heightScaleFactor As Single
   Dim widthScaleFactor As Single
   Dim width As Single
   heightScaleFactor = 2.1
   widthScaleFactor = 2.1
   width = 205
   Height = 360

   ActiveWindow.View.GotoSlide 2

    Set oSlide = ActiveWindow.Presentation.Slides(2)

    Set oPicture = oSlide.Shapes.AddPicture("C:\H5_Samples\Plots\WeeklyPlots\Nominal DS Real spectra.png", _
        msoFalse, msoTrue, 1, 150, Height, width)


    Set oPicture = oSlide.Shapes.AddPicture("C:\H5_Samples\Plots\WeeklyPlots\Nominal DS Imaginary spectra.png", _
        msoFalse, msoTrue, 350, 150, Height, width)

    'Full Resolution
    ActiveWindow.View.GotoSlide 3

    Set oSlide = ActiveWindow.Presentation.Slides(3)

    Set oPicture = oSlide.Shapes.AddPicture("C:\H5_Samples\Plots\WeeklyPlots\Full Resolution DS Real spectra.png", _
        msoFalse, msoTrue, 1, 150, Height, width)


    Set oPicture = oSlide.Shapes.AddPicture("C:\H5_Samples\Plots\WeeklyPlots\Full Resolution DS Imaginary spectra.png", _
        msoFalse, msoTrue, 350, 150, Height, width)
    End Function

【问题讨论】:

  • 试试 www.pptxbuilder.com

标签: vba powerpoint office-addins


【解决方案1】:

想到了两种方法:

  1. 具有 应用程序级 事件处理程序的加载项。然后,您可以利用应用程序类PresentationOpen 事件,将文件名与您要操作的已知文件进行比较,然后仅在打开正确的文件时运行宏。有关创建应用程序级事件处理程序类的一些信息,请参阅this MSDN link

  2. 使用功能区 XML 框架从功能区的 OnLoad 事件调用过程。

第二种方法基本上是独立的,而前一种方法需要至少一个额外的插件文件来控制该过程。

第二种方法将使用类似于以下内容的 VBA:

Option Explicit
Public Rib As IRibbonUI

'Callback for customUI.onLoad
Sub RibbonOnLoad(ribbon As IRibbonUI)
    Set Rib = ribbon
    Call Auto_Open()  '### I would recommend changing your procedure name just to avoid confusion...
End Sub
Sub Auto_Open()
    '### This is your procedure/macro that you want to run
    ' etc
    ' etc

End Sub

您需要功能区 XML(使用 CustomUI editor 插入它,否则可以这样做,但这可能是最简单的):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
   <customUI onLoad="RibbonOnLoad" xmlns="http://schemas.microsoft.com/office/2009/07/customui">
   </customUI>

它的工作方式是,当文件打开时,RibbonOnLoad 过程被调用,然后该过程调用执行宏代码的过程。请注意,unhandled run-time errors may cause the loss of the ribbon object variable 在较大的应用程序中可能是灾难性的,但在您的特定用例中,这应该不是问题,因为从磁盘重新打开文件总是会重新加载功能区。

This link 还提供有关功能区自定义基础知识的更多信息,但出于您的目的,我认为上述代码就是您所需要的。

【讨论】:

    猜你喜欢
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    相关资源
    最近更新 更多