Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Adding Bookmark in MS Word OLE

Posted on 2010-11-16
5
Medium Priority
?
1,993 Views
Last Modified: 2012-05-10
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);

// ======================================================================
0
Comment
Question by:DPINCGK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 

Author Comment

by:DPINCGK
ID: 34152604
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 :).
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 34153741
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);
0
 

Author Comment

by:DPINCGK
ID: 34156686
Thank you for the suggestion Graham, I tried OleProcedure but it creates the same result - throws an exception.
0
 

Accepted Solution

by:
DPINCGK earned 0 total points
ID: 34158552
I found the answer.

// OLD way - DOES NOT WORK
vBookmarks.OleProcedure("Add", (Variant)asBookMk, vRangeTest);

// NEW way - WORKS (must be done using automation interface classes from SysVari.h, which
// include class Procedure, Function, PropertyGet, PropertySet, and the function ::Exec()

Variant vRangeTest  = (get or define a range here)

Procedure BookMarkAddProc("Add");
BookMarkAddProc << asBookMk;
BookMarkAddProc << vRangeTest;
vBookmarks.Exec(BookMarkAddProc);
0
 

Author Closing Comment

by:DPINCGK
ID: 34186427
I found the solution myself - hope this helps other people if they have the same issue with Borland C++ Builder.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question