none
How to Bookmark a Range? RRS feed

  • Question

  • Hi,

    I am doing word automation using mfc. I would like to bookmark a Range programmatically, but there is a problem. I tried to do this by calling Bookmarks.add method. This method takes two parameters, a LPCTSTR as its first parameter and a VARIANT* as its second parameter. In fact, the problem is that I don't know what to give as the second parameter to this method. Any help would be appriciated.

    Regards,

    Hamidreza Ghader

    Wednesday, December 29, 2010 10:23 AM

Answers

  • I have tried to give a Range object as the second parameter of the Bookmarks.Add() method, but it gives me  compile error telling: "cannot convert parameter 2 from 'class msword::Range' to 'struct tagVARIANT *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called"

    As I told in my first post I do automation using MFC in C++. I don't know how to pass a Range object as VARIANT*.


    Hi Hamidreza

    I'm afraid I'm not familiar with C++, but I have some experience with C#. Perhaps if I explain how we have to do it for C#, that will help...

    Office allows optional parameters for methods. C# (until very recently) doesn't support that - all parameters have to be passed. Due to the way Word want's receive optional parameters, C# can't just pass something as (for example) Word.Range. Instead, it first needs to be cast as an object:
        Word.Range rng = wdApp.Selection.Range;
        object oRng = rng;

    And this then must be passed "by ref":
        doc.Bookmarks.Add("Name", ref oRng);

    Not sure whether this helps you...?

    See, also, perhaps:
    http://www.codeguru.com/forum/printthread.php?t=410422&pp=15&page=3 (the message from alex_gusev dated January 11 2007)

    and:
    http://msdn.microsoft.com/de-de/library/microsoft.office.interop.word.bookmarks.add(office.11).aspx


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Hamidreza G Thursday, December 30, 2010 7:52 AM
    • Unmarked as answer by Hamidreza G Thursday, December 30, 2010 7:52 AM
    • Marked as answer by Hamidreza G Thursday, December 30, 2010 7:54 AM
    Thursday, December 30, 2010 7:01 AM
    Moderator

All replies

  • The easiest way to find out is to look up Bookmarks.Add in the Word VBA help. The syntax is

    Bookmarks.Add(Name, Range)

    The first argument is the name for the new bookmark, and the second argument is the range for the bookmark. The range is a Range object that can be specified as the Range of the current Selection or by referring to a specific paragraph, etc.


    Regards, Hans Vogelaar
    Wednesday, December 29, 2010 3:52 PM
  • Hi,

    I have tried to give a Range object as the second parameter of the Bookmarks.Add() method, but it gives me  compile error telling: "cannot convert parameter 2 from 'class msword::Range' to 'struct tagVARIANT *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called"

    As I told in my first post I do automation using MFC in C++. I don't know how to pass a Range object as VARIANT*.

     

    Thursday, December 30, 2010 6:18 AM
  • I have tried to give a Range object as the second parameter of the Bookmarks.Add() method, but it gives me  compile error telling: "cannot convert parameter 2 from 'class msword::Range' to 'struct tagVARIANT *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called"

    As I told in my first post I do automation using MFC in C++. I don't know how to pass a Range object as VARIANT*.


    Hi Hamidreza

    I'm afraid I'm not familiar with C++, but I have some experience with C#. Perhaps if I explain how we have to do it for C#, that will help...

    Office allows optional parameters for methods. C# (until very recently) doesn't support that - all parameters have to be passed. Due to the way Word want's receive optional parameters, C# can't just pass something as (for example) Word.Range. Instead, it first needs to be cast as an object:
        Word.Range rng = wdApp.Selection.Range;
        object oRng = rng;

    And this then must be passed "by ref":
        doc.Bookmarks.Add("Name", ref oRng);

    Not sure whether this helps you...?

    See, also, perhaps:
    http://www.codeguru.com/forum/printthread.php?t=410422&pp=15&page=3 (the message from alex_gusev dated January 11 2007)

    and:
    http://msdn.microsoft.com/de-de/library/microsoft.office.interop.word.bookmarks.add(office.11).aspx


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Hamidreza G Thursday, December 30, 2010 7:52 AM
    • Unmarked as answer by Hamidreza G Thursday, December 30, 2010 7:52 AM
    • Marked as answer by Hamidreza G Thursday, December 30, 2010 7:54 AM
    Thursday, December 30, 2010 7:01 AM
    Moderator
  • Dear Cindy,

    Thank you for the reply, the link of codeguru has the answer of my question.

    Regards,

    Hamidreza Ghader

    Thursday, December 30, 2010 7:58 AM