1
我以編程方式打開一個用於搜索的詞文件並突出顯示關鍵字。我的日常工作正常。問題是當我以編程方式打開文件,然後出現對話框並要求我以只讀模式打開文件。對話框看起來像 有關打開word文件和只讀模式使用c#&MS-word interop的問題
實際上我不想以只讀模式打開文件,因爲人們可以打開並喜歡更改和保存。所以請指導我如何才能以只讀模式打開文件。
這是我的完整代碼。只需看看並告訴我代碼中出現了什麼問題,或告訴我任何技巧,因爲我可以不以只讀模式打開文件。這是我的代碼。
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();
}
}