【问题标题】:open pbix file if not opened in VBA如果未在 VBA 中打开,则打开 pbix 文件
【发布时间】:2020-06-13 20:17:53
【问题描述】:

如果文件尚未打开,我正在尝试从 VBA 打开 Power Bi 桌面“.pbix”文件。我正在寻找的只是一种启动 power bi 桌面的方法

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

谁能给我一个字符串,它可以在'notepad.exe'中包含一些适用于power bi的东西 我尝试了带和不带间距的“Powerbi”和“PowerbiDesktop”,但对我没有用。 任何帮助将不胜感激。 一个

【问题讨论】:

    标签: vba shell powerbi


    【解决方案1】:

    我正在寻找的只是一种启动 power bi desktop

    找到应用,右键获取目标地址。使用该目标地址并查询 winmgmts 以确保进程尚未运行。

    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
    


    如果文件尚未打开,我正在尝试从 VBA 打开 Power Bi 桌面“.pbix”文件。

    对于特定的 PBI 文件:

    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
    

    参考:

    1. Determine if application is running with Excel
    2. https://stackoverflow.com/a/39903527/6241235@Tony Emrud

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-18
      • 1970-01-01
      • 2013-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      相关资源
      最近更新 更多