Read a value from Word with automation

For a customer i’ve developed a quote in Word (Attain 3.01). With automation the whole quote will be transferred to a word-document. I use bookmarks to transfer f.e. the customer-name, address, place. Our customer wants to change some of these values and transfer them back to Navision Attain. I’ve used the following code for this: _WordBookmark := WdDoc.Bookmarks.Item(_NameBookmark); _wdRange := _WordBookmark.Range; _wdRange.SetRange(1,30); _Text := _wdRange.Text; This works when the text in Word is 30 character long. When this is 10 characters long the other 20 characters will be read from an other Word-line. I can’t get the end-position of the bookmark. Does someone know a solution for this. Must i locate positions in the Worddocument when transferring the quote from Navision to Word? When yes, how do i do that?

When you write in the Word document you must pass TypeParagraph function as well, then i think you will not face any problem in reading the line, i did in the same way but a long time back, try it otherwise let me know i will test it again.

Another possibility is to use ‘select’ as in the following VB code: Dim newtext as String ActiveDocument.Bookmarks.Item(“test”).Select newtext = Selection.Text

Thanks for the answers but this isn’t the solution. You can use Typeparagraph when a line in Word only has 1 bookmarks. When there are more it doesn’t work. I tried to use ‘select’ but this doesn’t work either. A bookmark is just a begin-position of a text. You don’t know where the end-position is. Maybe Word has something you give in a begin- and end-position and lock this area. Then you know where to read from Attain in a Word-document. I hope someone can help me further.

A bookmark is not just a position-marker in the text. It actually can contain a selection of text. I tested the construction with Select and it worked fine. I created a word document, select part of the text and define a bookmark, then select another part of the text and define a second bookmark. Here is the Navision code: DocName := ‘c:\temp\wordtest.doc’; BookmarkName := ‘Test1’; wdDoc := wdApp.Documents.Open(DocName); wdApp.ActiveDocument.Bookmarks.Item(BookmarkName).Select; ResText := wdApp.Selection.Text; BookmarkName := ‘Test2’; wdApp.ActiveDocument.Bookmarks.Item(BookmarkName).Select; ResText := ResText + ’ /Text 2: ’ + wdApp.Selection.Text; When i run this code text variable ResText contains the expected results. However, it only works when you make sure the required text is contained in the selection. If the selection becomes empty then the bookmark is nothing more than a postion marker in the text. An alternative may be the use of document variables and require of the user that all data that must be transferred back to navision will be entered by variables (e.g. using forms).

Thanks Wouter, You’re right about the bookmarks when using them good. When the Word-document is generated by Navision it’s possible the user expandes a value of a bookmark. When reading the value of the bookmark back into Navision the old value will be given. The expanded part is not a part of the bookmark. When the user select the complete changed value and make that the bookmark then there nothing wrong. This often is forgotten. I search further for a better method.