qq会员刷钻教程:excel中如何通过VBA打开word文件和ppt文件?

来源:百度文库 编辑:中财网 时间:2024/04/28 00:18:07
 
一、打开word文件代码:


Set wo = CreateObject("Word.Application")
wo.Documents.Open ThisWorkbook.Path & "\
流程.doc"
wo.Visible = True


二、打开ppt文件代码:

方法1


Set wo = CreateObject("Powerpoint.Application")
wo.Visible = True
wo.Presentations.Open ThisWorkbook.Path & filename


方法2


Sub dd()
    Dim filepath$, filename$
    filepath = Chr(34) & ThisWorkbook.Path & filename & Chr(34)
    Shell "POWERPNT.EXE " & filepath
End Sub


  附:双击打开PPS文件,在演示完后退出PPS时并没有PowerPoint主窗口保留,但在Excel中使用VBA打开的PPS文件,在演示完PPS退出后,PowerPoint主窗口仍然打开。

  这里使用一个循环判断演示窗口是否存在,加上错误捕捉程序来处理上面这个问题。

 
Private Sub CommandButton1_Click()
    Dim wo As Object
    Dim app As Object
 
    '
创建PowerPoint应用实例
    Set app = CreateObject("Powerpoint.Application")
    '
使PowerPoint可见
    app.Visible = True
    '
打开PPS文件
    Set wo = app.Presentations.Open(ThisWorkbook.Path  &  "\a.pps")
 
    '
PPS演示结束时,wo对象的SlideShowWindow不存在,捕捉到错误
    On Error GoTo errHandle
    ' PPS
演示时全屏
    Do While wo.SlideShowWindow.isFullScreen
        DoEvents
    Loop
errHandle:
    '
退出PowerPoint
    app.Quit
    Set app = Nothing
    Set wo = Nothing
End
Sub