简体   繁体   English

打开文件而不调用文件路径

[英]Open File Without Calling Filepath

I'm using excel VBA. I want to press a button that opens another file directly without the effect of "choosing file window".我用的是excel VBA,我想按一个按钮直接打开另一个文件,没有“选择文件窗口”的效果。

This is the current code:这是当前代码:

Sub loadFile_click()
Workbooks.Open("C:\Users\GIL\Desktop\ObsReportExcelWorkbook.xlsx")
End Sub

In this case, the file is in the same folder as the main file.在这种情况下,该文件与主文件位于同一文件夹中。

Is there any way to do it without entering the file's path?有没有办法不输入文件路径就可以做到这一点?

If the file name is fixed you can use the ActiveWorkbook.Path function to return the path of the current workbook:如果文件名是固定的,您可以使用 ActiveWorkbook.Path function 返回当前工作簿的路径:

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

Check if you need that extra slash character.检查您是否需要额外的斜杠字符。

If the file is in the same folder as the document containing your VBA macro, use如果文件与包含 VBA 宏的文档位于同一文件夹中,请使用

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). (即使文档不再是活动文档,或者您更改了当前目录,这也有效)。

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.我刚刚在 Excel VBA 中尝试了两者,都成功了。

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

Open Folder of ActiveWorkBook打开 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.当我以编程方式在工作簿的同一文件夹中动态保存一堆 Excel 时,这对我非常有用。 Because instead of closing/minimizing the workbook to go to explorer.因为不是将工作簿关闭/最小化到 go 到资源管理器。 I could focus on the project and not lose train of thought.我可以专注于该项目,而不会失去思路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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