Free Support Forum - aspose.com

How to retrieve/read data from word template

Hi:

I know how to navigate to a table, row, and cell and write text into it but how can I do the opposite? I want to open the word document which is already populated and read say table(0) row (1) and cell (0) and assign the value of what is in that cell to a variable. Any code examples would be great! Thanks.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. I think that you can try using ToTxt method. Please see the following code:

Dim value As String = doc.FirstSection.Body.Tables(0).Rows(1).Cells(0).ToTxt()

Hope this helps.

Best regards.

I tried that what you suggested, it works but the problem is the value contains some weird characters at the end. I am using a licensed Aspose.Words by the way. In the word template, the cell has "K222" filled in with no empty spaces or anything else but when I use the ToTxt() it returned me "K222".

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. These characters are paragraph break and cell break. If you need just text then you can try using the following code:

Dim value As String = String.Empty

For Each run As Run In doc.FirstSection.Body.Tables(0).Rows(1).Cells(0).GetChildNodes(NodeType.Run, True)

value += run.Text

Next

Hope this helps.

Best regards.

Hi Alexey, thanks alot for your help! This works great - can I also ask you if it has to be in a for loop? How could I get the same result without having it as a for loop? Thanks again!

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. You can also just remove special characters in the string. See the following code:

Dim value As String = doc.FirstSection.Body.Tables(0).Rows(1).Cells(0).ToTxt().Trim()

Hope this helps.

Best regards.