VBA:将文本文件中的行复制到 Word 文档中

发布于 2024-11-09 08:52:55 字数 265 浏览 4 评论 0原文

通常我创建宏时会记录该功能,然后相应地调整它们,因此不幸的是,我不知道如何实现此操作。

我有许多文本文件,它们是帐户档案;每个文件至少有 30k+ 行,每个文件中有数百个甚至数千个帐户。文档中的第一行包含帐号,每个帐户都由唯一的文本字符串分隔。

目标是有一个宏在文本文件中查找,找到帐号,然后复制其下面的所有行,直到到达唯一的分隔字符串,并将它们粘贴到活动 Word 文档中以供查看。

我不认为我会从这样一个模糊的问题中得到具体的答案,但任何可以提供的指示将不胜感激。

Usually I create my macro's be recording the feature and then tweaking them accordingly, so I unfortunately have no idea of how to go about achieving this operation.

I have a number of textfiles that are archives of accounts; each file has at least 30k+ lines in, and hundreds if not thousands of accounts in each. The first line in the document has the account number, and each account is divided by a unique string of text.

The goal is to have a macro that looks in the text file, finds the account number, then copies all of the lines beneath it until it reaches the unique dividing string, and paste them into the active word document for viewing.

I don't imagine I'll get a concrete answer out of such a vague question, but any pointers that can be offered would be appreciated.

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

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

发布评论

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

评论(1

浅忆 2024-11-16 08:52:55

这是一个快速&肮脏的一个来说明原理......让文本文件被称为“accounts.txt”并包含以下内容:

Account 1
item 1
item 2
item 3
=========
Account 2
item 4
item 5
=========
Account 3
item 6
item 7
=========

现在让我们看一些使用 Open As的非常基本的 VBA 代码>行输入和循环构造....

Sub GetAccountFromTextFile(FileName As String, Accnt As String)
Dim MyLine As String, State As String

    Open FileName For Input As #1
    State = "Searching"               ' we could make this much simpler but 
                                      ' want to illustrate the different stati
                                      ' the loop is reaching

    Do While Not (EOF(1) Or State = "End")

        Line Input #1, MyLine         ' read next line from text file

                                      ' now process as function of
                                      ' content and current state

        If State = "Reading" And MyLine = "=========" Then
            State = "End"

        ElseIf MyLine = "Account " & Accnt Then
            Selection.InsertAfter "Account " & Accnt & vbCrLf
            State = "Reading"

        ElseIf State = "Reading" Then
            Selection.InsertAfter MyLine & vbCrLf
        End If

    Loop
    Close #1

End Sub

您可以通过另一个子 Start test() 从 Word 文档中的任何位置调用它

Sub test()
    GetAccountFromTextFile "C:\Documents and Settings\MySelf\Desktop\accounts.txt", 1
    GetAccountFromTextFile "C:\Documents and Settings\MySelf\Desktop\accounts.txt", 3
    GetAccountFromTextFile "C:\Documents and Settings\MySelf\Desktop\accounts.txt", 2
End Sub

,并且以下内容将被粘贴到文档中:

Account 1
item 1
item 2
item 3
Account 3
item 6
item 7
Account 2
item 4
item 5

现在您可以在您的关于如何获取文件名和的主要子部分(可能是对话框形式)在调用帐户 getter 之前,您需要修改帐号的条件以及 getter 中的分隔模式。不是很复杂,但应该足以让你继续下去。

祝你好运
迈克·D

here's a quick & dirty one to illustrate the principles .... let the text file be called "accounts.txt" with the following content:

Account 1
item 1
item 2
item 3
=========
Account 2
item 4
item 5
=========
Account 3
item 6
item 7
=========

Now let's look at some very basic VBA code making use of Open As and Line Input and a loop construct ....

Sub GetAccountFromTextFile(FileName As String, Accnt As String)
Dim MyLine As String, State As String

    Open FileName For Input As #1
    State = "Searching"               ' we could make this much simpler but 
                                      ' want to illustrate the different stati
                                      ' the loop is reaching

    Do While Not (EOF(1) Or State = "End")

        Line Input #1, MyLine         ' read next line from text file

                                      ' now process as function of
                                      ' content and current state

        If State = "Reading" And MyLine = "=========" Then
            State = "End"

        ElseIf MyLine = "Account " & Accnt Then
            Selection.InsertAfter "Account " & Accnt & vbCrLf
            State = "Reading"

        ElseIf State = "Reading" Then
            Selection.InsertAfter MyLine & vbCrLf
        End If

    Loop
    Close #1

End Sub

you call this by another sub

Sub test()
    GetAccountFromTextFile "C:\Documents and Settings\MySelf\Desktop\accounts.txt", 1
    GetAccountFromTextFile "C:\Documents and Settings\MySelf\Desktop\accounts.txt", 3
    GetAccountFromTextFile "C:\Documents and Settings\MySelf\Desktop\accounts.txt", 2
End Sub

Start test() from anywhere in a Word Doc, and the following will be pasted into the document:

Account 1
item 1
item 2
item 3
Account 3
item 6
item 7
Account 2
item 4
item 5

Now you can be very creative in your main sub (maybe a dialog form) on how to get file name and account number before you can call the account getter, and you will need to modify the conditions for finding the account number and the seperation pattern within the getter. Not very sophisticated but should be enough to get you going.

Good luck
MikeD

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