简体   繁体   English

通过 excel VBA 打开 PowerPoint

[英]Open PowerPoint through excel VBA

I want to open my PowerPoint file through excel VBA by only giving the extension of the file (. pptx).我想通过 excel VBA 打开我的 PowerPoint 文件,只提供文件的扩展名(.pptx)。 I have seen some codes but they require full name of the file to be given.我看过一些代码,但它们需要提供文件的全名。 Is it possible to do this?是否有可能做到这一点? I am only keeping one PowerPoint file in my folder.我的文件夹中只保留一个 PowerPoint 文件。

You can do something like this:你可以这样做:

Sub Open_PPT()

    Dim PPTpath As String
    Dim PPTname As String

    Dim ThisExtension As String
    Dim temp As Variant

    temp = Split(ThisWorkbook.Name, ".")
    ThisExtension = temp(UBound(temp))

    PPTpath = Replace(ThisWorkbook.FullName, ThisExtension, "pptx")
    PPTname = Replace(ThisWorkbook.Name, ThisExtension, "pptx")

    Dim PPT As Object, PPPres As Object
    Set PPT = CreateObject("PowerPoint.Application")
    PPT.Visible = True

    PPT.Presentations.Open FileName:=PPTpath
    Set PPPres = PPT.Presentations(PPTname)
    'open PPT

    PPPres.Slides(1).Select
End Sub

I take the workbook's Name and split on "."我取工作簿的名称并拆分为“。” and find the last element of the generated array - likely "xlsx".并找到生成数组的最后一个元素 - 可能是“xlsx”。 Then replace this with "pptx" so that we now have the full path like:然后将其替换为“pptx”,以便我们现在拥有完整的路径,例如:

"C:\Users\name\Documents\Excel_name.pptx" "C:\Users\name\Documents\Excel_name.pptx"

and the file name like "Excel_name.pptx" then we can create a PPT Application Object and open the file (I am assuming it exists, otherwise you need to open a new blank PPT and save it accordingly)和“Excel_name.pptx”这样的文件名然后我们可以创建一个PPT应用程序Object并打开文件(我假设它存在,否则你需要打开一个新的空白PPT并相应地保存)

If your question is more like "can I find a.pptx file with any name in the same folder as the current folder (where the Excel is saved)" Then you are looking for something like:如果您的问题更像是“我可以在与当前文件夹(保存 Excel 的位置)相同的文件夹中找到具有任何名称的 .pptx 文件”然后您正在寻找类似的内容:

Sub Find_and_open_PPT()

    On Error Resume Next

    Dim FSO As Object, fld As Object
    Dim fileExtn As String
    Dim PPTpath As String
    Dim PPTname As String

    fileExtn = ".pptx"

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set fsoFolder = FSO.GetFolder(ThisWorkbook.Path)

    For Each fsoFile In fsoFolder.Files 'check the files in the parent folder
        If Err.Number > 0 Then
            'MsgBox "error handling file, likely due to permission"
            Err.Clear
        End If
        If Right(fsoFile, Len(fileExtn)) = fileExtn Then
            'PPT found
            PPTpath = fsoFile
            PPTname = fsoFile.Name
            Exit For
        End If
    Next        

    Dim PPT As Object, PPPres As Object
    Set PPT = CreateObject("PowerPoint.Application")
    PPT.Visible = True

    PPT.Presentations.Open FileName:=PPTpath
    Set PPPres = PPT.Presentations(PPTname)
    'open PPT

    PPPres.Slides(1).Select        

End Sub

You could also modify this to find exactly the file name you are looking for or even a unix file match something like:您还可以修改它以准确找到您正在寻找的文件名,甚至 unix 文件匹配如下:

if fsoFile.Name like "example*.ppt*" then

Currently, the code stop looking once you find a file with the extension ".pptx":目前,一旦您找到扩展名为“.pptx”的文件,代码就会停止查找:

If Right(fsoFile, Len(fileExtn)) = fileExtn Then

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM