none
how can i insert information to activeDocument RRS feed

  • Question

  • i use addin developer and want to insert something information to activedocument, i use below code:

    Word.Selection sln = Globals.ThisAddIn.Application.Selection;
    sln.InsertParagraphAfter();
    sln.MoveStart();
    sln.InsertFile(file);
    sln = null;

    if there is only one document, this code will be ok, but if has several document, this code sometimes will adding information to another document which not a activedocument.

    following step should reproduce(use addin develop)

    1. open a document, and then click ribbon command to show a custompanel which contain a usercontrol, click a command(host in usercontrol) then open a custom dialog, here i use Form.showdialog(owner) method,  the owner parameter just the usercontrol.  something addition detail infor: the custompanel is one-to-one relationship with document, it create in documentopen event and destory in documentclose event.

    2. open another document, and click anywhere in document to make cursor in document body.

    3. click the dialog in windows taskbar to active custom dialog which opened in step1, then excute the insert method, you can found the information is insert to the second document but not the first document.

    i don't understand because in step3 the first document is exact in active. 

    i just want to insert information to activedocument, anyone can help me? thanks.


    • Edited by sendreams Sunday, December 29, 2013 11:51 AM
    Sunday, December 29, 2013 11:49 AM

Answers

  • Hello sendreams,

    Well, it looks like the selection object comes from the second document. Please check out the following points:

    1. Try to specify the Word window as a parent to the dialog (not your user control). You can used the ActiveWindow property of the Document class.

    2. Use the ActiveDocument property of the Application class to get the selection object. Frankly speaking, I don't think it can help. But who knows ;) 

    3. The most easiest way, as a workaround, is to save the reference do the document where the data should be pasted.

    • Marked as answer by sendreams Monday, December 30, 2013 3:06 AM
    Sunday, December 29, 2013 12:03 PM

All replies

  • Hello sendreams,

    Well, it looks like the selection object comes from the second document. Please check out the following points:

    1. Try to specify the Word window as a parent to the dialog (not your user control). You can used the ActiveWindow property of the Document class.

    2. Use the ActiveDocument property of the Application class to get the selection object. Frankly speaking, I don't think it can help. But who knows ;) 

    3. The most easiest way, as a workaround, is to save the reference do the document where the data should be pasted.

    • Marked as answer by sendreams Monday, December 30, 2013 3:06 AM
    Sunday, December 29, 2013 12:03 PM
  • hi, Eugene

    thanks for your reply.

    in my application , i just export some html string to a file, than insert it to the activedocument,

    i can get the activedocument, but how to insert this file to it(current cursor position)? i can't find selection property on Word.Document object.

    i search this forum and just find code snippet i posted in this thread, i don't know how to do, ,

    Sunday, December 29, 2013 2:13 PM
  • another accompanying question

    if insert html file(with some paragraph) to activedocument sucessed, how can get the inserting paragraphs?

    i use the code below, can't working.

    Word.Selection sln = Globals.ThisAddIn.Application.Selection;
    sln.InsertParagraphAfter();
    sln.MoveStart();
    sln.InsertFile(file); // sln is a Word.Selection object
    Word.Paragraphs ps = sln.Paragraphs;
    foreach (Word.Paragraph p in ps) {
       // only one paragraph and not the insert paragraph
    }
    sln = null;
    thanks


    Sunday, December 29, 2013 2:22 PM
  • hi, Eugene

    your first advice can't workable.

    Word.Window window = Globals.ThisAddIn.Application.ActiveWindow;
    frm.Show(window);    // compile error

    this code has a compile error in Show() method.

    your second propose is a problem for me, i can't found the Word.Selection property on Word.Document object

    your third method is my current way, but only can insert information to the end of the document, but i want to insert to current sursor. 

    thanks


    Monday, December 30, 2013 2:31 AM
  • hi, Eugene

    i solve this problem, use below code

    Word.Selection sln = currentDocument.ActiveWindow.Selection;
    sln.InsertParagraphAfter();
    sln.MoveStart();
    sln.InsertFile(file);
    //DealInsertInformation(sln, blocks);
    sln = null;

    the currentDocument is a reference cache before custom dialog opening.



    Monday, December 30, 2013 2:48 AM
  • Hi sendreams,

    You are right, the Document class doesn't provide the Selection property. Instead, the Window class provides the Selection property (not Document).

    Monday, December 30, 2013 8:22 AM
  • Good news! Thank you for sharing the solution for other forum readers.
    Monday, December 30, 2013 8:24 AM