简体   繁体   中英

Open a PowerPoint presentation from Excel with VBA without specifying Excel file path

I am looking for a way for VBA in PowerPoint to automatically identify the only open Excel file on my computer and use this Excel file to read data from it. I'd like to avoid having to manually insert Excel file path in my code. Is it possible?

Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("PATH\FILENAME.xlsm")
    xlBook.Application.Visible = False

Thank you!

Try the next code, please:

Sub testOpenWorkbook()
  Dim Ex As Object, wb As Object
   On Error Resume Next
    Set Ex = GetObject(, "Excel.Application")
     If Err.Number <> 0 Then
        Err.Clear: On Error GoTo 0
        MsgBox "There is not any Excel session open...", vbInformation, "Ups...": Exit Sub
     Else
        On Error GoTo 0
        If Ex.Workbooks.count = 1 Then
            Set wb = Ex.Workbooks(1)
        Else
            MsgBox "There are more Excel Workbooks open...", vbInformation, "Ups...": Exit Sub
        End If
     End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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