MS Word Automation - Check for Bookmark

I use automation to print Production Certificates in Word. In some cases i would like to check if a certain bookmark is present in the Word document. I use the following code to export data to Word: NameBookmark := ‘BookmarkA’; WordBookmark := wdDoc.Bookmarks.Item(NameBookmark); WordBookmark.Select(); WordBookmark.Range.Text := ‘Test’; When BookmarkA is not present naturally an error appears. [?]How can i check for the presence of the bookmark? Thanks in advance, Marcel Bierens

Hi Perhaps something like this: NameBookmark := 'BookmarkA'; i := wdDoc.Bookmarks.Count; found:= False; if i > 0 then begin x:= 1; repeat WordBookmark := wdDoc.Bookmarks.Item(x); WordBookmark.Select(); // I'm not sure. It should work. If Format(wdDoc.Bookmarks.Item()) = NameBookmark then found := true; // x := x +1; until (x > i) or (found = True); end; if found = true then WordBookmark.Range.Text := 'Test'; As always: without testing it [;)]. bye André

Hi Idefix. I think this should work. BookmarkName := ‘BookmarkName’; IF WordApp.ActiveDocument.Bookmarks.Exists(BookmarkName) THEN …

Both André and Gudmundur thanks for your reply to my question. I quickly tried the solution Gudmundur sugested and it works fine. Thanks, Marcel

Hi Ok! Gudmundur’s solution is a little bit shorter. But only 14 lines or about 500 letters [}:)]. So the difference is hard to see [8D]. Good job Gudmundur. bye André