Mismatch b/w Bookmark Id and Bookmark Count

S

Sankalp

Hi,

I have a pop-up menu which is used for removing a bookmarks. When I go
through Word's Bookmark menu, I am able to see all the bookmarks. At the
time of deleting a bookmark through the custom pop-up menu I am also able to
get a valid BookmarkId(say 3). When I try to get the size of the Bookmarks
collection I am returned a size of only one (though more than one bookmark
exists). As a result I am not able to refer to the bookmark I need to
delete.

Is there anything I can do about this? or am I doing something wrong. Why is
this behaviour?

Code below:
HRESULT hr = S_OK;
CComBSTR bsCaption;
CComBSTR bsCompare;
CComPtr<Word::Selection> pSelCurrent;
CComPtr<Word::Bookmarks> pBMColl;
CComPtr<Word::Bookmark> pBMToDel;

hr = Btn->get_Caption(&bsCaption);
bsCompare.LoadString(IDS_BUTTON_POPUP_REMOVETAG);
if (_bstr_t(bsCaption) == _bstr_t(bsCompare)) {
hr = m_pWordApp->get_Selection(&pSelCurrent);
long lngBMIdx = -1;
hr = pSelCurrent->get_BookmarkID(&lngBMIdx); //returns an id of 3
if (lngBMIdx > 0) {
hr = pSelCurrent->get_Bookmarks(&pBMColl);
hr = pBMColl->put_ShowHidden(VARIANT_TRUE);
hr = pBMColl->put_DefaultSorting(Word::wdSortByName);
long lngCount = -1;
hr = pBMColl->get_Count(&lngCount); // returns 1
hr = pBMColl->Item(&CComVariant(lngBMIdx), &pBMToDel); //
this fails
if (NULL != pBMToDel.p) {
hr = pBMToDel->Delete();
}
}
}

Thanks,
Sankalp
 
S

Sankalp

Hi,
Just figured it out.
Getting the bookmarks collection from the selection, will only give the
bookmark(s) that is currently selected. Instead we need the bookmarks for
the complete document. This needs to be fetched through the ActiveDocument
object', Bookmarks collection and NOT through the Selection object'
Bookmarks collection.

Regards,
Sankalp


Sankalp said:
Hi,

I have a pop-up menu which is used for removing a bookmarks. When I go
through Word's Bookmark menu, I am able to see all the bookmarks. At the
time of deleting a bookmark through the custom pop-up menu I am also able to
get a valid BookmarkId(say 3). When I try to get the size of the Bookmarks
collection I am returned a size of only one (though more than one bookmark
exists). As a result I am not able to refer to the bookmark I need to
delete.

Is there anything I can do about this? or am I doing something wrong. Why is
this behaviour?

Code below:
HRESULT hr = S_OK;
CComBSTR bsCaption;
CComBSTR bsCompare;
CComPtr<Word::Selection> pSelCurrent;
CComPtr<Word::Bookmarks> pBMColl;
CComPtr<Word::Bookmark> pBMToDel;
CComPtr said:
hr = Btn->get_Caption(&bsCaption);
bsCompare.LoadString(IDS_BUTTON_POPUP_REMOVETAG);
if (_bstr_t(bsCaption) == _bstr_t(bsCompare)) {
hr = m_pWordApp->get_Selection(&pSelCurrent);
hr = m_pWordApp->get_ActiveDocument(&pActDoc); //NEW
long lngBMIdx = -1;
hr = pSelCurrent->get_BookmarkID(&lngBMIdx); //returns an id of 3
if (lngBMIdx > 0) {
//hr = pSelCurrent->get_Bokmarks(&pBMColl); // this does
not give the desired result
hr = pActDoc->get_Bookmarks(&pBMColl); // this is the
way to go
hr = pActDoc->get_Bookmarks(&pBMColl);
 

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