I'm trying to add a bookmark into an open MS Word file in code. I'm using C++ Builder 5 (I also have C++ Builder 2009). This should work via the Word.Documents.Document.BookMarks.Add( Name , Range) function but I'm having problems. Wondering if anyone can point me in the direction where I'm going wrong. Here's my code:
// ========================================================================
Variant vWordApplication;
try
{
vWordApplication = Variant::GetActiveObject("word.application");
}
catch(...)
{
try
{
vWordApplication = Variant::CreateObject("word.application");
}
catch(...)
{
// if we got here, we were unable to create new Word object
return;
}
}
// get the documents collection
Variant vDocuments = vWordApplication.OlePropertyGet("Documents");
// open the document in question
Variant vDocument = vDocuments.OleFunction("Open", FileNameString);
// make Word visible to the user
vWordApplication.OlePropertySet("Visible", (Variant)True);
// get a Range object of characters 1 through 10
Variant vTenCharRange = vDocument.OleFunction("Range", (Variant)1, (Variant)10);
// get the Document.BookMarks property collection
Variant vBookMarks = vDocument.OlePropertyGet("BookMarks");
// now - add a new bookmark in! Nothing seems to work.
// NOT WORKING
vBookMarks.OleFunction("Add", (Variant)"TestName");
// ALSO NOT WORKING
vBookMarks.OleFunction("Add", (Variant)"TestName", vTenCharRange);
// ======================================================================
which takes me to line 1852 of sysvari.h
template <class P1, class P2>
Variant Variant::OleFunction(const
{
TAutoArgs<2> args;
args[1] = p1; args[2] = p2;
return OleFunction(name, static_cast<TAutoArgsBase*
}
I'm sorry to admit but I'm not entirely certain how to solve this one. Any help would be greatly appreciated :).