📜  VBA-文本文件

📅  最后修改于: 2020-11-19 09:15:29             🧑  作者: Mango


您也可以使用VBA读取Excel文件并将单元格的内容写入文本文件。 VBA允许用户使用两种方法处理文本文件-

  • 文件系统对象
  • 使用写命令

文件系统对象(FSO)

顾名思义,FSO帮助开发人员处理驱动器,文件夹和文件。在本节中,我们将讨论如何使用FSO。

Sr.No. Object Type & Description
1

Drive

Drive is an Object. Contains methods and properties that allow you to gather information about a drive attached to the system.

2

Drives

Drives is a Collection. It provides a list of the drives attached to the system, either physically or logically.

3

File

File is an Object. It contains methods and properties that allow developers to create, delete, or move a file.

4

Files

Files is a Collection. It provides a list of all the files contained within a folder.

5

Folder

Folder is an Object. It provides methods and properties that allow the developers to create, delete, or move folders.

6

Folders

Folders is a Collection. It provides a list of all the folders within a folder.

7

TextStream

TextStream is an Object. It enables the developers to read and write text files.

驾驶

驱动器是一个对象,可用于访问特定磁盘驱动器或网络共享的属性。 Drive对象支持以下属性-

  • 可用空间
  • 驾驶证
  • 驱动类型
  • 文件系统
  • 可用空间
  • 准备好了
  • 路径
  • 根文件夹
  • 序列号
  • 共享名
  • 总尺寸
  • 卷名

步骤1-在继续使用FSO编写脚本之前,我们应该启用Microsoft Scripting Runtime。为此,请导航至“工具”→“引用”,如以下屏幕截图所示。

VBScript中的Excel FSO

步骤2-添加“ Microsoft Scripting RunTime”,然后单击“确定”。

VBScript中的Excel FSO

步骤3-在文本文件中添加要写入的数据并添加命令按钮。

VBScript中的Excel FSO

步骤4-现在是时候编写脚本了。

Private Sub fn_write_to_text_Click()
   Dim FilePath As String
   Dim CellData As String
   Dim LastCol As Long
   Dim LastRow As Long
  
   Dim fso As FileSystemObject
   Set fso = New FileSystemObject
   Dim stream As TextStream
  
   LastCol = ActiveSheet.UsedRange.Columns.Count
   LastRow = ActiveSheet.UsedRange.Rows.Count
    
   ' Create a TextStream.
   Set stream = fso.OpenTextFile("D:\Try\Support.log", ForWriting, True)
  
   CellData = ""
  
   For i = 1 To LastRow
      For j = 1 To LastCol
         CellData = Trim(ActiveCell(i, j).Value)
         stream.WriteLine "The Value at location (" & i & "," & j & ")" & CellData
      Next j
   Next i
  
   stream.Close
   MsgBox ("Job Done")
End Sub

输出

执行脚本时,请确保将光标放在工作表的第一个单元格中。如以下屏幕快照中“ D:\ Try”下所示创建Support.log文件。

VBScript中的Excel FSO

该文件的内容显示在以下屏幕截图中。

VBScript中的Excel FSO

写命令

与FSO不同,我们不需要添加任何引用,但是,我们将无法使用驱动器,文件和文件夹。我们将能够仅将流添加到文本文件中。

Private Sub fn_write_to_text_Click()
   Dim FilePath As String
   Dim CellData As String
   Dim LastCol As Long
   Dim LastRow As Long
  
   LastCol = ActiveSheet.UsedRange.Columns.Count
   LastRow = ActiveSheet.UsedRange.Rows.Count
    
   FilePath = "D:\Try\write.txt"
   Open FilePath For Output As #2
  
   CellData = ""
   For i = 1 To LastRow
      For j = 1 To LastCol
         CellData = "The Value at location (" & i & "," & j & ")" & Trim(ActiveCell(i, j).Value)
         Write #2, CellData
      Next j
   Next i
  
   Close #2
   MsgBox ("Job Done")
End Sub

输出

执行脚本后,将在位置“ D:\ Try”中创建“ write.txt”文件,如以下屏幕截图所示。

VBScript中的Excel FSO

文件的内容显示在以下屏幕快照中。

VBScript中的Excel FSO