搜索和替换的替代方法,用于告诉应用程序将内容放在 Word 文档中的位置
我编写了一个还可以进行文档管理的应用程序。我在 Delphi 中执行此操作,但问题也可能适用于 C#。我刚刚收到一个非常有用的回复 我问的这个问题使得“搜索和替换”工作变得更容易。
我的一个要求是每次用户打开文档时根据数据库数据自动在文档中插入一些数据。到目前为止(这包括上面提到的问题的主题)我只做了一次:“从模板创建文档”=替换一次,工作就完成了。搜索与Replace 非常适合这个,对于用户来说也很容易。
现在的要求是每次打开文档时连续执行此操作,因此同一文档应包含“占位符”和“真实数据”(一旦我打开它)。
一个简单的例子是这样的:
在Word文档的标题中,用户想要为3个字段插入一种占位符:company_logo(图像)、修订编号(整数)、修订日期(日期时间)。
目前我所知道的唯一技术是:
1)搜索和搜索。替换 - 我已经用它来做其他事情
2)占位符 - 我从未使用过它,但我想它是可行的(这里有几个帖子,例如 这个)
带有搜索和;替换我会这样做:
a)我要求用户准备一个word文档,在其中他在大括号中写入数据库字段,如{NAME} {SURNAME}(注意:这只是大括号,而不是MSWord占位符)。 b) 当用户“签出”文档进行编辑时,他将继续阅读 {NAME} 和 SURNAME} c)当用户“以只读模式打开”时,我执行搜索和替换技巧
这肯定会与搜索和替换一起工作。替换对于用户来说是一种非常简单易懂的技术。
我想占位符也同样适用(可以使用 CTRL + F9 在 Word 中插入的占位符)。
但如何处理图像呢?
可以使用哪些替代方法在运行时在 Word 文档中插入内容?
I write an application that does also document management. I do this in Delphi but the question may apply to c# too. I just got a very useful reply to this question I asked that made ma Search&Replace work easier.
One requirement I have is automatically insert some data in the document according to the db data every time the user opens a document. Up to now (and this includes the subject of the question mentioned above) I was doing this just once: "create document from template" = substitute once and the work is done. Search & Replace works perfectly for this, it is also very easy for users.
Now the requirement is to do it continuously, every time I open a document, so the same document should contain the "placeholder" and the "real data" (once I open it).
A simple example is this:
in the header of a word docuement the user wants to insert a kind of placeholder for 3 fields: company_logo (an image), revision numner (an integer), revision date (a datetime).
Currently the only techniques I am aware of are:
1) Search & Replace - I already use this for doing other things
2) placeholders - I never used that but I guess it is doable (there are several posts here like this one)
With Search & Replace I would do like this:
a) I ask a user to prepare a word document where he writes db fields in curly braces like {NAME} {SURNAME} (note: this are just curly braces, not MSWord placeholders).
b) when the user "checks out" the document for editing it he will keep reading {NAME} and SURNAME}
c) when the user "opens in read only mode" I do the Search&Replace trick
This for sure will work sith Search & Replace and it is a very simple technique to understand for the user.
I guess the same will work with Placeholders (the ones that is possible to inserti in Word with CTRL + F9).
But what to do with images?
And which are the alternatives one can use to insert content at runtime in a word document?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在对您的问题的评论中如何避免 255 限制 ( 如何使用 OLE 绕过 MSWord 搜索和替换中的 255 个字符限制),我提到文档变量可能是向单词添加运行时内容的更简单方法 文档。
我认为它们也是您想要在这里做的事情的答案。然而,它们对于最终用户来说并不完全直观,因此最好保留占位符。不幸的是,这并不完全可能,因为您最终会得到一个包含占位符和文档变量字段的文档。这最终会导致价值观被重复。
尽管如此,我们仍然可以为最终用户保留占位符。当然是为了他们对“模板”的初步编辑。甚至可以向已“转换为使用变量”的现有 Word 文档添加新的占位符。
下面的代码展示了如何进行。我没有包含打开和关闭 Word 和/或文档的“标准”Word 自动化部分。我从您使用模板打开新文档并且现在准备开始替换值的位置开始。解决方案在于三种方法:
需要 HideExistingFieldCodes,因为我们也使用占位符作为文档变量的名称,如果字段显示其代码而不是值,我们最终会用新的名称替换文档变量名称。 document 变量,这可能会让 Word 大声呕吐。
一旦隐藏任何现有字段的代码,我们就可以扫描文档中的占位符,并将每个占位符替换为文档变量字段。
我使用 TValueListEditor(名为 PlaceHoldersEdit)来保存占位符名称和值。它的第一行包含此控件中列的标题,因此会被跳过。之后,需要循环遍历所有占位符以及循环遍历其在 Word 文档中出现的位置。
最后我们可以开始添加实际的文档变量,它将保存文档变量字段的值。 (哦,我多么喜欢所有这些听起来相似的名称。)
在这个方法中,我循环遍历所有文档变量字段并从每个字段的代码中提取变量名称。然后我尝试查找该名称的文档变量。如果它还不存在,我会添加它。如果它确实存在,我会更改它的值。如果性能是一个问题并且变量名称在许多字段中使用,则可以对此进行一些优化以仅更改该值一次。
哦,我已经使用“已保存”文档进行了测试,但我还没有向已经通过将占位符转换为文档变量字段的文档添加新的占位符。所以,你可能仍然需要把事情弄平一些。
顺便说一句:在 Word 中,Shft-F9 可在显示其代码和显示其值之间切换字段。您将不得不使用“选项”对话框(Word2003,不知道在以后的版本中最终在哪里)来一次性显示/隐藏它们。该选项是“显示”或“查看”选项卡(翻译自荷兰语)上“显示”或“显示”下的“字段代码”。
In a comment on your question how to avoid the 255 limit ( How to bypass the 255 char limitation in MSWord Search&Replace using OLE ), I mentioned document variables as perhaps an easier way to add run-time content to a word document.
I think they are also the answer to what you want to do here. However, they are not exactly intuitive for end-users, so it would be nice to keep the place holders as well. Unfortunately that is not entirely possible, because you would end up with a document containing both a place holder and a document variable field. And that would in the end result in values being repeated.
Still, we can retain the place holders for the end-users. Certainly for their initial editing of the "template". And even for adding new place holders to an existing word document that has already been "converted to using variables".
The code below shows how to go about it. I haven't included the "standard" Word automation parts of opening and closing Word and/or a document. I start from the point where you have used the template to open a new document and are now ready to start replacing values. The solution then lies in three methods:
The HideExistingFieldCodes is needed because we use the place holders as names for the document variables as well and if the fields are showing their codes instead of their values, we end up replacing the document variable name with a new document variable and that will probably make Word barf loudly.
Once the codes of any existing fields have been hidden, we can scan the document for place holders and replace each with a document variable field.
I used a TValueListEditor (named PlaceHoldersEdit) to hold the place holder names and values. Its first row contains the captions of the columns in this control and is skipped. After that it is a matter of looping over all the place holders and for each looping over its occurences in the Word document.
Then finally we can start adding the actual document variables which will hold the values for the document variable fields. (Oh how I love all these similar sounding names.)
In this method I loop over all document variable fields and extract the variable name from each fields' code. Then I try to find a document variable of that name. If it doesn't exist yet, I add it. If it does exist I change its value. If performance is a concern and a variable name is used in many fields, this could be optimised a bit to change the value only once.
Oh, I have tested with a "saved" document, but I haven't gone as far as adding new place holders to a document that has already been through converting place holders to document variable fields. So, you might still have to smooth things out a bit.
By the way: In Word, Shft-F9 toggles a field between showing its code and showing its value. You will have to use the Options dialog (Word2003, don't know where the ended up in later versions) to show/hide them all in one fell swoop. The option is "Field codes" under "Show" or "Display" on the "Display" or "View" tab (translated from Dutch).