在VBA中写入文本文件 [英] Write to text file in VBA
本文介绍了在VBA中写入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将文件打开到使用VBA创建的文件后,我尝试打印文件.我被卡住的部分正在写入文件.这是我到目前为止所拥有的.
I am trying to print a file after opening it to a file I create with the VBA. The part I am stuck at is writing to the file. Here is what I have so far.
Sub test()
Dim myFile As String
Dim testFile As String
Dim intChoice As Integer
Dim fs, f
myFile = Application.GetSaveAsFilename & "kml"
Open myFile For Output As #1
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
intChoice = Application.FileDialog(msoFileDialogOpen).Show
If intChoice <> 0 Then
testFile = Application.FileDialog( _
msoFileDialogOpen).SelectedItems(1)
End If
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile(testFile)
Print #1, f
Close #1
End Sub
推荐答案
逐行读取文本流并根据需要进行打印.循环执行此操作.
Read the textstream line-by-line and print as needed. Do this in a loop.
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile(testFile)
Do While Not f.AtEndOfStream
Print #1, f.ReadLine
Loop
Set f = Nothing
Set fs = Nothing
或者,您也许可以省略循环,而只需执行 Print#1,f.ReadAll
.
Or, you may be able to omit the loop and simply do Print #1, f.ReadAll
.
这篇关于在VBA中写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文