MaxF
MaxF

Reputation: 55

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!

Upvotes: 0

Views: 219

Answers (1)

FaneDuru
FaneDuru

Reputation: 42256

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

Upvotes: 2

Related Questions