在VBA中打开方法 [英] Workbooks.Open Method in VBA
问题描述
我的vba脚本在 myMacro.xls Workbooks.Open
方法工作正常如下,
My vba script in myMacro.xls Workbooks.Open
Method work well as below,
Workbooks.Open Filename:="D:\ExcelMacroProj\myTest.xls", ReadOnly:=True
但是,当我尝试将文件名
值更改为新路径时,如下所示,工作显示运行时错误1004 。
But when I try to change the Filename
value to a new path as below, but all my practices didn't work. Show Run time error 1004.
Workbooks.Open Filename:="myTest.xls", ReadOnly:=True
or
Workbooks.Open Filename:="./myTest.xls", ReadOnly:=True
or
Workbooks.Open Filename:=".\myTest.xls", ReadOnly:=True
实际上 myMacro.xls 和 myTest.xls 被放置在同一个文件夹中。这就是为什么我要更改为灵活的文件夹目录。
Actually myMacro.xls and myTest.xls was placed in the same folder. That's why I want to change to a flexible folder directory.
我该如何解决这个问题?感谢您的阅读和回复。
how could I fix this issue? Appreciated for your read and reply.
推荐答案
您可以尝试使用 ThisWorkbook.Path
制作绝对路径。它返回运行宏的工作簿的文件夹路径(不包括文件名)。这样的东西应该可以工作:
You might try using ThisWorkbook.Path
to make an absolute path. It returns the folder path of the workbook running the macro (excluding the filename). Something like this should work:
Workbooks.Open Filename:=ThisWorkbook.Path & "\myTest.xls", ReadOnly:=True
确保包含反斜杠,因为工作簿路径不以一个结尾。
Make sure to include a backslash, since the workbook path doesn't end with one.
这篇关于在VBA中打开方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!