简体   繁体   中英

issue regarding open word file and read only mode using c# & MS-word interop

i am programmatically open a word file for search and highlight keyword. my routine is working fine. the problem is when i am opening the file programmatically then a dialog come and ask me to open file in read only mode. the dialog look like 在此处输入图片说明

actually i do not want to open the file in read only mode because people can open and like to change and save. so guide me what i can do to not to open the file in read only mode.

here is my full code. just have a look and tell me what is wrong in my code or tell me any trick as a result i can open the file not in read only mode. here is my code.

private void button1_Click(object sender, EventArgs e)
        {
            object fileName = "";
            string filePath = "";
            string strSaveasPath = "";
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                fileName = openFileDialog1.FileName;
                //strSaveasPath = Path.GetDirectoryName(path.ToString()); 
            }



            //fileName = "Z:\\C0000000003.doc";
            List<string> _list = new List<string>();
            _list.Add("tridip");
            _list.Add("arijit");

            //object fileName = "D:\\CVArchievePath\\C0000000001.doc";
            object textToFind = "test";
            object readOnly = false;
            Word.Application word = new Word.Application();
            Word.Document doc = new Word.Document();
            object missing = Type.Missing;
            try
            {
                doc = word.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);
                doc.Activate();

                object matchPhrase = false;
                object matchCase = false;
                object matchPrefix = false;
                object matchSuffix = false;
                object matchWholeWord = false;
                object matchWildcards = false;
                object matchSoundsLike = false;
                object matchAllWordForms = false;
                object matchByte = false;
                object ignoreSpace = false;
                object ignorePunct = false;

                object highlightedColor = Word.WdColor.wdColorGreen;
                object textColor = Word.WdColor.wdColorLightOrange;

                object missingp = false;
                Word.Range range = doc.Range();

                foreach (string line in _list)
                {
                    textToFind = line;
                    bool highlighted = range.Find.HitHighlight(ref textToFind,
                                                               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,
                                                               ref missing,
                                                               ref missing,
                                                               ref missing,
                                                               ref missing,
                                                               ref missing,
                                                               ref missing);
                }

                System.Diagnostics.Process.Start(fileName.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error : " + ex.Message);
                //Console.ReadKey(true);
            }
            finally
            {
                //doc.Close(missing, missing, missing);
                if(doc!=null)
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);

                if (word != null)
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(word);

                word = null;
                doc = null;
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }


        }

guide me what i can do to not to open the file in read only mode

Well, forget the code for now, it is locked because it is open by "tridip". You cannot write to a Word file that is currently open by another user, so that user would need to close it. Maybe you are "tridip" and you have the document open already which your program is trying to open???

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM