vbscript - 无法获取 .vbs 文件中的当前目录
问题描述
我编写了一个 Visual Basic 脚本来执行 Excel 宏。我想动态设置 .xlsm 文件的位置。我使用 FileSystemObject 来获取当前目录。它返回 C:\Windows\System32 但我需要 .vbs 文件所在的目录。
我尝试使用 FileSystemObject 和 WScript 获取当前目录但没有成功。
' Create a FileSystemObject to get the current directory
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
strPath = fso.GetAbsolutePathName(".")
myExcelWorker.DefaultFilePath = strPath
' Open the Workbook specified on the command-line
Dim oWorkBook
Dim strWorkerWB
strWorkerWB = strPath & "\Data center Wattsight long term.xlsm"
Set oWorkBook = myExcelWorker.Workbooks.Open(strWorkerWB)
预期的行为是打开 Excel 文件。但我收到一个错误:
解决方案
您将当前工作目录与脚本的位置混淆了。两者并不相同。
其中任何一个都会为您提供当前的工作目录:
CreateObject("WScript.Shell").CurrentDirectory
CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
这将为您提供脚本的位置:
CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
推荐阅读
- vba - 如果单元格在任何工作表中包含字符串,则 Excel VBA 宏设置左对齐
- r - Purrr 根据包含字符向量的未嵌套变量过滤嵌套数据
- django - 使用外键引用保存 Django 表单
- neo4j - Neo4j 缺少一些程序
- c# - 值不能为空(绘制数据表)
- javascript - 如何过滤道具以使用 ReactJS 进行渲染?
- ruby-on-rails - 在为 Rails 项目的阿拉伯语 lang 文件解析第 ... 行的块映射时没有找到预期的密钥
- python - 在python中输入变量
- c# - 使用 C# 在什么情况下在 String 或 Char 上使用 ToString() 时需要提供 CultureInfo IFormatProvider?
- ios - 两个应用共享同一个通用链接