如何使用vba获取当前工作目录? [英] How to get current working directory using vba?

查看:269
本文介绍了如何使用vba获取当前工作目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MS Excel 2010,并尝试使用以下代码获取当前目录,

I am using MS Excel 2010 and trying to get the current directory using the below code,

    path = ActiveWorkbook.Path

但ActiveWorkbook.Path返回空白。

But ActiveWorkbook.Path returns blank.

推荐答案

我已经测试过:

当我打开Excel文档 D:\db\\ \\ tmp\test1.xlsm

When I open an Excel document D:\db\tmp\test1.xlsm:


  • CurDir() / code>返回 C:\Users\ [username] \Documents

ActiveWorkbook.Path 返回 D:\db\tmp

所以 CurDir()具有系统默认值,可以更改。

So CurDir() has a system default and can be changed.

ActiveWorkbook.Path 对于相同的已保存的工作簿不会更改。

ActiveWorkbook.Path does not change for the same saved Workbook.

例如,当您执行文件/另存为命令时, CurDir()会更改,并在文件/目录选择对话框中选择一个随机目录。然后单击取消以跳过保存。但是, CurDir()已经更改为上一个选定的目录。

For example, CurDir() changes when you do "File/Save As" command, and select a random directory in the File/Directory selection dialog. Then click on Cancel to skip saving. But CurDir() has already changed to the last selected directory.

这篇关于如何使用vba获取当前工作目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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