论文部分内容阅读
摘 要: 由于利用PowerPoint提供的插入控件的方法来插入Flash动画比较复杂,本文利用VBA的强大功能,通过Win32 API调用Windows内置的对话框实现了Flash动画插入操作的自动化,提高了工作效率。
关键词:PowerPoint;VBA;Flash;宏
中图分类号:TP311文献标识码:B 文章编号:1673-8454(2008)20-0056-02
一、问题的提出
在PowerPoint下新建一个幻灯片文件,然后依次选择菜单“工具”→“宏”→“Visual Basic 编辑器”,如图1所示,在“Microsoft Visual Basic”窗口下再单击菜单“插入”→“模块”,在当前工程下添加一个模块,进入VBA编辑环境,将下列VBA代码复制到右侧编辑窗口中。
Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As openfilename) As Long
Type openfilename
lstructsize As Long
hwndowner As Long
hinstance As Long
lpstrfilter As String
lpstrcustomfilter As String
nmaxcustfilter As Long
nfilterindex As Long
lpstrfile As String
nmaxfile As Long
lpstrfiletitle As String
nmaxfiletitle As Long
lpstrinitialdir As String
lpstrtitle As String
flags As Long
nfileoffset As Integer
nfileextension As Integer
lpstrdefext As String
lcustdata As Long
lpfnhook As Long
lptemplatename As String
End Type
Sub InsertFlash()
Dim OpenFile As openfilename
Dim fname As String
OpenFile.lstructsize = Len(OpenFile)
OpenFile.lpstrfilter = "FlashFile(*.swf)|*.swf"
OpenFile.lpstrfile = Space(254)
OpenFile.nmaxfile = 255
OpenFile.lpstrfiletitle = Space(254)
OpenFile.nmaxfiletitle = 255
OpenFile.lpstrinitialdir = "d:\"
OpenFile.lpstrtitle = "Open File"
OpenFile.flags = 0
fname = GetOpenFileName(OpenFile)
If fname >= 1 Then
fname = OpenFile.lpstrfile
fname = Dir(fname, vbNormal)
ActiveWindow.Selection.Slide Range.Shapes.AddOLEObject(_
Left:=200#, Top:=200#, Width:=200#, Height:=200#, _
ClassName:="ShockwaveFlash.Shoc kwaveFlash", Link:=msoFalse).Select
Set swfObject = ActiveWindow.Selection._
SlideRange.Shapes("Shockwave Flash1").OLEFormat.Object
swfObject.Movie = fname
swfObject.Playing = True
Else
MsgBox "Not found swf file,cancel was pressed!"
End If
End Sub
Sub auto_open()
Set newmenu = CommandBars.ActiveMenuBar._
Controls.Add(Type:=msoControl Popup, temporary:=True)
newmenu.Caption = "插入Flash文件"
Set enu = newmenu.Command Bar. Controls.Add(Type:=msoControl Button)
With enu
.Caption = "打开Flash文件"
.Style = msoButtonIcon And Caption
.OnAction = "InsertFlash"
.BeginGroup = True
End With
End Sub
首先运行刚才的宏,单击菜单“工具”→“宏”→“宏”,打开“宏”对话框,如图2所示,选中“宏auto_open”,然后单击“运行”按钮,在当前菜单下自动生成自定义菜单“插入Flash文件”,单击菜单“插入Flash文件”→“打开Flash文件”,打开对话框,浏览文件夹找到需要插入的Flash动画文件,最后单击“打开”即可将所选择的Flash动画文件插入到当前幻灯片中。
四、制作加载宏
关闭VBA编辑器,单击“文件”菜单下的“另存为”按钮,打开“另存为”对话框,将“保存类型”设置为“PowerPoint加载宏(*.ppa)”,然后取名“InsertFlash”保存。
加载宏文件(*.ppa)保存在“C:\Documents and Settings\XXX\App -lication Data\Microsoft\AddIns”文件夹(其中“XXX”为用户名),若需要在其它电脑上插入Flash动画文件,只需将上述加载宏文件复制到其它电脑上与上述文件夹同名的文件夹中,单击“工具”菜单下的“加载宏”打开“加载宏”对话框,单击“添加”按钮,浏览文件夹打开加载宏文件InsertFlash.ppa,如图3所示,加载宏文件会自动添加一个名为“插入Flash文件”的菜单栏,并在该菜单栏下添加一个名为“打开Flash文件”的菜单项。若不用加载宏时可选中加载宏单击“卸载”即可。
在当前幻灯片下插入一个Flash动画文件,单击自定义菜单“插入Flash文件”→“打开Flash文件”,打开一个对话框,浏览文件夹找到所需插入的Flash动画文件,最后单击“打开”,即可将所选择的Flash动画文件插入到当前幻灯片中。
五、注意事项
系统自动插入的ShockwaveFlash控件的大小是固定的,可根据实际情况拖动控件改变大小。
在幻灯片中插入Flash动画前,新建的PowerPoint文件一定要与Flash的动画文件存放在同一个文件夹下面,否则会因为路径问题而影响动画的播放。
若无法加载宏,请单击“工具”→“宏”→“安全性”,将“安全级”设为“低”即可。
有些杀毒软件默认情况下是不允许调用API函数的,如网络版卡巴斯基5.0的“配置实时扫描”中的“宏”选项卡的“对可疑宏的处理动作”中有一项“正在调用API函数”,在默认状态下是“阻止宏命令执行”的,双击此状态,选择“允许宏命令执行”即可取消杀毒软件对“调用API函数”的阻止。
六、结论
由于利用PowerPoint提供的插入控件的方法来插入Flash动画的步骤比较复杂,容易忘记。本文利用VBA的强大功能,通过Win32 API调用Windows内置的对话框实现了插入Flash动画操作的自动化,简化了步骤,提高了工作效率,而且讲解了将宏制作成“加载宏”的具体方法,方便了宏代码的移植使用。
关键词:PowerPoint;VBA;Flash;宏
中图分类号:TP311文献标识码:B 文章编号:1673-8454(2008)20-0056-02
一、问题的提出
在PowerPoint下新建一个幻灯片文件,然后依次选择菜单“工具”→“宏”→“Visual Basic 编辑器”,如图1所示,在“Microsoft Visual Basic”窗口下再单击菜单“插入”→“模块”,在当前工程下添加一个模块,进入VBA编辑环境,将下列VBA代码复制到右侧编辑窗口中。
Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As openfilename) As Long
Type openfilename
lstructsize As Long
hwndowner As Long
hinstance As Long
lpstrfilter As String
lpstrcustomfilter As String
nmaxcustfilter As Long
nfilterindex As Long
lpstrfile As String
nmaxfile As Long
lpstrfiletitle As String
nmaxfiletitle As Long
lpstrinitialdir As String
lpstrtitle As String
flags As Long
nfileoffset As Integer
nfileextension As Integer
lpstrdefext As String
lcustdata As Long
lpfnhook As Long
lptemplatename As String
End Type
Sub InsertFlash()
Dim OpenFile As openfilename
Dim fname As String
OpenFile.lstructsize = Len(OpenFile)
OpenFile.lpstrfilter = "FlashFile(*.swf)|*.swf"
OpenFile.lpstrfile = Space(254)
OpenFile.nmaxfile = 255
OpenFile.lpstrfiletitle = Space(254)
OpenFile.nmaxfiletitle = 255
OpenFile.lpstrinitialdir = "d:\"
OpenFile.lpstrtitle = "Open File"
OpenFile.flags = 0
fname = GetOpenFileName(OpenFile)
If fname >= 1 Then
fname = OpenFile.lpstrfile
fname = Dir(fname, vbNormal)
ActiveWindow.Selection.Slide Range.Shapes.AddOLEObject(_
Left:=200#, Top:=200#, Width:=200#, Height:=200#, _
ClassName:="ShockwaveFlash.Shoc kwaveFlash", Link:=msoFalse).Select
Set swfObject = ActiveWindow.Selection._
SlideRange.Shapes("Shockwave Flash1").OLEFormat.Object
swfObject.Movie = fname
swfObject.Playing = True
Else
MsgBox "Not found swf file,cancel was pressed!"
End If
End Sub
Sub auto_open()
Set newmenu = CommandBars.ActiveMenuBar._
Controls.Add(Type:=msoControl Popup, temporary:=True)
newmenu.Caption = "插入Flash文件"
Set enu = newmenu.Command Bar. Controls.Add(Type:=msoControl Button)
With enu
.Caption = "打开Flash文件"
.Style = msoButtonIcon And Caption
.OnAction = "InsertFlash"
.BeginGroup = True
End With
End Sub
首先运行刚才的宏,单击菜单“工具”→“宏”→“宏”,打开“宏”对话框,如图2所示,选中“宏auto_open”,然后单击“运行”按钮,在当前菜单下自动生成自定义菜单“插入Flash文件”,单击菜单“插入Flash文件”→“打开Flash文件”,打开对话框,浏览文件夹找到需要插入的Flash动画文件,最后单击“打开”即可将所选择的Flash动画文件插入到当前幻灯片中。
四、制作加载宏
关闭VBA编辑器,单击“文件”菜单下的“另存为”按钮,打开“另存为”对话框,将“保存类型”设置为“PowerPoint加载宏(*.ppa)”,然后取名“InsertFlash”保存。
加载宏文件(*.ppa)保存在“C:\Documents and Settings\XXX\App -lication Data\Microsoft\AddIns”文件夹(其中“XXX”为用户名),若需要在其它电脑上插入Flash动画文件,只需将上述加载宏文件复制到其它电脑上与上述文件夹同名的文件夹中,单击“工具”菜单下的“加载宏”打开“加载宏”对话框,单击“添加”按钮,浏览文件夹打开加载宏文件InsertFlash.ppa,如图3所示,加载宏文件会自动添加一个名为“插入Flash文件”的菜单栏,并在该菜单栏下添加一个名为“打开Flash文件”的菜单项。若不用加载宏时可选中加载宏单击“卸载”即可。
在当前幻灯片下插入一个Flash动画文件,单击自定义菜单“插入Flash文件”→“打开Flash文件”,打开一个对话框,浏览文件夹找到所需插入的Flash动画文件,最后单击“打开”,即可将所选择的Flash动画文件插入到当前幻灯片中。
五、注意事项
系统自动插入的ShockwaveFlash控件的大小是固定的,可根据实际情况拖动控件改变大小。
在幻灯片中插入Flash动画前,新建的PowerPoint文件一定要与Flash的动画文件存放在同一个文件夹下面,否则会因为路径问题而影响动画的播放。
若无法加载宏,请单击“工具”→“宏”→“安全性”,将“安全级”设为“低”即可。
有些杀毒软件默认情况下是不允许调用API函数的,如网络版卡巴斯基5.0的“配置实时扫描”中的“宏”选项卡的“对可疑宏的处理动作”中有一项“正在调用API函数”,在默认状态下是“阻止宏命令执行”的,双击此状态,选择“允许宏命令执行”即可取消杀毒软件对“调用API函数”的阻止。
六、结论
由于利用PowerPoint提供的插入控件的方法来插入Flash动画的步骤比较复杂,容易忘记。本文利用VBA的强大功能,通过Win32 API调用Windows内置的对话框实现了插入Flash动画操作的自动化,简化了步骤,提高了工作效率,而且讲解了将宏制作成“加载宏”的具体方法,方便了宏代码的移植使用。