VBA Powerpoint - 打开时自动运行并在后台运行 [英] VBA Powerpoint - Auto run on open and run in the background

查看:196
本文介绍了VBA Powerpoint - 打开时自动运行并在后台运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 PowerPoint 演示文稿,该演示文稿将显示在工作场所受伤后的天数.

I'm trying to create a powerpoint presentation which will show the number of days since an injury in the workplace.

当用户第一次打开演示文稿时,我希望运行一个宏,提示输入自上次受伤以来的日期.到目前为止,我有这个似乎工作正常:-

When the presentation is first opened by the user i'd like a macro to run that prompts for a date to be entered since the last injury. So far for that i have this which appears to work ok:-

Sub EveryDayAccidents()
Dim injdate As String
Dim lastdate As String
Dim injfree As Integer
Dim BnrMsg As String

'This Macro defines the latest injury date

injdate = InputBox("Please enter last injury date in this format:  dd/mm/yyyy")
lastdate = injdate
injfree = DateDiff("d", injdate, Now)
BnrMsg = injfree
ActivePresentation.Slides(3).Shapes("Accidents").TextFrame.TextRange = BnrMsg
End Sub

我缺少的是一些代码或另一个在打开演示文稿时调用此代码的子代码.

What i'm missing is some code or another sub that will call this code when the presentation is opened.

文本框会在日期自然变化时更新还是需要在后台运行某些内容来更新文本框?计划是让演示文稿中的幻灯片循环运行,直到发生意外,然后重置并重新开始.

Will the text box then update when the date changes naturally or will something need to be running in the background to update the text box? The plan is to leave the slides in the presentation running in a loop until an accident occurs then it would be reset and start again.

任何帮助将不胜感激!

编辑

所以现在我有了这个:-

So now i have this:-

Sub OnSlideShowPageChange(ByVal SSW As SlideShowWindow)
 If SSW.View.CurrentShowPosition = 3 Then

     injdate = ActivePresentation.Slides(3).Shapes("Accidents").TextFrame.TextRange
     injfree = DateDiff("d", injdate, Now)
     BnrMsg = injfree
     ActivePresentation.Slides(3).Shapes("Accidents").TextFrame.TextRange = BnrMsg

 End If
End Sub

当演示文稿运行时,它会更新幻灯片......但它将文本框中的数字视为实际日期(65 变成 05/03/1900),这意味着我的日期差异在41,600...我想做的是暂时完全忽略日期.

Which does update the slide when the presentation is running... But it is treating the number in the text box as an actual date (65 turns into 05/03/1900) which means my date difference is in the region of 41,600... What i'd like to do is ignore the dates completely for a moment.

如果我在文本框中输入一个数字(比如 1),那么我希望该数字每天递增 1,我认为这段代码目前无论如何都会这样做,但我缺乏转换的技能:-

If i input a number (say 1) into the text box i would then want that number to increment by 1 each day, i think this code will do that anyway at the moment but i'm lacking the skills to convert :-

injfree = DateDiff("d", injdate, Now)

进入

injfree = injfree + 1 when date changes (garbage i know)

请帮忙:)

推荐答案

Soooooo!!!首先非常感谢@David Zemens 和@Steve Ringsberg!

Soooooo!!! Many thanks to @David Zemens and @Steve Ringsberg firstly!

我已经设法想出一个可能在未来对其他人有所帮助的解决方案,所以这是最终结果.这样做的好处是不需要插件或其他任何东西,结果 anwser 最终非常简单......

I've managed to come up with a solution which might help someone else in the future so here is the final result. The up side of this is that no addins were required or anything else and it turns out the anwser is quite simple in the end...

对于我想在每张幻灯片上执行此操作,我在单独的模块中有以下代码以保持清晰,唯一的区别是变量、幻灯片编号和文本框名称.

For each slide that i wanted to do this on i have the following code in seperate modules to keep things clear, the only differences are the variables, slide numbers and Text Box names.

Sub OnSlideShowPageChange(ByVal SSW As SlideShowWindow)
 If SSW.View.CurrentShowPosition = 2 Then

     actdate = ActivePresentation.Slides(2).Shapes("Last Prod").TextFrame.TextRange
     injfree = DateDiff("d", actdate, Now)
     BnrMsg = injfree
     ActivePresentation.Slides(2).Shapes("Activity").TextFrame.TextRange = BnrMsg

 End If
 End Sub

这段代码的作用是,当演示文稿的当前位置到达幻灯片 2,3 等时,它将运行附加到该幻灯片的代码.在这里,我使用了一个实际上不在幻灯片上的小文本框并输入了开始日期.然后代码将该日期命名为actdate",然后找出该日期与当前日期之间的差异,然后更新第二个文本框在幻灯片上显示差异值.

What this code is doing is that as the presentation runs when the current position of the presentation reaches slide 2,3 etc it will then run the code attached to that slide. Here i used a small text box that isn't actually on the slide and put a starting date in. The code then names that date as 'actdate' then finds the difference between that date and the current date, then updates the second text box on the slide to show the difference value.

因此,如果Last Prod"(文本框)= 01/01/2014 并且当前日期是 02/01/2014,则Activity"(文本框)= 1

So if 'Last Prod' (textbox) = 01/01/2014 and the current date was 02/01/2014 then 'Activity' (textbox) = 1

真的很简单:)

这篇关于VBA Powerpoint - 打开时自动运行并在后台运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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