如何从Excel VBA可靠地写入文本文件? [英] How can I write to a text file reliably from Excel VBA?
问题描述
我正在尝试使用Excel VBA写入文本文件。我正在做以下几件事:
I'm trying to use Excel VBA to write to a text file. I'm doing a few of these:
MyFile1 = "C:\outputFromExcel1.txt"
fnum1 = FreeFile()
Open MyFile1 For Output As fnum1
然后写如下:
Print #fnum1, text
上面的所有变量都被声明为 Dim
。我正在为这些文件写上数百条线,很少有线被截断 - 即两端正在被切断。有没有更好的方式写入Excel VBA中的文件?
All variables in the above are declared just with Dim
. I'm writing hundreds of lines to the files and, very rarely, lines are being truncated -- i.e. the ends are being chopped off. Is there a better way to write to a file in Excel VBA?
编辑:我刚刚意识到,它总是最后一行被写入被截断。所以我想我需要关闭或刷新文件?
I've just realized that it's always the last lines to be written that are truncated. So I guess I need to close or flush the files somehow?
推荐答案
你可以使用关闭#fnum1
关闭文件句柄,它应该刷新剩余的缓冲区内容。
You can use Close #fnum1
to close the file handle and it should flush the remaining buffer contents.
这篇关于如何从Excel VBA可靠地写入文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!