DPINCGK
asked on
Adding Bookmark in MS Word OLE
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.Bo okMarks.Ad d( 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.appli cation");
}
catch(...)
{
try
{
vWordApplication = Variant::CreateObject("wor d.applicat ion");
}
catch(...)
{
// if we got here, we were unable to create new Word object
return;
}
}
// get the documents collection
Variant vDocuments = vWordApplication.OleProper tyGet("Doc uments");
// open the document in question
Variant vDocument = vDocuments.OleFunction("Op en", FileNameString);
// make Word visible to the user
vWordApplication.OleProper tySet("Vis ible", (Variant)True);
// get a Range object of characters 1 through 10
Variant vTenCharRange = vDocument.OleFunction("Ran ge", (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("Ad d", (Variant)"TestName");
// ALSO NOT WORKING
vBookMarks.OleFunction("Ad d", (Variant)"TestName", vTenCharRange);
// ========================== ========== ========== ========== ========== ====
// ==========================
Variant vWordApplication;
try
{
vWordApplication = Variant::GetActiveObject("
}
catch(...)
{
try
{
vWordApplication = Variant::CreateObject("wor
}
catch(...)
{
// if we got here, we were unable to create new Word object
return;
}
}
// get the documents collection
Variant vDocuments = vWordApplication.OleProper
// open the document in question
Variant vDocument = vDocuments.OleFunction("Op
// make Word visible to the user
vWordApplication.OleProper
// get a Range object of characters 1 through 10
Variant vTenCharRange = vDocument.OleFunction("Ran
// get the Document.BookMarks property collection
Variant vBookMarks = vDocument.OlePropertyGet("
// now - add a new bookmark in! Nothing seems to work.
// NOT WORKING
vBookMarks.OleFunction("Ad
// ALSO NOT WORKING
vBookMarks.OleFunction("Ad
// ==========================
Not used C++ or seen Builder, but looking at the sample here that adds a table:
http://bcbjournal.org/articles/vol3/9902/Word_97_OLE_Automation.htm?PHPSESSID=ac4e5a061dd38996dceb88c55b308f21 ,
I would suggest:
vBookMarks.OleProcedure("A dd", (Variant)"TestName", vTenCharRange);
http://bcbjournal.org/articles/vol3/9902/Word_97_OLE_Automation.htm?PHPSESSID=ac4e5a061dd38996dceb88c55b308f21 ,
I would suggest:
vBookMarks.OleProcedure("A
ASKER
Thank you for the suggestion Graham, I tried OleProcedure but it creates the same result - throws an exception.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I found the solution myself - hope this helps other people if they have the same issue with Borland C++ Builder.
ASKER
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 :).