如果的打开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;
        }
    }
}


展开阅读全文

没有更多推荐了,返回首页