none
Microsoft.Office.Interop.Word Selection.InsertFile() Method->How to use Bookmark in Range Parameter RRS feed

  • Question

  • We're doing an update to an MS Word Add-In that allows users to insert MS Word files into a central MS Word document by using the following function:

     //Created rng variable
     Microsoft.Office.Interop.Word.Range rng = Globals.ThisAddIn.Application.Selection.Range;
    //MS Interop method.
    Globals.ThisAddIn.Application.Selection.InsertFile(
         filename,
         ref missing,//<--Range - For Word can be a bookmark                              
         ref missing, //Confirm Conversions
         ref falsevalue, //Link file
         ref falsevalue //Attachment
    );

    The second parameter:

    ref missing, //Range - For Word can be a bookmark

    Seems to say that we can pass a Range parameter that can be a bookmark (which we can use to delete the added content, if needed), however we haven't been able to find any implementation examples that demonstrate how this is done.

    MSDN describes this parameter as follows:

    Range

    Type: System.Object

    Optional Object.

    If the specified file is a Word document, this parameter refers to a bookmark. If the file is another type (for example, a Microsoft Excel worksheet), this parameter refers to a named range or a cell range (for example, R1C1:R3C4).

    Since we want to insert a new bookmark with the text, we assumed that we need to:

    1. Create a new bookmark, and
    2. Pass it into the InsertFile parameter list

    However at this point, since we can't find any examples of how this is achieved, we're at a stopping point.

    We also found examples such as these:

    https://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.bookmark.bookmarks(v=vs.120).aspx

    http://www.c-sharpcorner.com/article/add-replace-and-remove-bookmarks-in-word-using-c-sharp/

    But it doesn't look like their implementations are using Word Interop or Selection.InsertFile().

    Intellisense doesn't seem to indicate a Range property on Microsoft.Office.Interop.Word.Bookmark:

    Thanks


    Wednesday, July 26, 2017 7:34 PM

Answers

  • Hi drdexter33,

    The range parameter is for source file instead of target file.

    If you specify the parameter as a bookmark name in document A, it will only insert content of the bookmark’s range in document A to document B.

    If you want to bookmark the added content, you need get the range of the added content.

    You could record two point, one is current selection point and then you move the selection to right one step, that is the second point.

    After inserting the file content to the first point, you could get the range of added content according to the two points.

    Here is  the example.

                Word.Range sel1 = Globals.ThisAddIn.Application.Selection.Range;//first point

                Globals.ThisAddIn.Application.Selection.MoveRight(Unit: Word.WdUnits.wdCharacter, Count: 1);//move to right

                Word.Range sel2 = Globals.ThisAddIn.Application.Selection.Range;//second point       

                sel1.InsertFile(FileName: filename, Link: false, Attachment: false);//inset file content to first point

                Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;

                Word.Range insertRange = doc.Range(Start: sel1.Start, End: sel2.Start-1);//get range of added content

                Word.Bookmark bm1 = doc.Bookmarks.Add("bm1", insertRange);//add bookmark

    Demonstration


    Best Regards,

    Terry

    • Marked as answer by drdexter33 Friday, July 28, 2017 3:49 PM
    Friday, July 28, 2017 9:17 AM

All replies

  • Hi drdexter33,

    I think Range in this method means an object which could refer to a Range in the document that you want to insert. For a word document, it will  be the name of a bookmark in the document.

    I have done a test for demonstration.

    Here is the document I want to insert into another document. I create three paragraphs  and  bookmarks for each paragraph


    When I try to insert the document without specifying range.

    Code:

    Globals.ThisAddIn.Application.Selection.InsertFile(FileName:filename,Link:false,Attachment:false);

    Result:


    When I try to insert the document and specify the range bookmark BK2.

    Code:

    Globals.ThisAddIn.Application.Selection.InsertFile(FileName:filename,Range: "BK2",Link:false,Attachment:false);

    Result:

    Best Regards,

    Terry

    Thursday, July 27, 2017 7:21 AM
  • Thanks Terry.

    However we've been unsuccessful creating a new bookmark programatically to use as a parameter

    Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;

    Microsoft.Office.Interop.Word.Bookmark bm1 = doc.Bookmarks.Add("bm1", doc.Paragraphs[1].Range); Globals.ThisAddIn.Application.Selection.InsertFile(FileName: filename, Range: bm1, Link: false, Attachment: false);


    The above example produces this error:

    {"Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"}



    • Edited by drdexter33 Thursday, July 27, 2017 1:21 PM
    Thursday, July 27, 2017 12:34 PM
  • Hi drdexter33,

    The range parameter is for source file instead of target file.

    If you specify the parameter as a bookmark name in document A, it will only insert content of the bookmark’s range in document A to document B.

    If you want to bookmark the added content, you need get the range of the added content.

    You could record two point, one is current selection point and then you move the selection to right one step, that is the second point.

    After inserting the file content to the first point, you could get the range of added content according to the two points.

    Here is  the example.

                Word.Range sel1 = Globals.ThisAddIn.Application.Selection.Range;//first point

                Globals.ThisAddIn.Application.Selection.MoveRight(Unit: Word.WdUnits.wdCharacter, Count: 1);//move to right

                Word.Range sel2 = Globals.ThisAddIn.Application.Selection.Range;//second point       

                sel1.InsertFile(FileName: filename, Link: false, Attachment: false);//inset file content to first point

                Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;

                Word.Range insertRange = doc.Range(Start: sel1.Start, End: sel2.Start-1);//get range of added content

                Word.Bookmark bm1 = doc.Bookmarks.Add("bm1", insertRange);//add bookmark

    Demonstration


    Best Regards,

    Terry

    • Marked as answer by drdexter33 Friday, July 28, 2017 3:49 PM
    Friday, July 28, 2017 9:17 AM
  • Thanks Terry.

    I think that worked, except I had to change this code:

    sel1.InsertFile(FileName: filename, Link: false, Attachment: false);//inset file content to first point
    Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
    Word.Range insertRange = doc.Range(Start: sel1.Start, End: sel2.End-1);//get range of added content

    Friday, July 28, 2017 3:47 PM