1

我正在以编程方式打开一个 word 文件进行搜索并突出显示关键字。我的日常工作正常。问题是当我以编程方式打开文件时,会出现一个对话框,要求我以只读模式打开文件。对话框看起来像 在此处输入图像描述

实际上我不想以只读模式打开文件,因为人们可以打开并喜欢更改和保存。所以请指导我如何不以只读模式打开文件。

这是我的完整代码。看看并告诉我我的代码有什么问题或告诉我任何技巧,因此我可以以非只读模式打开文件。这是我的代码。

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();
            }


        }
4

1 回答 1

0

指导我如何不以只读模式打开文件

好吧,暂时忘记代码,因为它是由“tridip”打开的,所以它被锁定了。您不能写入当前由其他用户打开的 Word 文件,因此该用户需要将其关闭。也许你是“tridip”并且你已经打开了你的程序试图打开的文档???

于 2013-04-07T05:57:44.703 回答