VBA:将文本文件中的行复制到 Word 文档中
通常我创建宏时会记录该功能,然后相应地调整它们,因此不幸的是,我不知道如何实现此操作。
我有许多文本文件,它们是帐户档案;每个文件至少有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个快速&肮脏的一个来说明原理......让文本文件被称为“accounts.txt”并包含以下内容:
现在让我们看一些使用
Open As
和的非常基本的 VBA 代码>行输入
和循环构造....您可以通过另一个子 Start test() 从 Word 文档中的任何位置调用它
,并且以下内容将被粘贴到文档中:
现在您可以在您的关于如何获取文件名和的主要子部分(可能是对话框形式)在调用帐户 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:
Now let's look at some very basic VBA code making use of
Open As
andLine Input
and a loop construct ....you call this by another sub
Start test() from anywhere in a Word Doc, and the following will be pasted into the document:
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