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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5888011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:26:43  来源:igfitidea点击:

Open File Without Calling Filepath

excelvba

提问by Gil Peretz

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?

有没有办法在不输入文件路径的情况下做到这一点?

回答by Simon

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

如果文件名是固定的,您可以使用 ActiveWorkbook.Path 函数返回当前工作簿的路径:

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

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

Check if you need that extra slash character.

检查您是否需要额外的斜杠字符。

回答by Doc Brown

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).

(即使文档不再是活动文档,或者您更改了当前目录,这也有效)。

回答by rajah9

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 中尝试了两者,都成功了。

回答by Mohit Singh

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

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

回答by DeerSpotter

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. Because instead of closing/minimizing the workbook to go to explorer. I could focus on the project and not lose train of thought.

当我在工作簿的同一文件夹中以编程方式动态保存一堆 Excel 时,这对我非常有用。因为不是关闭/最小化工作簿以转到资源管理器。我可以专注于项目,不会失去思路。