1

I am trying to open a Power Bi desktop ".pbix" file from VBA if the file is not opened already. All I am looking for is a way to launch power bi desktop

Shell("notepad.exe " & myDirectory, vbNormalFocus)

Can someone please provide me a string which can sub 'notepad.exe' with something which works for power bi I tried "Powerbi" and "PowerbiDesktop" with and without spacings but nothing works for me. Any help will be greatly appreciated. A

QHarr
  • 83,427
  • 12
  • 54
  • 101
Py_junior
  • 93
  • 1
  • 10

1 Answers1

1

All I am looking for is a way to launch power bi desktop

Find the application and right click to get the target address. Use that target address and query winmgmts to ensure process not already running.

enter image description here

Option Explicit

Public Sub test()

    If GetObject("winmgmts:").ExecQuery("select * from win32_process where name='PBIDesktop.exe'").Count = 0 Then
        Shell "C:\Program Files\Microsoft Power BI Desktop RS\bin\PBIDesktop.exe", vbNormalFocus
    End If

End Sub


I am trying to open a Power Bi desktop ".pbix" file from VBA if the file is not opened already.

For specific PBI file:

Option Explicit

Public Sub test()

    Dim fileName As String

    fileName = "C:\Users\<User>\Desktop\NEL VTE Template SQL.pbix"

    If Not IsFileOpen(fileName) Then
        Shell "C:\Program Files\Microsoft Power BI Desktop RS\bin\PBIDesktop.exe" & " " & """" & fileName & """"
    End If

End Sub

'Adapted by me from https://exceloffthegrid.com/vba-find-file-already-open/
Public Function IsFileOpen(ByVal fileName As String) As Boolean

    Dim fileNum As Long
    Dim errNum As Long

    'Allow all errors to happen
    On Error Resume Next
    fileNum = FreeFile()

    'Try to open and close the file for input.
    'Errors mean the file is already open
    Open fileName For Input Lock Read As #fileNum
    Close fileNum

    'Get the error number
    errNum = Err

    'Do not allow errors to happen
    On Error GoTo 0

    'Check the Error Number
    Select Case errNum

        'errNum = 0 means no errors, therefore file closed
    Case 0
        IsFileOpen = False

        'errNum = 70 means the file is already open
    Case 70
        IsFileOpen = True

        'Something else went wrong
    Case Else
        IsFileOpen = errNum

    End Select

End Function

Ref:

  1. Determine if application is running with Excel
  2. https://stackoverflow.com/a/39903527/6241235 @Tony Emrud
QHarr
  • 83,427
  • 12
  • 54
  • 101