开发者

Problem using Interop Range.Find when range contains table

I'm trying to write a word add-in (with C#) that searches a document for all occurrences of certain pieces of text and makes some changes to the sections of text it finds.

I've created a loop that uses Range.Find to get all of the ranges in the document that contain a piece of text and the use the range objects it returns to do the manipulation later. A problem comes up when there is a table in the document, though.

In my first attempt at this, I just kept creating a new range, from the end of my last found occurrence to the end of the document and then searching again in that new range until it returns no found values. When I did this with a document containing a table, it just got stuck inside the table and created an infinite loop.

Then, I found this article: http://www.codeproject.com/KB/office/wordaddinpart1.aspx, and when using the Find function the article describes, it successfully continues on through a table, but开发者_Python百科 unfortunately doesn't successfully grab all of the values within that table, which I need it to do.

Does anyone have any advice about getting around this problem? I've seen a couple people talk about having this problem, but no solutions.


I would suggest using the OpenXml SDK for this. The Office interop is a relic. Here's an article that explains how to use the OpenXml SDK to search a Word document:

http://msdn.microsoft.com/en-us/library/bb508261.aspx

Here's an SO question that discusses how to replace an image in a Word document using the OpenXml SDK:

Replace image in word doc using OpenXML

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜