none
Inserting Word Docs at Current Cursor Position from an Add-in RRS feed

  • Question

  • We are creating an add-in that will allow a user to insert word documents(.doc) into a document that is already open.  We would like this to be at the current cursor position.  I have seen examples that use Application.Selection.InsertFile() or Selection.TypeText() to insert content into a file, but I am having no luck in trying to use these or getting the current cursor position.  Is it possible to do what I am looking to do and if so, any pointers to how I might accomplish this?

     

    Thanks

    Friday, October 21, 2011 7:24 PM

Answers

  • Here's some code I'm using to do this same thing....

    Word.

    Application wordApp = this.Application;
    Word.Document wordDoc = wordApp.ActiveDocument;
    Word.
    Selection sln = wordApp.Selection;
    object oMissing = System.Reflection.Missing.Value;

    string strFileName = "FileName.docx";
    sln.InsertFile(strFileName,
    ref oMissing, ref oMissing, ref oMissing, ref

    oMissing);


    Jo Crews
    Friday, October 21, 2011 8:35 PM
  • Thank you everyone for your help.  Sorry for all the missing details.

    I'm targeting Word 2007, VSTO 3, C#.  

    I'm hosting WPF in a Windows Form control and it turns out I needed to set a property to hold the Word.Application object in order to have access to it.  Jo's sample code works now.  Thanks again.

    Monday, October 24, 2011 1:34 PM

All replies

  • Here's some code I'm using to do this same thing....

    Word.

    Application wordApp = this.Application;
    Word.Document wordDoc = wordApp.ActiveDocument;
    Word.
    Selection sln = wordApp.Selection;
    object oMissing = System.Reflection.Missing.Value;

    string strFileName = "FileName.docx";
    sln.InsertFile(strFileName,
    ref oMissing, ref oMissing, ref oMissing, ref

    oMissing);


    Jo Crews
    Friday, October 21, 2011 8:35 PM
  • I don't write in C# (only VB) so I would not be able to necessarily identify a coding error in your subroutine but it seems fine. Something I did note is that unless the file you are trying to insert is in the same folder as the currently active document, that variable should include the full path to the file. 

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    Saturday, October 22, 2011 12:17 AM
  • Hi jmncush

    Jo has provided you with a code snipppet in C# for VSTO that should work.

    But we're missing a few things in your problem description:

    1. Version of Word you're targeting

    2. Version of VSTO you're using (if you're not working with the VSTO technology, please specificy what kind of project this is)

    3. The programming language you prefer

    4. Exactly how what you've tried "isn't working". Just saying you're having no luck doesn't give us any hints as to where the problem might be. Selection.InsertFile is certainly the correct method.


    Cindy Meister, VSTO/Word MVP
    Saturday, October 22, 2011 6:23 AM
    Moderator
  • Sorry Jo, I thought I was responding to the OP and that they had contributed the code sample.

    Senior moment :-)


    Kind Regards, Rich ... http://greatcirclelearning.com
    Monday, October 24, 2011 12:33 AM
  • Hello,

     

    The Cursor position of the mouse or of the text?

    For the text, you simply use the Globals.ThisAddin.Application.Selection.

    For the mouse position you need to implement some window hooks.

     

    Best regards,

    Silviu.


    http://www.rosoftlab.net/
    Monday, October 24, 2011 10:49 AM
  • Thank you everyone for your help.  Sorry for all the missing details.

    I'm targeting Word 2007, VSTO 3, C#.  

    I'm hosting WPF in a Windows Form control and it turns out I needed to set a property to hold the Word.Application object in order to have access to it.  Jo's sample code works now.  Thanks again.

    Monday, October 24, 2011 1:34 PM