none
ThisAddIn.Application.Documents.Open() methods opens new word instance even though the active document is empty. RRS feed

  • Question

  • Hi,

    I have created a Ribbon for MS Word(2010). On click of ribbon button(Download button) I have to open the document specified in the code. If active document(present document) is empty then it should open in same word instance and if the active document contains some text it should open in new word instance. I have the below piece of code which does this.

    private static void OpenWordDocument(byte[] content, string tempfile)
            {
                System.IO.File.WriteAllBytes(tempfile, content);
                object missing = System.Reflection.Missing.Value;
                object fileName = tempfile;
                object readOnly = true;
                object isVisible = true;
                object confirmConversions = true;
                object addToRecentFiles = false;
                object saveas = false;
                Microsoft.Office.Interop.Word.Document wordDoc = Globals.ThisAddIn.Application.Documents.Open(
                    ref fileName,
                    ref confirmConversions,
                    ref readOnly,
                    ref addToRecentFiles,
                    ref missing,
                    ref missing,
                    ref missing,
                    ref missing,
                    ref missing,
                    ref missing,
                    ref missing,
                    ref isVisible,
                    ref missing,
                    ref missing,
                    ref missing,
                    ref missing);
                ThisAddIn.Application.ActiveDocument.ActiveWindow.DisplayHorizontalScrollBar = true;
                ThisAddIn.Application.ActiveDocument.ActiveWindow.ActivePane.View.Type = WdViewType.wdPrintView;
            }

    Issue: The functionality does not work expected in below scenario:

    1. Open saved word document which has some content in it.
    2. Click on File tab -> New -> Blank document.
    3. In this blank document, click on my Addin's download button which should download the content and display it in Word document.
    Since we have opened new blank document and this document is empty. It should have opened the downloaded content in this empty document itself instead it is getting displayed in new word instance.

    However the functionality works as expected when I first open the new document instead of saved word document in the first go.i.e, in below scenario:

    1. Go to Start --> Select MS Word 2010.
    2. In this blank document, click on my Addin's download button.
    This action downloads the content and display it in same blank document.

    Please help me to solve the above issue, thanks in advance.

    Regards, Chetan.


    Chetan Rajakumar

    Friday, August 22, 2014 9:35 AM

Answers

  • Hi Chetan,

    Based on the description, that when you open a document there is a blank document which opened by clicking File tab -> New -> Blank document, then new document is opened in a new instance. This is feature is by designed. As a workaround, we can close the blank document manullay. Here is a sample for your reference:

     private void btnOpenDoc_Click(object sender, RibbonControlEventArgs e)
            {
               
                Globals.ThisAddIn.Application.Documents.Open(@"C:\Users\UserName\Desktop\Hello_2010.docx");
                foreach (Document doc in Globals.ThisAddIn.Application.Documents)
                {
                    if (doc.Content.End <= 1)
                    {
                        doc.Close(WdSaveOptions.wdDoNotSaveChanges);
                    }
                }
    
            }

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, August 25, 2014 8:32 AM
    Moderator

All replies

  • Saturday, August 23, 2014 10:52 AM
  • Hi Chetan,

    This forum focues on questions and feedback for Office client. Since your query is directly related to Word for development, I'm moving it to the dedicated forum of Word for Developers, where you can get more experienced responses:

    http://social.msdn.microsoft.com/Forums/office/en-US/home?forum=worddev

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    Regards,

    Ethan Hua

    Forum Support


    Come back and mark the replies as answers if they help and unmark them if they provide no help.

    If you have any feedback on our support, please click here
    Monday, August 25, 2014 6:04 AM
  • Thanks Ethan. Hope i get some help in that forum.

    Regards, Chetan.


    Chetan Rajakumar

    Monday, August 25, 2014 6:56 AM
  • Hi Chetan,

    Based on the description, that when you open a document there is a blank document which opened by clicking File tab -> New -> Blank document, then new document is opened in a new instance. This is feature is by designed. As a workaround, we can close the blank document manullay. Here is a sample for your reference:

     private void btnOpenDoc_Click(object sender, RibbonControlEventArgs e)
            {
               
                Globals.ThisAddIn.Application.Documents.Open(@"C:\Users\UserName\Desktop\Hello_2010.docx");
                foreach (Document doc in Globals.ThisAddIn.Application.Documents)
                {
                    if (doc.Content.End <= 1)
                    {
                        doc.Close(WdSaveOptions.wdDoNotSaveChanges);
                    }
                }
    
            }

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, August 25, 2014 8:32 AM
    Moderator
  • Thanks Fei.


    Chetan Rajakumar

    Tuesday, September 2, 2014 3:56 AM