Need help replacing Bookmars

  • Thread starter Travis Lingenfelder
  • Start date
T

Travis Lingenfelder

I am creating an application that uses Word to producce reports using COM
Interop. I have created a Word template that contains named bookmarks
everywhere I want data to be displayed. When I execute the component in my
application that produces the report the replaced text within the bookmark is
not being displayed.

The bookmarks are being changed from a Named bookmark to a field. If I
right-click where the bookmark sould be I can click Toggle field codes and
the data for the field is something like"{ insertedText }".

How can I get the insertedText to display properly?
 
J

Jean-Guy Marcil

Travis Lingenfelder was telling us:
Travis Lingenfelder nous racontait que :
I am creating an application that uses Word to producce reports using
COM Interop. I have created a Word template that contains named
bookmarks everywhere I want data to be displayed. When I execute the
component in my application that produces the report the replaced
text within the bookmark is not being displayed.

The bookmarks are being changed from a Named bookmark to a field. If
I right-click where the bookmark sould be I can click Toggle field
codes and the data for the field is something like"{ insertedText }".

It would be most helpful if you posted the relevant part of the code that
are inserting the text at the bookmarked areas.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

Chip Orange

It sounds like you're not updating the text in the bookmark properly. You
should be using a line like the below (for the simplest case):

ActiveDocument.Bookmarks("MyBookMarkName").Range.Text = "new text which
overwrites the previous text"

The above however will delete the bookmark after it's updated; if you will
need to reference it by name after the update, you'll need a general purpose
subroutine for updating bookmarks which leaves them in place, like the one
below:

Public Sub UpdateBookmark(ByVal BookmarkToUpdate As String, ByVal TextToUse
As String)
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
BMRange.Text = TextToUse
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub


hth,


Chip
 
T

Travis Lingenfelder

Sorry, I meant to post the code with my previous post.

Here is the procedure to replace the text in a Named Bookmark

private void BookMarkReplaceRange(string bookmarkName, string newText)
{
object oBookmarkName = bookmarkName;
Word.Range rng = wrdDoc.Bookmarks.get_Item(ref oBookmarkName).Range;
rng.Text = newText;
object oRng = rng;
wrdDoc.Bookmarks.Add(bookmarkName, ref oRng);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top