none
How to find font symbols in word document using C# ? RRS feed

  • Question

  • Hi,

    I am using the namespace Microsoft.Office.Interop.Word to modify a word file.

    Are there anyone know how to find a symbol such as: Copyright (or Wingdings 254) in a word document ?

    And how to remove a line that contains a particular word ?

    Thank you very much !
    Wednesday, December 26, 2012 4:09 AM

Answers

  • Hi Taibc,

    Thanks for posting in the MSDN Forum.

    Following code will do:

     
    ' This sample is under a Word Document application, This is a Ribbon Class
    ' It works fine on my side Win8 64-bit, Office 2010 32-bit, Visual Studio 2010
    Imports Microsoft.Office.Tools.Ribbon
    Imports System.Text.RegularExpressions
    Public Class Ribbon1
        Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click
            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(&HA9) reference the symbol of copyright
            Character = Chr(&HA9)
            wdRange.Select()
            With wdApp.Selection.Find
                .Text = Character
                .Forward = True
                .Wrap = Word.WdFindWrap.wdFindStop
                .Execute()
            End With
            'Following code will remove a line
            wdApp.Selection.HomeKey(wdLine, Type.Missing)
            wdApp.Selection.MoveDown(wdLine, count, wdExtend)
            wdApp.Selection.Delete(wdCharacter, Type.Missing)
        End Sub
    End Class

    I hope it can help you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Thursday, December 27, 2012 3:00 AM
    Moderator
  • With vba, you could use code like the following to toggle the male/female checkbox states. I'll leave it to you to adapt for Microsoft.Office.Interop.Word. The F/R code assumes the words 'Male:' & 'Female:' are followed by tabs, then the checkboxes.


    Sub ToggleCheckState()
    Dim bSex As Boolean
    With ActiveDocument.Content
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "Male:" & vbTab & "[" & ChrW(61608) & ChrW(61694) & "]"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
        .Execute
      End With
      If .Find.Found Then
        If .Characters.Last.Text = ChrW(61608) Then
          bSex = True
        Else
          bSex = False
        End If
      End If
      With .Find
        .Text = "Female:" & vbTab & "[" & ChrW(61608) & ChrW(61694) & "]"
        .Execute
      End With
      If .Find.Found Then
        If bSex = False Then
          .Characters.Last.Text = ChrW(61694)
        Else
          .Characters.Last.Text = ChrW(61608)
        End If
      End If
    End With
    End Sub

    You can use vba code like the following to get the attributes of a selected symbol:

    Sub GetSymbol()
    Dim SelFont, SelCharNum
    With Selection
      With Dialogs(wdDialogInsertSymbol)
        SelFont = .Font
        SelCharNum = (65536 + .charnum) Mod 65536
      End With
    End With
    MsgBox SelFont & vbTab & SelCharNum
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]


    Thursday, December 27, 2012 10:22 AM
  • Hi Taibc,

    Try following code, it works fine on my side:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Office.Tools.Ribbon;
    using Word = Microsoft.Office.Interop.Word;
    using System.Windows.Forms;
    using System.Text.RegularExpressions;
    using Microsoft.VisualBasic;
    namespace WordDocument10
    {
        public partial class Ribbon1
        {
            private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
            {
            }
            private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                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();
                }
                
            }
            private void button2_Click(object sender, RibbonControlEventArgs e)
            {
            }
        }
    }

    I think the "&HFE" is correct, however in C# you need declare it as "0xF0FE" instead of "0xFE", due to it is a Unicode string.

    Additional information:

    You are able to copy past this symbol from Word document to C# code directly, it will work too. And I think it is the most directly way for this issue.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Monday, December 31, 2012 6:56 AM
    Moderator

All replies

  • Hi Taibc,

    Thanks for posting in the MSDN Forum.

    Following code will do:

     
    ' This sample is under a Word Document application, This is a Ribbon Class
    ' It works fine on my side Win8 64-bit, Office 2010 32-bit, Visual Studio 2010
    Imports Microsoft.Office.Tools.Ribbon
    Imports System.Text.RegularExpressions
    Public Class Ribbon1
        Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click
            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(&HA9) reference the symbol of copyright
            Character = Chr(&HA9)
            wdRange.Select()
            With wdApp.Selection.Find
                .Text = Character
                .Forward = True
                .Wrap = Word.WdFindWrap.wdFindStop
                .Execute()
            End With
            'Following code will remove a line
            wdApp.Selection.HomeKey(wdLine, Type.Missing)
            wdApp.Selection.MoveDown(wdLine, count, wdExtend)
            wdApp.Selection.Delete(wdCharacter, Type.Missing)
        End Sub
    End Class

    I hope it can help you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Thursday, December 27, 2012 3:00 AM
    Moderator

  •         'Chr(&HA9) reference the symbol of copyright
            Character = Chr(&HA9)
           

    Thanks Tom Xu,

    Do you know what is the code for Wingdings 254 (checked box) ?

    Kind regards,

    Thursday, December 27, 2012 6:51 AM
  • Hi Taibc,

    I think you might want ChrW(&H2611) for your goal. Please notice the different. There must use ChrW instead of Chr.

    Update that ChrW(&H2611) is the checked box which you can us on document directly. As a Wingding formatted checked box you use a ChrW(&HFE) in fact. You will use ChrW(&HFE) in the code in my original post to find out that symbol.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.






    Thursday, December 27, 2012 7:58 AM
    Moderator
  • Hi Taibc,

    I think you might want ChrW(&H25A1) for your goal. Please notice the different. There must use ChrW instead of Chr.


    Thank you very much ! But I can't find the function "ChrW" or "Chr", I am using C#. Do I need import anything ?

    Thursday, December 27, 2012 8:38 AM
  • Hi Tom,

    Please see my below image.

    How can I jump(find) to after the word "Male:" to replace the checkbox (not checked) by one checked box ?

    Thanks and regards,

    Thursday, December 27, 2012 8:54 AM
  • With vba, you could use code like the following to toggle the male/female checkbox states. I'll leave it to you to adapt for Microsoft.Office.Interop.Word. The F/R code assumes the words 'Male:' & 'Female:' are followed by tabs, then the checkboxes.


    Sub ToggleCheckState()
    Dim bSex As Boolean
    With ActiveDocument.Content
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "Male:" & vbTab & "[" & ChrW(61608) & ChrW(61694) & "]"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
        .Execute
      End With
      If .Find.Found Then
        If .Characters.Last.Text = ChrW(61608) Then
          bSex = True
        Else
          bSex = False
        End If
      End If
      With .Find
        .Text = "Female:" & vbTab & "[" & ChrW(61608) & ChrW(61694) & "]"
        .Execute
      End With
      If .Find.Found Then
        If bSex = False Then
          .Characters.Last.Text = ChrW(61694)
        Else
          .Characters.Last.Text = ChrW(61608)
        End If
      End If
    End With
    End Sub

    You can use vba code like the following to get the attributes of a selected symbol:

    Sub GetSymbol()
    Dim SelFont, SelCharNum
    With Selection
      With Dialogs(wdDialogInsertSymbol)
        SelFont = .Font
        SelCharNum = (65536 + .charnum) Mod 65536
      End With
    End With
    MsgBox SelFont & vbTab & SelCharNum
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]


    Thursday, December 27, 2012 10:22 AM
  • Thanks Macropod,

    But, when I try to used below codes (in C#), I got the error:

    "The number must between -32768 and 32767"

    char character = Convert.ToChar(61608);

    WordApp.Selection.InsertSymbol(character, ref missing , ref missing, ref missing );



    Friday, December 28, 2012 7:39 AM

  • Update that ChrW(&H2611) is the checked box which you can us on document directly. As a Wingding formatted checked box you use a ChrW(&HFE) in fact. You will use ChrW(&HFE) in the code in my original post to find out that symbol.


    Hi Tom,

    When I try to use "ChrW(&HFE)", it shows an error: the name "HFE" does not exist in the current context. How can I fix it ?

    Thanks and regards,

    Friday, December 28, 2012 8:30 AM
  • You should be able to use Chr(&HFE) & Chr(&HA8) or Chr(254) & Chr(168) and, so:
    char character = Convert.ToChar(254) ' for þ
    char character = Convert.ToChar(168) ' for ¨


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Friday, December 28, 2012 10:47 AM
    Friday, December 28, 2012 10:46 AM
  • You should be able to use Chr(&HFE) & Chr(&HA8) or Chr(254) & Chr(168) and, so:
    char character = Convert.ToChar(254) ' for þ
    char character = Convert.ToChar(168) ' for ¨


    Cheers
    Paul Edstein
    [MS MVP - Word]


    Thanks Paul Edstein very much.

    Please see my below codes.

    char character = Convert.ToChar(254)
    
    if (WordApp.Selection.Find.Execute(character , ref missing,
                    ref missing, ref missing, ref missing, ref missing, true,
                    Microsoft.Office.Interop.Word.WdFindWrap.wdFindStop, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing))
    
       MesseageBox.Show ("Found");


    I think it is ok to find the þ with the above codes. But, I can't find the checked box symbol (in font Wingdings).

    Can I assign the font "Wingdings" for the function WordApp.Selection.Find.Execute (...) ?


    • Edited by Taibc Saturday, December 29, 2012 6:35 AM
    Saturday, December 29, 2012 6:32 AM
  • AFAIK, you'd have to use a construct like:

    With wdApp.Selection.Find
        .Text = Character
        .Font = "Wingdings"
        
    .Forward = True
        
    .Wrap = Word.WdFindWrap.wdFindStop ' or Microsoft.Office.Interop.Word.WdFindWrap.wdFindStop
        .Execute()
    End With

    As per Tom Xu's earlier post (I don't know C#).


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, December 29, 2012 6:50 AM
  • AFAIK, you'd have to use a construct like:

    With wdApp.Selection.Find
        .Text = Character
        .Font = "Wingdings"
        
    .Forward = True
        
    .Wrap = Word.WdFindWrap.wdFindStop ' or Microsoft.Office.Interop.Word.WdFindWrap.wdFindStop
        .Execute()
    End With

    As per Tom Xu's earlier post (I don't know C#).


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thank you.

    In C#, I tried to use: WordApp.Selection.Find.Font = "Wingdings". It showed an error (because WordApp.Selection.Find.Font is Microsoft.Office.Interop.Word.Font type, not string)

    So, I used WordApp.Selection.Find.Font.Name = "Wingdings". But it not still successful.

    Are there anyone know how to set Find.Font to "Wingdings" in C# ?

    Kind regards,

    Saturday, December 29, 2012 7:49 AM
  •  
    int emptyCheckbox = -3928; //-3842
    object font = "Wingdings";
    object unicode = Microsoft.Office.Core.MsoTriState.msoTrue;
    WordApp.Selection.InsertSymbol(emptyCheckbox, ref font, ref unicode, ref missing);
     

    You may find that setting the parameters you need in your .Find routine in a similar way helps. 

    However, if you didn't insert the symbols using a similar mechanism, please be aware that there is more than one mechanism in Word to do so (e.g. you can use a { SYMBOL } field, and in that case searching for the Wingdings symbol itself probably won't work. There was a discussion of some of the issues here:

    http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/bed16cf1-0c39-4fce-85c2-c48632aab095


    Peter Jamieson


    • Edited by Peter Jamieson Saturday, December 29, 2012 10:58 AM extra info.
    Saturday, December 29, 2012 10:10 AM
  • Hi Taibc,

    Try following code, it works fine on my side:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Office.Tools.Ribbon;
    using Word = Microsoft.Office.Interop.Word;
    using System.Windows.Forms;
    using System.Text.RegularExpressions;
    using Microsoft.VisualBasic;
    namespace WordDocument10
    {
        public partial class Ribbon1
        {
            private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
            {
            }
            private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                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();
                }
                
            }
            private void button2_Click(object sender, RibbonControlEventArgs e)
            {
            }
        }
    }

    I think the "&HFE" is correct, however in C# you need declare it as "0xF0FE" instead of "0xFE", due to it is a Unicode string.

    Additional information:

    You are able to copy past this symbol from Word document to C# code directly, it will work too. And I think it is the most directly way for this issue.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Monday, December 31, 2012 6:56 AM
    Moderator
  • Thanks Tom Xu very much !

    That's is great. Do you know how to only delete the symbols (that found) (not the line).

    Wish you a happy new year - 2013 !

    Kind regards,

    Wednesday, January 2, 2013 1:31 AM
  • Hi Taibc,

    The following snippet might help you. In this snippet I will delete all of the unchecked box or use checked box instead of them:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Office.Tools.Ribbon;
    using Word = Microsoft.Office.Interop.Word;
    using System.Windows.Forms;
    using System.Text.RegularExpressions;
    using Microsoft.VisualBasic;
    
    namespace WordDocument10
    {
        public partial class Ribbon1
        {
            private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
            {
    
            }
    
            private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                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(0x25A1);    
                wdRange.Select();
                wdApp.Selection.Find.Text = FindText.ToString();
                wdApp.Selection.Find.Forward = true;
                wdApp.Selection.Find.Wrap = Word.WdFindWrap.wdFindStop;
    
                while (wdApp.Selection.Find.Execute())
                {
                    //This will delete the symbol
                    wdApp.Selection.Range.Text = string.Empty;
                    //This will replace the symbol
                    /*
                    wdApp.Selection.Font.Name = "Wingdings";
                    wdApp.Selection.Range.Text = char.ConvertFromUtf32(0xFE);
                     */
                }     
            }
    
            private void button2_Click(object sender, RibbonControlEventArgs e)
            {
                Word.Application wdApp = Globals.ThisDocument.Application;
                Word.Document wdDoc = wdApp.ActiveDocument;
                Word.Range wdRange = wdDoc.Range(wdDoc.Content.Start,
                    wdDoc.Content.End);
            }
        }
    }

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.



    Wednesday, January 2, 2013 5:29 AM
    Moderator
  • Hi Tom,

    Thank you, your code is very useful. I only need to change "0x25A1" to "0xF0A8", then it is successful

    Kind regards,

    Tai

    Wednesday, January 2, 2013 6:33 AM
  • Hi Taibc,

    I'm glad to hear that can solve your problem.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 3, 2013 1:08 AM
    Moderator
  • Hi Taibc,

    I'm glad to hear that can solve your problem.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Dear Tom,

    May I ask you another question ?

    For example: My file include a line: "Male {check box}   Female {check box}".

    How can I identify or find only the "check box" (after "Male"), to replace it (not all check boxs in the file).

    Kind regards,


    • Edited by Taibc Tuesday, January 8, 2013 7:21 AM
    Tuesday, January 8, 2013 7:21 AM
  • Hi Taibc,

    I would recommend you create a new thread for your new request, it will let other community members easier to retrieve your question and let your receive better support.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 9, 2013 2:22 AM
    Moderator
  • Hi Taibc,

    I would recommend you create a new thread for your new request, it will let other community members easier to retrieve your question and let your receive better support.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Hi Tom,

    Thank you. I can do now :)

    Wednesday, January 9, 2013 3:27 AM