Word Section Index Problem

J

Jose

Hello Every one,


This is really bugging me :)

I Add a Section at a range by the code:

hr = m_pDoc->get_Sections(&m_pSections);

hr = m_pSections->Add(&vtRange,&_variant_t((LONG)
wdSectionContinuous),&m_pSection); //I then add many sections

m_pSection now has the reference to the section it just created.

hr = m_pSection->get_Index (&nSectionIndex); // I then ask for the
index of the range in the document.


Surprisingly i get similar values for different m_pSection !!!!



This is funny

Do help,

Jose Chen
(Wannabe Word MVP)
 
J

Jean-Guy Marcil

Jose was telling us:
Jose nous racontait que :
Hello Every one,


This is really bugging me :)

I Add a Section at a range by the code:

hr = m_pDoc->get_Sections(&m_pSections);

hr = m_pSections->Add(&vtRange,&_variant_t((LONG)
wdSectionContinuous),&m_pSection); //I then add many sections

m_pSection now has the reference to the section it just created.

hr = m_pSection->get_Index (&nSectionIndex); // I then ask for the
index of the range in the document.


Surprisingly i get similar values for different m_pSection !!!!

Can you post the actual VBA code you are using?

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

Jose

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(&sectionRangePtr);

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");
}
}
 
J

Jean-Guy Marcil

Jose was telling us:
Jose nous racontait que :
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(&sectionRangePtr);

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");
}
}

I can read your code and get a general idea of what it is doing, but I
cannot debug it as I do not know anything about C++ (Just a little about C#,
which is quite different!)

Have you checked the result to see if the document actually has all the new
sections as it should? Maybe it is a problem with the error trapping itself,
not with the execution.

If not, make sure that you are properly clearing the range you are using to
add the section break. Try not to use the equivalent of the selection object
from VBA in your code.

Sorry I cannot be of more help.

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

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