Disappearing bookmarks

A

Alex

Hello,

I've encountered strange bookmark behaviour.

The code is in C# but it should not matter:

// myWordApp variable points to the Application object
// myBookmark variable points to a bookmark.
myBookmark.Select();
Word.Selection selection = myWordApp.ActiveWindow.Selection;
selection.Text = "\xD6";

So far so good, but if I do:

selection.Font.Name = "Wingdings";

or

selection.InsertParagraphAfter();

The bookmark disappears.

Can anyone explain what's going on and how to overcome this issue?

Thanks,
Alex.
 
A

Alex

Sorry, a correction - the bookmark disappears after changing the text.

// myWordApp variable points to the Application object
// myBookmark variable points to a bookmark.
myBookmark.Select();
Word.Selection selection = myWordApp.ActiveWindow.Selection;
selection.Text = "\xD6"; // the bookmark disappears.

Anyway, found the answer:
http://word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm

Thanks,
Alex.
 
P

papagru

I apologize for this stupid way to break in but I can't seem to post the
civilized way. When I click on new, the posting screen says I have not put
in my profile, but I have.
What am I doing wrong?
papagru
 
W

Word Heretic

G'day "Alex" <[email protected]>,

Don't select the bookmark, work with the bookmark's range. Anyway,
this is a known issue, the easiest way is to re-make the bookmark on
your new range.

Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


Alex reckoned:
 
A

Alex

Hello Steve,

Word Heretic said:
G'day "Alex" <[email protected]>,

Don't select the bookmark, work with the bookmark's range. Anyway,
this is a known issue, the easiest way is to re-make the bookmark on
your new range.

The reason that I select the bookmark is because this code is a special case of an operation that works with the current selection.

So I have Func() { /* do something with the current selection */ }
and I reuse it by selecting the bookmark, calling Func() and doing some bookmark-specific stuff later.


Best wishes,
Alex.
 
W

Word Heretic

G'day "Alex" <[email protected]>,

Change that func to use a range. When you need to call it from
selection (avoided where possible) call it with Selection.Range

Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


Alex reckoned:
 
A

Alex

Hello Steve,

Word Heretic said:
Change that func to use a range. When you need to call it from
selection (avoided where possible) call it with Selection.Range

Refactoring. Good idea.
 
W

Word Heretic

G'day "Alex" <[email protected]>,

The term - refactoring - had me confused a few months ago as to what
the hell it was. Then I discovered it was basically what I had already
written a book about - optimising and cleaning up yer code :)

Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


Alex reckoned:
 

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