Avatar of DPINCGK
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.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);

// ======================================================================
Microsoft WordCC#

Avatar of undefined
Last Comment
DPINCGK

8/22/2022 - Mon
DPINCGK

ASKER
By the way, one additional comment, the program throws an EOleSysError exception. Value  0x80020009,

which takes me to line 1852 of sysvari.h

  template <class P1, class P2>
  Variant Variant::OleFunction(const String& name, P1 p1, P2 p2)
  {
    TAutoArgs<2> args;
    args[1] = p1;  args[2] = p2;
    return OleFunction(name, static_cast<TAutoArgsBase*>(&args));     // this line!!
  }

I'm sorry to admit but I'm not entirely certain how to solve this one. Any help would be greatly appreciated :).
GrahamSkan

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("Add", (Variant)"TestName", vTenCharRange);
DPINCGK

ASKER
Thank you for the suggestion Graham, I tried OleProcedure but it creates the same result - throws an exception.
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
DPINCGK

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
DPINCGK

ASKER
I found the solution myself - hope this helps other people if they have the same issue with Borland C++ Builder.