Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
722 views
in Technique[技术] by (71.8m points)

vba - Write contents of immediate window to a text file

I'm writing a macro which goes through a document and tries to parse it by Style. Right now, anything in the designated style is copied onto the immediate window. Is there a way to automate the macro further to move the text from the immediate window into a txt file? Otherwise, anyone using the macro would not be able to see the text unless they opened up VBA, correct?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's my suggestion: write to the immediate window AND to a file at the same time. Examples below.

Why make the information first transit in the immediate window, and only then write it to a file from there? That just sounds perversely and uselessly difficult!

Dim s As String
Dim n As Integer

n = FreeFile()
Open "C:est.txt" For Output As #n

s = "Hello, world!"
Debug.Print s ' write to immediate
Print #n, s ' write to file

s = "Long time no see."
Debug.Print s
Write #n, s ' other way of writing to file

Close #n


Dim FSO As Scripting.FileSystemObject
Set FSO = New Scripting.FileSystemObject
Dim txs As Scripting.TextStream
Set txs = FSO.CreateTextFile("C:est2.txt")
s = "I like chickpeas."
Debug.Print s ' still writing to immediate
txs.WriteLine s ' third way of writing to file
txs.Close
Set txs = Nothing
Set FSO = Nothing

Note that this last bit of code requires a reference to be set: Tools > References > checkmark at Microsoft Scripting Runtime.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...