Read text(fields) from a word-document


#1

Hello there,

I’m a Dutch developer, evaluating apose.Word for possible usage in our next project.
What I need to know is how I can read pieces of text out of a word-document?
Do I need to do this with form-fields? Are there other possibilities?
I just want to make some Word template document with form-fields, or sections or whatever
sor of possible ‘text blocks’.
I just need to be able to read out those text blocks (like form fields).

Is this possible? And if so, how? Is need some example code for this. I couldn’t find it in the demo’s.
-------------------------------------------------

So far, I tried this code below, but I get an error message 'System.Exception: Cannot find a document field that wraps this form field.'

I’ve just created some test document in Word2002 with some (text)formfields in it (and 1 checkbox-formfield).

Dim word As word = New word
Dim doc As Document = word.Open(MapPath(".") + “…\Documents\testform.doc”) 'word.Open(“d:\docs\testform.doc”)

Dim s As String = doc.Range.Text() ’ goes fine
Dim i As Integer = doc.Range.FormFields.Count ’ error on this line
Dim s3 As String = doc.Range.FormFields(“Text1”).Result ’ also error on this line


regards,
Martin
----------


#2

Hi Martin,

1. If you only need to retrieve small pieces of text and form fields are suitable for your purpose, then yes, you can use form fields and retrieve data from them using Aspose.Word. Your code is correct. The exception is thrown probably because of some undocumented feature or combination of data in the document that Aspose.Word does not yet support. Please email the document to word@aspose.com for examination.

2. If you need to search and retrieve bigger blocks of text (that cannot fit into form fields) you can use Range.Text to retrieve text of a document (Document.Range.Text) or a section Document.Sections[0].Range.Text and then search inside the returned string.

3. Later we will introduce better find and replace API because it was requested by so many customers. It might be available some time in August, but with Aspose licensing policy you will be able to upgrade for free at any time.


#3

Hi Martin,

Aspose.Word 1.6.1 with a fix is available http://www.aspose.com/forums/ShowPost.aspx?PostID=8134#8225.


#4

Hi Martin.

I just came through your problem.

1. You can have bookmark in the document.

ex., here i have given a bookmark name as BMFirstName, u can access the text in the bookmark by Doc.Bookmarks.item(bookmark name).range.text

Dim WordDoc As New Word.Application()
Dim Doc As New Word.Document()

Doc = WordDoc.Documents.Open(StrResfile)

TxtFirstName.Text = Trim( Doc.Bookmarks.Item(“BMFirstName”).Range.Text)

2. You can read complete document text by

doc.range.text

Note. close the word,document while unloading ur form

i have done plenty with vb/vb.net with msword / msexcell. u can ask me help at any stage. Don’t hesitate to ask more


Regards,
Thirugnanam / .net Developer India
vkthirugnanam@yahoo.com