VBA 写入文本文件:运行时错误 52“错误的文件名或编号”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6301495/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:25:29  来源:igfitidea点击:

VBA write to text file: Run-time error 52 'bad file name or number'

vba

提问by rolls

Public Sub EdgeColor_Click()
    Dim intInFile As Integer
    intInFile = FreeFile

    Open "c:\picturename.csv" For Output As #intFileNo
    Print #intFileNo, "test"

    Close #intFileNo
End Sub

This is my code, and I get Run-time error 52 'bad file name or number'. I've tried doing as Input and making the file exist but I get the same error.

这是我的代码,我收到运行时错误 52“错误的文件名或编号”。我试过做为输入并使文件存在,但我得到了同样的错误。

This is VBA inside of a Proficy iFix graphic, and can't for the life of me figure out what I am doing wrong.

这是 Proficy iFix 图形中的 VBA,我一生都无法弄清楚我做错了什么。

回答by paxdiablo

You're mixing up intInFileand intFileNo.

你搞混了intInFileintFileNo

It's possible that intFileNois simply set to zero if you're not using the option explicitcommand, which may explain the invalid number.

这是可能的intFileNo,如果你使用的不是被简单地设置为零option explicit的命令,这可能解释无效的数量。

That little command goes at the top of every oneof my VB/VBA files because I've been burnt by this so many time before.

这个小命令位于我的每个VB/VBA 文件的顶部,因为我之前已经被这个问题烧了很多次了。