none
Open another document and the ribbon can not be clicked RRS feed

  • Question

  • Sorry for my bad English,so I can just try best to explain the problem  I faced with;

    I want to click a button to open a document(word1.docx) first ,and click another button to open another document(word2.docx) and close it by programme. Then I click the "word1.docx..." in the taskbar,but I can not click the ribbon anymore.

    When I click another app in the taskbar and click back to "word1.docx" in the taskbar,the ribbon can be clicked again.

    I have posted  my code below.  

    Thanks for any help in advance!

    Regards

    partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.button1 = new System.Windows.Forms.Button();
                this.button2 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(94, 21);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(229, 81);
                this.button1.TabIndex = 0;
                this.button1.Text = "OpenDocumentA";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // button2
                // 
                this.button2.Location = new System.Drawing.Point(94, 157);
                this.button2.Name = "button2";
                this.button2.Size = new System.Drawing.Size(229, 81);
                this.button2.TabIndex = 1;
                this.button2.Text = "OpenDocumentB";
                this.button2.UseVisualStyleBackColor = true;
                this.button2.Click += new System.EventHandler(this.button2_Click);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(420, 323);
                this.Controls.Add(this.button2);
                this.Controls.Add(this.button1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);
    
            }
    
            #endregion
    
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.Button button2;
        }

    public partial class Form1 : Form
        {
            private Word.Application app = null;
            private Word.Document doc1 = null;
            private Word.Document doc2 = null;
    
            public Form1()
            {
                InitializeComponent();
                app = new Word.ApplicationClass();
                this.TopMost = true;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                object readOnly = false;
                object fileName = @"C:\Users\Administrator\Desktop\word1.docx";
                object missing = Type.Missing;
    
                doc1=app.Documents.Open(ref fileName,
                    ref missing, ref readOnly, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing);
                doc1.ActiveWindow.WindowState = Word.WdWindowState.wdWindowStateMaximize;
                app.Visible = true;
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
    
                object readOnly = false;
                object fileName = @"C:\Users\Administrator\Desktop\word2.docx";
                object missing = Type.Missing;
    
                doc2 = app.Documents.Open(ref fileName,
                    ref missing, ref readOnly, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing);
    
                object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
                doc2.Close(ref doNotSaveChanges, ref missing, ref missing);
    
            }
        }


    • Edited by dwjujq Thursday, May 21, 2015 7:37 PM
    Thursday, May 21, 2015 7:36 PM

Answers

  • Hi dwjujq,

    Base on my test with word 2013, I can reproduce that issue too.

    Base on your code, you are using the same application instance to open document, I suggest that you could create the local variable of word application to deal with that issue, then open the document.

    For example:

    private void button2_Click(object sender, EventArgs e)
            {
                object readOnly = false;
                object fileName = @"D:\Files\ff\word2.docx";
                object missing = Type.Missing;
                Word.Application app2 = new Word.Application();
                app2.Visible = true;
                doc2 = app2.Documents.Open(ref fileName,
                    ref missing, ref readOnly, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing);
    
                object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
                doc2.Close(ref doNotSaveChanges, ref missing, ref missing);
                app2.Quit();
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(doc2);
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(app2);
                doc2 = null;
                app2 = null;
            }

    Regards

    Starain


    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.

    Friday, May 22, 2015 8:47 AM
    Moderator

All replies

  • Hi dwjujq,

    Base on my test with word 2013, I can reproduce that issue too.

    Base on your code, you are using the same application instance to open document, I suggest that you could create the local variable of word application to deal with that issue, then open the document.

    For example:

    private void button2_Click(object sender, EventArgs e)
            {
                object readOnly = false;
                object fileName = @"D:\Files\ff\word2.docx";
                object missing = Type.Missing;
                Word.Application app2 = new Word.Application();
                app2.Visible = true;
                doc2 = app2.Documents.Open(ref fileName,
                    ref missing, ref readOnly, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing);
    
                object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
                doc2.Close(ref doNotSaveChanges, ref missing, ref missing);
                app2.Quit();
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(doc2);
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(app2);
                doc2 = null;
                app2 = null;
            }

    Regards

    Starain


    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.

    Friday, May 22, 2015 8:47 AM
    Moderator
  • Hi dwjujq

    Try changing this:

    app = new Word.ApplicationClass();

    to this and see if it makes any difference:

    app = new Word.Application();

    Also, try releasing the COM objects at the end of a procedure, explicitly:

    doc1 = null;
    GC.Collect();
    GC.WaitForPendiongFinalizers();GC.Collect();
    GC.WaitForPendiongFinalizers();...and the same for doc2.


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, May 23, 2015 4:27 AM
    Moderator