如何在VBA中获取Excel文件名/路径 [英] How to get the excel file name / path in VBA

查看:193
本文介绍了如何在VBA中获取Excel文件名/路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,我正在Excel文件 sample.xls 中编写VBA.现在,我想在我的VBA中获得 sample.xls 完整路径.我该怎么办?

Say, I'm writing a VBA inside my excel file sample.xls. Now I want to get the full path of sample.xls in my VBA. How do I do it?

推荐答案

如果您是指VBA,则可以使用FullName,例如:

If you mean VBA, then you can use FullName, for example:

strFileFullName = ThisWorkbook.FullName

(根据评论进行更新:如果其他office文件可能已打开并处于活动状态,则前者使用的ActiveWorkbook.FullName很有可能是错误的.但是,如果您将宏存储在另一个文件中,则由用户@ user7296559 此处,并且确实想要使用宏的文件的文件名,ActiveWorkbook可能是正确的选择,如果保证在执行时处于活动状态.)

(updated as considered by the comments: the former used ActiveWorkbook.FullName could more likely be wrong, if other office files may be open(ed) and active. But in case you stored the macro in another file, as mentioned by user @user7296559 here, and really want the file name of the macro-using file, ActiveWorkbook could be the correct choice, if it is guaranteed to be active at execution time.)

这篇关于如何在VBA中获取Excel文件名/路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆