使用c#& amp;有关打开文字文件和只读模式的问题MS-word互操作

时间:2013-03-19 14:50:50

标签: c# ms-word office-interop

我以编程方式打开一个word文件进行搜索并突出显示关键字。我的日常工作正常。问题是当我以编程方式打开文件然后出现一个对话框并要求我以只读模式打开文件。对话框看起来像 enter image description here

实际上我不想以只读模式打开文件,因为人们可以打开并喜欢更改和保存。所以我可以做些什么来不以只读模式打开文件。

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

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


        }

1 个答案:

答案 0 :(得分:0)

  

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

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