在 Excel 2007 VBA 中使用相对路径工作的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1128677/
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
Issue Working w/ Relative Path in Excel 2007 VBA
提问by user138388
I am working with Excel 2007 and the following suggestion from this site has not worked:
我正在使用 Excel 2007,但该站点的以下建议无效:
Workbooks.Open Filename:=ThisWorkbook.Path & "\Chapter 7 - 10 MECHANICAL.xls"
I have also tried the following code w/ no luck as well:
我也尝试了以下代码,但没有运气:
Workbooks.Open Filename:=app.Path & "Chapter 7 - 90 ECS 1 LLC.xls"
The files are in the same path as the workbook with the macro, so I am at a loss for what I am doing wrong.
这些文件与带有宏的工作簿位于同一路径中,所以我对我做错了什么感到茫然。
I am running Microsoft Vista.
我正在运行 Microsoft Vista。
Thanks in advance.
提前致谢。
回答by Dave Cripps
I know this is an older post, but I didn't see the final answer given and I ran into the same issue. The first line of code is correct except that the backslash needs to be escaped.
我知道这是一篇较旧的帖子,但我没有看到给出的最终答案,我遇到了同样的问题。第一行代码是正确的,只是反斜杠需要转义。
Simply change
简单地改变
Workbooks.Open Filename:=ThisWorkbook.Path & "\Chapter 7 - 10 MECHANICAL.xls"
to
到
Workbooks.Open Filename:=ThisWorkbook.Path & "\Chapter 7 - 10 MECHANICAL.xls"
and you'll be good to go.
你会很高兴去的。
回答by shahkalpesh
For ThisWorkbook.Path to work, an existing excel file should be open.
I mean - for a new workbook (which is not saved), the Path will be blank.
要使 ThisWorkbook.Path 工作,应打开现有的 excel 文件。
我的意思是 - 对于新工作簿(未保存),路径将为空白。
EDIT: Is the macro enabled? Does the code you provided run?
I tried with 2 files in same dir & the code with ThisWorkBook.Path & "\myOtherFile.xls" works.
编辑:是否启用了宏?你提供的代码能运行吗?
我尝试在同一目录中使用 2 个文件,并且使用 ThisWorkBook.Path 和 "\myOtherFile.xls" 的代码有效。
回答by JustPlainBill
Try this:
尝试这个:
Workbooks.Open Filename:=ThisWorkbook.Path & application.pathseparator & "Chapter 7 - 10 MECHANICAL.xls"
Workbooks.Open Filename:=ThisWorkbook.Path & application.pathseparator & "Chapter 7 - 10 MECHANICAL.xls"
This will make sure the proper slash is used.
这将确保使用正确的斜杠。
Bill
账单
回答by SLaks
app.Path
will return the path to Excel itself (in Program Files), which is not what you want.
app.Path
将返回 Excel 本身的路径(在程序文件中),这不是您想要的。
You probably want to call the CurDir function, which will return the current directory.
您可能想要调用 CurDir 函数,该函数将返回当前目录。
EDIT: On second thought, you might not want the CurDir function, becausethe current directory doesn't change when you open a file. If you're trying to open a file in the same folder as the current workbook, your code should work.
编辑:再想一想,您可能不需要 CurDir 函数,因为打开文件时当前目录不会更改。如果您尝试在与当前工作簿相同的文件夹中打开文件,您的代码应该可以工作。
Make sure that ThisWorkbook is the workbook that you think it is. Also, what error are you getting?
确保 ThisWorkbook 是您认为的工作簿。另外,你得到了什么错误?
回答by Robert Mearns
Your first line of code is correct.
你的第一行代码是正确的。
Workbooks.Open Filename:=ThisWorkbook.Path & "\Chapter 7 - 10 MECHANICAL.xls"
ThisWorkbook.pathwill return the path to the workbook where the code is being run from.
ThisWorkbook.path将返回运行代码的工作簿的路径。
If the workbook running the code has not yet been saved, it will return an empty string.
如果运行代码的工作簿尚未保存,它将返回一个空字符串。
Try adding this code to see what is happening:
尝试添加此代码以查看发生了什么:
Debug.Print ThisWorkbook.Path & "\Chapter 7 - 10 MECHANICAL.xls"
Debug.Print ThisWorkbook.Saved
The output can be viewed via 'View - Immediate Window'
可以通过“查看 - 立即窗口”查看输出