Run-time Error ‘1004’ The file could not be accessed in Excel 2016.

huangapple go评论68阅读模式
英文:

Run-time Error '1004' The file could not be accessed in Excel 2016

问题

我被给予了一个Excel 2016电子表格,我不拥有也不维护。一个宏映射到一个按钮,它传递一个文件名,比如*.\input\common\datafile.txt*。

这个电子表格随着datafile.txt文件一起提供在电子表格根目录下的input\common\子目录中。

当团队的其他成员在电子表格中执行宏时,datafile.txt文件可以正常加载。在我的电脑上,宏停止并显示运行时错误1004,表示无法访问文件。

宏看起来像这样:

Sub Load_Input_File(file_location As String)

' 其他代码已注释掉'

Workbooks.OpenText filename:=file_location, Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1)), TrailingMinusNumbers:=True

调试器显示file_location为".\input\common\datafile.txt"。如果我将file_location更改为"D:\Temp\input\common\datafile.txt",宏将正常运行。

对于所有其他用户,宏按原样运行。但对于我的系统来说,我使用的是与我的队友相同的标准笔记本电脑(Windows 10,Excel 2016),相对路径无法正常运行。

由于我不拥有电子表格或宏,更改代码不是一个选项。是否有人能想到Excel为什么会报告找不到基于相对路径名的文件的任何环境原因?这几乎就像它认为它正在从不同的位置运行一样。

我既不是Excel高手也不是VBA专家。这是另一组提供的一个“它只是有效”的解决方案,实际上每个人都能运行,除了我。据说我和我的团队成员拥有相同的Office安装等等。我没有看到我们的账户之间有任何特权或权限差异。

有人有什么想法吗?

英文:

I've been given an Excel 2016 spreadsheet which I don't own and do not maintain. A macro is mapped to a button, which passes a filename such as .\input\common\datafile.txt.

The spreadsheet ships with the datafile.txt file in the input\common\ subdirectory below the spreadsheet root directory.

When other members of the team execute the macro in the spreadsheet, the datafile.txt file loads without problem. On my PC, the macro halts with run time error 1004, saying that the file cannot be accessed.

The macro looks like this:

Sub Load_Input_File(file_location As String)

' Commented out other code'

Workbooks.OpenText filename:=file_location, Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1)), TrailingMinusNumbers:=True

and the debugger shows that file_location is ".\input\common\datafile.txt". If I change file_location to be "D:\Temp\input\common\datafile.txt", the macro runs correctly.

For all other users, the macro runs as is. On my system, which is the same standard laptop (Windows 10, Excel 2016) as my teammates, the relative path fails.

Since I don't own the spreadsheet or the macro, changing the code is not an option. Can anyone think of any environmental reason why Excel reports not finding a file based on a relative pathname? It's almost as if it's thinking it's running from a different location or something.

I'm neither an Excel guru nor a VBA wizard. This was provided by another group as a "it just works" solution that has in fact run for everyone but me. I supposedly have the same installation of Office and etc. as my team mates. I don't see any privilege or permission differences between our accounts.

Does anyone have any ideas?

答案1

得分: 1

正如Tim Williams在评论中所写,你的问题是.\input\common\datafile.txt指向所谓的“当前工作目录”(cwd)。这个cwd可以随时改变,所以在代码中使用它不是一个很聪明的主意。请注意,cwd对整个Excel会话(包括VBA)都有效,而不仅仅是一个工作簿。你可以使用VBA命令CurDir来检查cwd。

无论如何,正如你所说,你不能改变代码,所以你需要改变cwd,使其指向包含Excel宏的文件所在的文件夹(更准确地说,是Input子文件夹所在的位置)。

你可以通过打开文件浏览对话框来更改cwd:在Excel中,使用文件->打开,然后单击浏览。现在导航到文件夹。如果你的包含数据的Excel工作簿还没有打开,你可以从这里打开它,如果它已经打开,只需按取消。

另一种方法是在VBA中使用ChDir命令。你可以在个人文件中创建一个小宏,也可以为它分配一个键盘快捷键(如果你还没有个人工作簿,请参阅Microsoft文档)。

Sub openTheMagicMacroFile()
    Const Path = "C:\Users\FunThomas\Documents\TestVBA"
    Const Filename = "LoadInput.xlsm"
    
    ChDrive left(path, 2)
    ChDir Path
    Workbooks.Open Path & "\" & Filename
End Sub

只需根据你的需要调整PathFilename。如果你愿意,你还可以在工作簿中添加一个调用宏的操作(使用Application.Run命令)。

更新 感谢Tim Williams的提示:你需要发出ChDrive命令来先设置驱动器。在后台,每个驱动器都有一种cwd,所以如果你为不同的驱动器发出chDir命令,那个驱动器的cwd会改变,但真正的cwd保持不变。代码已经经过了修改。

英文:

As Tim Williams wrote in the comments, your issue is that .\input\common\datafile.txt points to the so called current working directory (cwd). This cwd can change at any time, so it's not a very clever idea to use this as path in the code. Note that the cwd is valid for the whole Excel session (including VBA), not only one workbook. You can check the cwd in VBA with the command CurDir.

Anyhow, as you stated that you can't change the code, you need to change the cwd so that points to the folder where the Excel with the macro is (or, more precise, where the Input-subfolder is located.

You can change the cwd by opening the File Browser Dialog: In Excel, use File->Open and click Browse. Now navigate to the folder. If your Excel Workbook with the data is not already open, you can open it from here, if it is already open, just press Cancel.

Alternative is to use the ChDir-command in VBA. You could create a small macro in your Personal file and maybe assign a keyboard shortcut to it (if you don't have a Personal workbook yet, see the Microsoft documentation).

Sub openTheMagicMacroFile()
    Const Path = "C:\Users\FunThomas\Documents\TestVBA"
    Const Filename = "LoadInput.xlsm"
    
    ChDrive left(path, 2)
    ChDir Path
    Workbooks.Open Path & "\" & Filename
End Sub

Just adapt the Path and Filename to your needs. If you want, you can also add a call to the macro within the workbook (use the Application.Run command)

Update Thanks to the hint of Tim Williams: You will need to issue a ChDrive to set the drive first. In the background, there is a kind of cwd for every drive, so if you issue the chDir command for a different drive, the cwd of that drive changed, but the real cwd remains untouched. Code adapted.

huangapple
  • 本文由 发表于 2023年3月1日 09:15:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75598772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定