vba Excel - 使用完整路径名关闭/保存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31147953/
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
Excel - Close/Save using full path name
提问by Elixir
Is there a way of inserting the full pathname instead of the filename when closing and saving a workbook
有没有办法在关闭和保存工作簿时插入完整路径名而不是文件名
E.g below
例如下面
Workbooks("BOOK1.XLS").Close SaveChanges:=true
Workbooks("C:\user\docs\BOOK1.XLS").Close SaveChanges:=true
Reason why I ask is that I've already named all my full pathnames in strings
我问的原因是我已经在字符串中命名了我所有的完整路径名
Workbooks(i).Close SaveChanges:=true
So for each i in array etc
所以对于数组中的每个 i 等
回答by paul bica
The Workbook object can be used like in your first statement
Workbook 对象可以像在你的第一条语句中一样使用
Workbooks("BOOK1.XLS").Close
because it uses the .Name property (without full path)
因为它使用 .Name 属性(没有完整路径)
The Workbooks.Openmethod on the other hand, takes as parameter the full path and file name:
Workbooks.Open另一方面,该方法将完整路径和文件名作为参数:
Workbooks.Open "C:\user\docs\BOOK1.XLS"
It can take the file name without a path as well, but it will search for it in the default folder
它也可以使用没有路径的文件名,但它会在默认文件夹中搜索它
.
.
There are 2 options I use to solve similar problems, where all strings include full path and file name:
我使用 2 个选项来解决类似问题,其中所有字符串都包含完整路径和文件名:
set a reference to the workbook when you open it:
Set wb = Workbooks.Open("C:\user\docs\BOOK1.XLS")then you can close it like this
wb.Close SaveChanges:=True
Extract the file name from the string containing both, path and file name:
wbName = Mid(fullName, InStrRev(fullName, "\") + 1)then you can close it as in your first line:
Workbooks(wbName).Close SaveChanges:=true
打开时设置对工作簿的引用:
Set wb = Workbooks.Open("C:\user\docs\BOOK1.XLS")然后你可以像这样关闭它
wb.Close SaveChanges:=True
从包含路径和文件名的字符串中提取文件名:
wbName = Mid(fullName, InStrRev(fullName, "\") + 1)然后你可以像在第一行一样关闭它:
Workbooks(wbName).Close SaveChanges:=true

