简体   繁体   English

Delphi 2010:从Word 2010文档中读取文本

[英]Delphi 2010: Read text from Word 2010 document

I have an application that inserts information into a Word Document from a database. 我有一个应用程序将信息从数据库插入Word文档。

I have had a request that the information in the Word Document can be changed at a later date. 我已经请求Word文档中的信息可以在以后更改。

In order to achieve that I have written a macro that launches my application which can then read the bookmarks in the document to find the sections in the document. 为了实现这一点,我编写了一个启动我的应用程序的宏,然后可以读取文档中的书签以查找文档中的部分。

I am stumbling with being able to read the text that has been set at a particular bookmark. 我对能够阅读已在特定书签上设置的文本感到磕磕绊绊。

I can set the text of the bookmark using the following code: 我可以使用以下代码设置书签的文本:

bookmark := 'PersonName';
R := WordDoc.Bookmarks.Item(bookmark).Range;
R.Text := "Joe Bloggs";

Part of the issue is that the bookmark is a not set as a block of text - this ensures that when the text is set the Bookmark persists - which means that at least I can set the range at second run. 部分问题是书签未被设置为文本块 - 这确保了在设置文本时书签仍然存在 - 这意味着至少我可以在第二次运行时设置范围。

Try as I might I cannot find a way of getting any result back for either 尝试我可能无法找到任何结果的方法

R.text

or 要么

WordApp.Selection.Text

when using the bookmark to set the starting point. 使用书签设置起点时。

Any suggestions? 有什么建议么?

Inserting text at the second run works fine. 在第二次运行时插入文本工作正常。

Try readding the bookmark after setting the text: 设置文本后尝试读取书签:

Bookmark := 'PersonName';
R := WordDoc.Bookmarks.Item(Bookmark).Range;
R.Text := 'Joe Bloggs';
WordDoc.Bookmarks.Add(Bookmark, R);

See here for more info 有关详细信息,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM