在Word 中用C#查找Wingdings符号

在Word我们通常在VBA或VB.NET环境下以如下方式查找Wingdings符号:

        Dim wdApp As Word.Application = Globals.ThisDocument.Application
        Dim wdDoc As Word.Document = wdApp.ActiveDocument
        Dim wdRange As Word.Range = wdDoc.Range()
        Dim M As Match
        Dim Character As String
        Dim P As Word.Paragraph
        Dim wdLine As Word.WdUnits = Word.WdUnits.wdLine
        Dim wdCharacter As Word.WdUnits = Word.WdUnits.wdCharacter
        Dim wdExtend As Word.WdMovementType = Word.WdMovementType.wdExtend
        Dim count As Integer = 1

        'Chr(&HFE) 对应一个wingdings符号
        Character = Chr(&HFE)
        wdRange.Select()
        With wdApp.Selection.Find
            .Text = Character
            .Forward = True
            .Wrap = Word.WdFindWrap.wdFindStop
            .Execute()
        End With
        '以下用来移除那个符号所在的行
        wdApp.Selection.HomeKey(wdLine, Type.Missing)
        wdApp.Selection.MoveDown(wdLine, count, wdExtend)
        wdApp.Selection.Delete(wdCharacter, Type.Missing)

可是你会发现在C#中你靠"0xFE"(与VB中的“&HFE"是对应的)并不能找到相应的符号。实际上这里应该在前面加上"F0”成为"0xF0FE"这样就能找到相应的符号了。代码如下(以下代码与上面的代码是等效的):

	    object missing = Type.Missing;
            Word.Application wdApp = Globals.ThisDocument.Application;
            Word.Document wdDoc = wdApp.ActiveDocument;
            Word.Range wdRange = wdDoc.Range(wdDoc.Content.Start,
                wdDoc.Content.End);
            object FindText = Convert.ToChar(0xF0FE);            
            wdRange.Select();
            wdApp.Selection.Find.Text = FindText.ToString();
            wdApp.Selection.Find.Forward = true;
            wdApp.Selection.Find.Wrap = Word.WdFindWrap.wdFindStop;
            object wdLine = Word.WdUnits.wdLine;
            object wdExtend = Word.WdMovementType.wdExtend;
            object count = 1;
            while (wdApp.Selection.Find.Execute())
            {
                wdApp.Selection.HomeKey(ref wdLine, ref missing);
                wdApp.Selection.MoveDown(ref wdLine, ref count, ref wdExtend);
                wdApp.Selection.Delete();
            }


你可能感兴趣的:(word)