让VBA从下到上追加txt

发布于 2024-10-01 06:49:39 字数 118 浏览 5 评论 0原文

我想向后附加一个文本 (.txt) 文件,这可能吗?

我所说的向后是指从下到上书写文本,而不是从上到下标准书写。

为什么是因为我要编译的txt文件被读取,以便列表顶部的项目优先于底部的项目。

I would like to append a text (.txt) file backwards is this possible?

By backwards I mean writing text from bottom to top rather then the standards top to bottom.

Why because the txt file I want to compile is read so that items at the top of the list are given priority to those at the bottom.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怪我闹别瞎闹 2024-10-08 06:49:39

每当您想在顶部插入数据,然后删除/重命名旧文件并将新文件重命名为新文件时,除了创建一个新文件之外,我想不出任何其他方法。

I can't think of any other way than to create a new file whenver you want to insert data at the top and then delete/rename the old one and rename the new file to the new one.

久随 2024-10-08 06:49:39

不确定您的具体要求,但 VBA 中最简单的方法是
1. 添加对 Microsoft Scripting Runtime 的引用。

Public Sub Reverse()
    Dim lReverseString As String
    Dim lFSO As FileSystemObject
    Set lFSO = New Scripting.FileSystemObject
    With lFSO.OpenTextFile("SourceName", ForReading)
        While Not .AtEndOfStream
            ' Note if you are looking to read a line at at a time use .ReadLine Instead of .Read
            lReverseString = .Read & lReverseString
        Wend
    End With
    ' now you have a string in reverse
    With lFSO.CreateTextFile("TargetName", True, False)
        .Write lReverseString
        .Close
    End With

End Sub

这是一个基本的表格,应该可以帮助您前进。

Not sure exactly your requirements BUT the easiest way in VBA is to
1. Add a reference to the Microsoft Scripting Runtime.

Public Sub Reverse()
    Dim lReverseString As String
    Dim lFSO As FileSystemObject
    Set lFSO = New Scripting.FileSystemObject
    With lFSO.OpenTextFile("SourceName", ForReading)
        While Not .AtEndOfStream
            ' Note if you are looking to read a line at at a time use .ReadLine Instead of .Read
            lReverseString = .Read & lReverseString
        Wend
    End With
    ' now you have a string in reverse
    With lFSO.CreateTextFile("TargetName", True, False)
        .Write lReverseString
        .Close
    End With

End Sub

This is a basic form which should get you going.

━╋う一瞬間旳綻放 2024-10-08 06:49:39

数组怎么样?它不适合非常大的文件:

Dim fs As Object
Dim ts As Object
Dim AllTextArray As Variant

''Late binding, no reference required
Set fs = CreateObject("Scripting.FileSystemObject")
''ForReading=1
Set ts = fs.OpenTextFile("c:\docs\BookX.csv", 1)
AllTextArray = Split(ts.ReadAll, vbCrLf)

For i = UBound(AllTextArray) To 0 Step -1
    Debug.Print AllTextArray(i)
Next

How about an array? It would not be suitable with a very large file:

Dim fs As Object
Dim ts As Object
Dim AllTextArray As Variant

''Late binding, no reference required
Set fs = CreateObject("Scripting.FileSystemObject")
''ForReading=1
Set ts = fs.OpenTextFile("c:\docs\BookX.csv", 1)
AllTextArray = Split(ts.ReadAll, vbCrLf)

For i = UBound(AllTextArray) To 0 Step -1
    Debug.Print AllTextArray(i)
Next
冷清清 2024-10-08 06:49:39

如果只是按照与显示顺序相反的顺序编写每个段落或句子:

Sub Test()
    Dim currentDocument As Document
    Set currentDocument = ActiveDocument

    Dim sourceDocument As Document
    Set sourceDocument = Documents.Add("c:\words.txt")

    Dim i As Long
    For i = sourceDocument.Paragraphs.Count To 1 Step -1
        currentDocument.Range.InsertAfter sourceDocument.Paragraphs(i).Range.Text
        DoEvents
    Next

    sourceDocument.Close wdDoNotSaveChanges
End Sub

If it's just writing each paragraph or sentence in the reverse order than it appears:

Sub Test()
    Dim currentDocument As Document
    Set currentDocument = ActiveDocument

    Dim sourceDocument As Document
    Set sourceDocument = Documents.Add("c:\words.txt")

    Dim i As Long
    For i = sourceDocument.Paragraphs.Count To 1 Step -1
        currentDocument.Range.InsertAfter sourceDocument.Paragraphs(i).Range.Text
        DoEvents
    Next

    sourceDocument.Close wdDoNotSaveChanges
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文