About ActiveDocument.Fields

Dear Staff, my corporation have commissioned me to find on the web a component which can be used on a web application to run some code that we actually have on a Word macro. I believe your Aspose.Word is very powerful one and I am testing it to replace the above macro. But I have some problems in translating the macro source code in a VB.NET (or C#) with your component.

That is: in the macro we search and do some read actions from the ActiveDocument.Fields collections to find specified fields name and content. Well I have tried some code that have been posted in this forum (inheriting IDocumentVisitor class [this class don’t exist even if I import Aspose.Word] or using the “new DocumentVisitor” sintax) but the code didn’t work.

How can I enumerate and use the equivalent of ActiveDocument.Fields with your component?

I tried also to watch the Document.Variables collection but it’s ever empty…

Best regards!

Hi,

Thank you for your interest in Aspose.Word.

Good point. Aspose.Word object model provides classes for most of the Word document elements such as sections, paragraphs, tables, and many others. We also recently exposed the Field class which represents a MS Word field - but due to some reason the functionality of this class is currently limited and it does not allow reading field code and result. We will improve it later so it will be applicable for this purpose.

At the moment, you should implement your own logic to select fields from the document and read their contents. As you might know, a Word field normally contains of three parts - field start, field separator, and field end. In the Aspose.Word API, these objects are represented by the FieldStart, FieldSeparator, and FieldEnd classes, respectively. Field code is normally represented by a sequence of text runs enclosed between field start and field separator; field result is also a sequence of text runs enclosed between field separator and field end.

Until the Field class gets fully functional, you have to select field starts from the document and move through their siblings (nodes located at the same level) until the field end is met. We have a ready code sample that uses the mentioned approach to make it easier to work with hyperlinks in a document (hyperlinks are actually fields whose field code represents the hyperlink target and field result represents the hyperlink name). Take a look how the Hyperlink class works, pay special attention to its constructor where the field nodes are scanned to extract the code and result:

https://docs.aspose.com/words/net/working-with-hyperlinks-and-html/

Feel free to post your further questions here.

Hi, I have tried the sample code enumerating the doc.SelectNodes("//FieldStart") and writing in the output ASP.NET page the Count property of that collection. The result is very strange and incorrect: the Winword macro says that in the document there are 2479 ActiveDocument.Fields, while the doc.SelectNodes("//FieldStart").Count returns only 1613. This Word document is a very large file (also more than 5MB), there is some dimension limitation?

Sorry for the delay, looking at this right now.

Please zip your document and attach here and I will see how Word count of fields is different from ours.