使用 VBA 从 .DAT 文件填充 Word 字段
我正在使用一个 Java 应用程序,该应用程序将编写一个 .dat
文件,该文件应该用作模板来填充具有 的
和其中的Word
模板字段书签
。我花了相当长的时间搜索有关用于模板化 Word 字段/书签
的 .dat
文件的信息,但我还没有找到任何答案。
我编写了 VBA 代码来获取文件,但我有两个大问题,我似乎无法找到答案。如果有人能回答以下问题,我将不胜感激:
.dat
文件代码应该是什么样子? (不是代码 生成它,有这个,但是应该如何格式化它才能达到所需的结果)用
VBA
打开.dat
文件后,我需要做什么任何事物 按代码方式让它用其数据替换fields/bookmarks
?
目前我确实有代码可以获取 .dat 文件并打开它。我编写了代码来插入书签和字段,但我不确定 .dat
文件是否有特定的结构供 vba
读取,我不知道这是否相关。感谢 Jean-François Corbett 和 Roman 对此进行了澄清。
考虑到这一点,我决定使用以下 .dat
文件格式。
File.dat
RequisitionHeader_requisitionNumber=11-2019
RequisitionHeader_poNumber=889-0936
RequisitionHeader_orderDate=11/12/1901
这样,使用我现在打开文件的VBA
,我是否需要在VBA
内部编写一个文本解析器来查找值并将其替换到相应的字段/书签名称中?
I'm working with a Java application that is going to write a .dat
file that is supposed to be used as a template to fill a Word
template that has fields
and bookmarks
in it. I've spent quite awhile searching for information on .dat
files being used to template a Word field/bookmark
and I've yet to come up with anything.
I have the VBA code written to go and get the file, but I have two large problems that I can't seem to locate the answer to. I'd appreciate if someone could answer the following:
What should the
.dat
file code look like? (not the code to
generate it, have that, but how should it be formatted to achieve the desired result)After I open the
.dat
file withVBA
, do I need to do anything
code-wise to have it replace thefields/bookmarks
with its data?
Currently I do have the code to go out and get the .dat
file and open it. I have code written to do inserts for bookmarks and fields, but seeing as how I wasn't sure if there was a specific structure to.dat
files for vba
to read, I didn't know if it was pertinent or not. Thank you to Jean-François Corbett and Roman for clarifying this.
Taking this into consideration, I've decided to use the following .dat
file format.
File.dat
RequisitionHeader_requisitionNumber=11-2019
RequisitionHeader_poNumber=889-0936
RequisitionHeader_orderDate=11/12/1901
With that, using the VBA
I have now that opens the file, do I need to write a text parser inside of VBA
to find and replace values into the corresponding field/bookmarkName?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,现在可以更好地理解了。因此,您已经有了循环浏览书签和字段并将值放入其中的代码,您的问题是了解如何使用 VBA 从文件中读取数据。
请记住,VBA 只不过是集成在 Office 应用程序中的 Visual Basic 6.0。因此,访问文件仅限于旧的基本 I/O 过程。您可以在此处找到第一信息。
另一方面,在使用 VBA 时,您始终可以使用 Windows 脚本主机将文本文件的所有行读取到内存/变量中,如下所示:
然后您可以随意解析文本,例如使用 Split 函数首先将所有行转换为数组,然后获取每行的值。
还有使用与 INI 文件相关的功能的旧方法;您可能会在一些旧手册中找到这一点。这里您应该记住,INI 文件的 Windows API 函数不会读取 = 符号后超过 256 个字符。
OK, now one can understand better. So you have already code to loop through bookmarks and fields and put values in it, and your problem is to know how you can read data from a file with VBA.
Please keep in mind that VBA is nothing else then an Visual Basic 6.0 integrated in an Office application. So accessing files is restricted to the old Basic I/O procedures. You can find first information here.
On the other hand, while working with VBA you can always use the Windows Scripting Host to read all lines of a text file into memory/into a variable, like this:
Afterwards you're free to parse your text as you like it, e.g. using the Split function to convert all lines first into an array, and then to get the values from each line.
There's also the old approach to use the functions related to INI files; you may find this in some old handbooks. Here you should keep in mind that the Windows API function for INI files don't read more than 256 characters after the = sign.
首先,您必须了解输入数据的数据结构(在 .dat 文件中 - 是文本数据还是二进制数据,数据建模的逻辑模式是什么),然后找到读取数据的好方法(在 vba 中打开文件,从 vba 中找到并访问文件中的相关数据),然后您可能必须验证数据(检查数据完整性、格式等),最后将数据填充到单词中的字段中文档。
正如让-弗朗索瓦·科贝特 (Jean-François Corbett) 之前指出的那样:除非您对您正在使用的内容提供一些见解,否则只能得到一般性的答案。
First you will have to understand the datastructure of the input data (in the .dat file - is it text data or binary data, what are the logical patterns after which the data is modelled), then find a good way to read the data (open the file in vba, locate and access the relevant data in the file from vba), maybe you will then have to validate the data (check for data integrity, format etc.) and finally fill in the data into the fields in the word document.
As pointed out by Jean-François Corbett before: Unless you provide some insight into what you are working with only general answers are possible.