繁体   English   中英

有关使用C#和MS-word互操作的打开Word文件和只读模式的问题

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

我以编程方式打开一个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();
            }


        }

指导我该怎么做才能不以只读模式打开文件

好吧,现在忘记代码了,因为“ tridip”打开了它,所以它已被锁定。 您无法写入另一个用户当前打开的Word文件,因此该用户需要将其关闭。 也许您是“ tridip”,并且您已经打开了程序正在尝试打开的文档???

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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