自动执行Powerpoint宏 [英] Automate Powerpoint Macro

查看:237
本文介绍了自动执行Powerpoint宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a PowerPoint Presentation that gets filled with pictures by a VBA script attached to it. I want to automatically open the Presentation and run the macro. Here's what I have tried:

  • 将脚本转换为加载项,如此处所示:我运行时单击激活它,但从未打开过Powerpoint.
  • 下载了一个预先构建的加载项,并将其称为脚本sub_auto_open().这行得通,但是因为它是一个启用宏的文件(?),但是我必须打开powerpoint并在打开文件之前启用add in,所以它并没有比仅运行宏自动得多
  • 通过MatLab运行PowerPoint.我使用了以下在此处找到的命令,它们确实打开了powerpoint,并且我感兴趣的文件.

  • turning the script into an add-in as shown here: it ran when I clicked activated it, but never when I simply opened powerpoint.
  • downloaded a pre-built add in, and called my script sub auto_open(). This worked, but because it is a macro enabled file(?), but I have to open powerpoint up and enable the add in before opening the file, so it's not much more automatic than just running the macro
  • Running PowerPoint through MatLab. I used the following commands that I found here, which do open powerpoint, and the file I am interested in.

  • g = actxserver('PowerPoint.Application');
  • Presentation = invoke(g.Presentations,'Open','\\path\Automatic_NEdN_Template2.pptm')
  • a = invoke(Presentation.Application,'Run','Auto_Open',[])
  • 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未填充.

With small test cases, it even seemed to work--I could call a vba function that read data from a file and it would return the data to matlab, but when I tried to call the one that makes pictures, it returned NaN and the PowerPoint was not filled.

理想情况下,我想双击某些内容,然后打开PowerPoint,然后运行我的脚本.

Ideally, I'd like to double click on something, then have PowerPoint open, and run my script.

如果有帮助,这里有一些代码片段:

Here are some code snippets if they'll help:

    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

推荐答案

想到这两种方法:

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

  1. An Add-in that has an application-level event handler. Then, you could leverage the application class PresentationOpen event, compare the filename against the known file which you want to operate on, and then run the macro only when the right file has been opened. See this MSDN link for some information about creating an application-level event handler class.

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

Use the Ribbon XML framework to call a procedure from the ribbon's OnLoad event.

第二种方法本质上是独立的,而前一种方法将需要至少一个附加的外接程序文件来控制该过程.

The second method would be essentially self-contained whereas the previous method would require at least one additional Add-in file to control the process.

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

The second method would use VBA that looks something like:

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编辑器插入它,也可以这样做,但这可能是最简单的):

And you would need the ribbon XML (use the CustomUI editor to insert this, it can be done otherwise but this is probably the simplest):

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

此方法的工作方式是:打开文件时,将调用RibbonOnLoad过程,然后该过程将调用执行您的宏代码的过程.请注意,未处理的运行时错误可能会导致功能区对象变量丢失在大型应用程序中可能会造成灾难性的后果,但是在您的特定用例中,这应该不是问题,因为从磁盘重新打开文件总是会始终重新加载功能区.

The way this works is that when the file is opened, the RibbonOnLoad procedure is called, and that procedure then calls the procedure which executes your macro code. Note that unhandled run-time errors may cause the loss of the ribbon object variable which can be disastrous in larger applications, but in your specific use-case this shouldn't be a problem as re-opening the file from disk will always reload the ribbon anyways.

此链接还提供了有关功能区定制基础知识的更多信息,但是出于您的目的,我认为上面的代码是您所需要的.

This link also has more info about the basics of ribbon customization, but for your purposes, I think the above code is all you should need.

这篇关于自动执行Powerpoint宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆