Open File Without Calling Filepath

后端 未结 5 1364
太阳男子
太阳男子 2021-02-05 15:04

I\'m using excel VBA. I want to press a button that opens another file directly without the effect of \"choosing file window\".

This is the current code:



        
相关标签:
5条回答
  • 2021-02-05 15:38

    If the file is in the same folder as the document containing your VBA macro, use

    ThisWorkbook.Path
    

    for example:

    Workbooks.Open(ThisWorkbook.Path & "\ObsReportExcelWorkbook.xlsx")
    

    (this works even if the document is not the active one any more, or you changed the current directory).

    0 讨论(0)
  • 2021-02-05 15:41

    Open Folder of ActiveWorkBook

    Sub OpenFolderofProject()
    Dim strMsg As String, strTitle As String
    Dim sFolder As String
    
    sFolder = ThisWorkbook.Path
    
        strMsg = "Open WorkBook Folder Location? "
        strTitle = "Folder Location:" & sFolder
    
            If MsgBox(strMsg, vbQuestion + vbYesNo, strTitle) = vbNo Then
            Else
               Call Shell("explorer.exe " & sFolder, vbNormalFocus)
            End If
    End Sub
    

    Context: Usually your revisions on your project consistently Change, or you have a automaticially generated Workbook with a dynamic name. Whatever the Case. This will know your workbook path location and ask if you want to open that specific folder.

    This was very useful for me when i dynamically saved out a bunch of Excels programmatically in the same folder of the workbook. Because instead of closing/minimizing the workbook to go to explorer. I could focus on the project and not lose train of thought.

    0 讨论(0)
  • 2021-02-05 15:45

    If it is in the same folder, then

    Workbooks.Open("ObsReportExcelWorkbook.xlsx")
    

    or

    Workbooks.Open(".\ObsReportExcelWorkbook.xlsx")
    

    should work. I just tried both in Excel VBA, with success.

    0 讨论(0)
  • 2021-02-05 15:52

    Workbooks.Open ("D:\data\Mohit Singh\msk\data\book1.xlsx")

    0 讨论(0)
  • 2021-02-05 15:54

    If the file name is fixed you can use the ActiveWorkbook.Path function to return the path of the current workbook:

    Dim FileName as string FileName = ActiveWorkbook.Path & "\myfile.xlsx

    Check if you need that extra slash character.

    0 讨论(0)
提交回复
热议问题