vba 如何在 MS Office 中使用宏创建文本文件?

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

How to create a text file using Macro in MS Office?

vbams-wordms-officeword-vba

提问by EbinPaulose

I have word document. I want to create a text file when opening this document. How can i create text file using macro?

我有word文档。我想在打开这个文档时创建一个文本文件。如何使用宏创建文本文件?

回答by Siddharth Rout

Try this. You can create the text file in Document_Open()event

尝试这个。您可以在Document_Open()事件中创建文本文件

Private Sub Document_Open()
    Dim filesize As Integer
    Dim FlName As String

    FlName = "C:\Sample.Txt"

    '~~> get a free file handle
    filesize = FreeFile()

    '~~> Open your file
    Open FlName For Output As #filesize

    '~~> Export Text
    Print #filesize, "Hello World"
    Close #filesize
End Sub