本文主要是介绍如果的打开Word文档是Visible参数设为false你会得不到Selection实体,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
以下是异常重现代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Win32;namespace WpfApplication5
{/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window{List<string> words = new List<string>();public MainWindow(){InitializeComponent();words.Add("me");words.Add("you");words.Add("us");}private void button1_Click(object sender, RoutedEventArgs e){Word.Application objApplication = new Word.Application();OpenFileDialog objOpenFileDialog = new OpenFileDialog();objOpenFileDialog.Filter = "Word document (*.docx)|*.docx";objOpenFileDialog.ShowDialog();object strPath = objOpenFileDialog.FileName;object flag = false;//object nflag = true;var missing = Type.Missing;Word.Document objDocument = objApplication.Documents.Open(ref strPath, ref missing, ref flag, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref flag, ref missing, ref missing, ref missing, ref missing);try{foreach (string word in words){SelectionFind(word, objApplication);}}catch{}finally{objDocument.Save();((Word._Application)objApplication).Quit();}}private void SelectionFind(string word, Word.Application objApplication){try{objApplication.Selection.Find.ClearFormatting();}catch{}objApplication.Visible = false;try{objApplication.Selection.Find.Replacement.ClearFormatting();}catch{}objApplication.Selection.Find.Replacement.Highlight = 1;objApplication.Selection.Find.Text = word;objApplication.Selection.Find.Replacement.Text = word;objApplication.Selection.Find.Forward = true;objApplication.Selection.Find.Wrap = Word.WdFindWrap.wdFindContinue;objApplication.Selection.Find.Format = true;objApplication.Selection.Find.MatchCase = false;objApplication.Selection.Find.MatchWholeWord = false;objApplication.Selection.Find.MatchWildcards = false;objApplication.Selection.Find.MatchSoundsLike = false;objApplication.Selection.Find.MatchAllWordForms = false;objApplication.Selection.Find.Execute(Replace: Word.WdReplace.wdReplaceAll);objApplication.Options.DefaultHighlightColorIndex = Word.WdColorIndex.wdYellow;objApplication.Selection.Range.HighlightColorIndex = Word.WdColorIndex.wdYellow;}}
}
这篇关于如果的打开Word文档是Visible参数设为false你会得不到Selection实体的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!