Jean,
Its C++ code. But its similar to how you would call your VBA code. The
issue is get_Index is giving me same values for different sections!!
and so i keep getting
"SECTIONLOOKUP: Section 2, Bookmark1 already in map!!!!"
"SECTIONLOOKUP: Section 2, Bookmark4 already in map!!!!"
"SECTIONLOOKUP: Section 2, Bookmark6 already in map!!!!"
The code is below...
Thanks for responding,
-Jose
/*
m_SectionLookup is a Map
m_pSections is a SectionsPtr
*/
void WordHelper::CreateSection(CString csBookMarkName, bool bReadOnly,
Word::SectionsPtr m_pSections)
{
HRESULT hr;
Word::SectionPtr m_pSection;
Word::_DocumentPtr m_pDoc;
Word::RangePtr sectionRangePtr;
Word::BookmarksPtr m_pBookMarks;
Word::BookmarkPtr m_pBookMark;
hr = m_pWord->get_ActiveDocument(&m_pDoc);
if (SUCCEEDED(hr))
{
hr = m_pDoc->get_Bookmarks(&m_pBookMarks);
if (SUCCEEDED (hr))
{
hr = m_pBookMarks->Item (&_variant_t(csBookMarkName),
&m_pBookMark);
if (SUCCEEDED(hr))
{
m_pBookMark->get_Range(§ionRangePtr);
long dPos, sPos;
hr = sectionRangePtr->get_End(&dPos);
hr = sectionRangePtr->get_Start(&sPos);
/* Add a section denoted by bReadOnly before this bookmark */
dPos+=1;
hr = sectionRangePtr->put_Start(dPos);
hr = sectionRangePtr->put_End(dPos);
hr = sectionRangePtr->get_End(&dPos);
hr = sectionRangePtr->get_Start(&sPos);
VARIANT vtRange;
vtRange.vt = VT_DISPATCH;
vtRange.pdispVal = sectionRangePtr;
hr = m_pSections->Add(&vtRange,&_variant_t((LONG)
wdSectionContinuous),&m_pSection);
if (SUCCEEDED(hr))
{
hr = m_pSection->get_Index (&nSectionIndex);
if (SUCCEEDED(hr))
{
if (m_SectionLookup.Lookup(nSectionIndex, bReadOnly))
{
_MyDebug ("SECTIONLOOKUP: Section %d, %s already in map!!!!",
nSectionIndex, csBookMarkName);
}
m_SectionLookup.SetAt(nSectionIndex, bReadOnly);
_MyDebug ("SECTIONDBG: %s, Section %d, %s, %X", bReadOnly ?
"readonly" : "readwrite", nSectionIndex, csBookMarkName, m_pSection);
} //get_Index if (SUCCEEDED(hr))
}// Add if (SUCCEEDED(hr))
LONG lCount;
hr = m_pSections->get_Count(&lCount);
if (SUCCEEDED(hr))
_MyDebug ("m_pSections->get_Count %d", lCount);
else
_MyDebug ("m_pSections->get_Count FAILED");
} // Item if (SUCCEEDED(hr))
else
_MyDebug ("CreateSection: Cant Find bookmark %s", csBookMarkName);
}// get_Bookmarks if (SUCCEEDED(hr))
else
_MyDebug ("CreateSection: Failed get_Bookmarks");
}
}