none
In C# how to detect non keyboard characters in a word file RRS feed

  • Question

  • I have a word file. It consists of lot of non key board characters in bold ,Italic, Underlined. I want to detect all non keyboard characters in a list with its style i.e., bold, Italic, Underline . Hope u can understand my question.......

    Thank You

    • Changed type VSTO NOOR Thursday, November 14, 2013 6:34 AM
    • Moved by CaillenModerator Friday, November 15, 2013 2:54 AM Word developing question.
    Thursday, November 14, 2013 6:34 AM

Answers

  • Hi,

    Is "non keyboard characters" the same with what this article is talking about? Maybe you could try Regular Expression, please check if this thread helps: Need RegEx to filter anything except keyboard characters

    And try this regex pattern in this thread:

    [\$@\^`,\|%;\.~\(\)\/\\\{\}:\?=\-\+_#!<>&\sa-zA-Z0-9\[\]]

    Since this question is more related to Word Developing, I'm moving it to Word for Developers forum where you could ask experts in Word Developing for help on how to get characters with its style in a word document.

    Thanks for your understanding.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, November 15, 2013 2:50 AM
    Moderator
  • Hi,

    Welcome to MSDN forum.

    We can use Word Object Model to get the list content and style in a Word document.

    Here is a sample to loop all text in a Word document list:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using MSWord = Microsoft.Office.Interop.Word;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                var application = new MSWord.Application();
                var document = application.Documents.Open(@"E:\List.docx");
    
                try
                {
                    MSWord.List wl = document.Lists[1];//get the first list
                    string rangtext = string.Empty;
                    foreach (MSWord.Paragraph wp in wl.ListParagraphs)
                    {
                        rangtext = wp.Range.Text; // get the text of a paragraph in the list
                        if (wp.Range.Bold != 0)//-1 represent all characters are Bold, 9999999 represent part of characters are Bold
                        {
                            
                        }
                        else if (wp.Range.Italic != 0)//-1 represent all characters are Italic, 9999999 represent part of characters are Italic
                        {
    
                        }
                        else if (wp.Range.Underline != MSWord.WdUnderline.wdUnderlineNone)
                        {
    
                        }
                    }
    	    }
                catch(Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    document.Save();
                    document.Close();
                    application.Quit();
                    Marshal.ReleaseComObject(application);
                }
            }
        }
    }

    In addition, don’t forget to add Word COM reference(Microsoft.Office.Interop.Word) in current project.

    Here are some related articles for your reference:

    Lists Members

    List Members

    ListParagraphs Members

    Paragraph Members

    Range Members

    Hope will help you.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 15, 2013 9:50 AM
    Moderator

All replies

  • Hi,

    Is "non keyboard characters" the same with what this article is talking about? Maybe you could try Regular Expression, please check if this thread helps: Need RegEx to filter anything except keyboard characters

    And try this regex pattern in this thread:

    [\$@\^`,\|%;\.~\(\)\/\\\{\}:\?=\-\+_#!<>&\sa-zA-Z0-9\[\]]

    Since this question is more related to Word Developing, I'm moving it to Word for Developers forum where you could ask experts in Word Developing for help on how to get characters with its style in a word document.

    Thanks for your understanding.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, November 15, 2013 2:50 AM
    Moderator
  • Hi,

    Welcome to MSDN forum.

    We can use Word Object Model to get the list content and style in a Word document.

    Here is a sample to loop all text in a Word document list:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using MSWord = Microsoft.Office.Interop.Word;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                var application = new MSWord.Application();
                var document = application.Documents.Open(@"E:\List.docx");
    
                try
                {
                    MSWord.List wl = document.Lists[1];//get the first list
                    string rangtext = string.Empty;
                    foreach (MSWord.Paragraph wp in wl.ListParagraphs)
                    {
                        rangtext = wp.Range.Text; // get the text of a paragraph in the list
                        if (wp.Range.Bold != 0)//-1 represent all characters are Bold, 9999999 represent part of characters are Bold
                        {
                            
                        }
                        else if (wp.Range.Italic != 0)//-1 represent all characters are Italic, 9999999 represent part of characters are Italic
                        {
    
                        }
                        else if (wp.Range.Underline != MSWord.WdUnderline.wdUnderlineNone)
                        {
    
                        }
                    }
    	    }
                catch(Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    document.Save();
                    document.Close();
                    application.Quit();
                    Marshal.ReleaseComObject(application);
                }
            }
        }
    }

    In addition, don’t forget to add Word COM reference(Microsoft.Office.Interop.Word) in current project.

    Here are some related articles for your reference:

    Lists Members

    List Members

    ListParagraphs Members

    Paragraph Members

    Range Members

    Hope will help you.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 15, 2013 9:50 AM
    Moderator