Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Dear All,

I have an windows application, in which we are automating MS-OFFICE 2013/16.

Now, we are trying to compare the Font Family, for that i want to create enum font family.

Is there font family enum in WordInterop?

What I have tried:

Because the font names have space, so i got stuck how to create this enum
public enum FontFamilies
{
  Arial,
  Arial Narrow//Error
}


can anyone plz help me...


Thanks
Posted
Updated 11-Apr-17 0:02am
v2

1 solution

You have several options.

Firstly you can enumerate all of the fonts installed on your system - see How to: Enumerate Installed Fonts[^]

If you want to build your own enum, you can have descriptions with spaces like this (ref:c# - Can my enums have friendly names?[^])
C#
//Requires using System.ComponentModel;
public enum FontFamilies
{
    [Description("Ariel")]
    Ariel,
    [Description("Arial Narrow")]
    ArialNarrow,
    [Description("Times New Roman")]
    TimesNewRoman
};
To get these descriptions you can do this (same ref as above):
C#
//Requires using System.Reflection;
public static string GetDescription(Enum value)
{
    Type type = value.GetType();
    string name = Enum.GetName(type, value);
    if (name != null)
    {
        FieldInfo field = type.GetField(name);
        if (field != null)
        {
            DescriptionAttribute attr =
                        Attribute.GetCustomAttribute(field,
                        typeof(DescriptionAttribute)) as DescriptionAttribute;
            if (attr != null)
            {
                return attr.Description;
            }
        }
    }
    return null;
}


An alternative method is to extend enum see - c# - Can my enums have friendly names? - Stack Overflow[^]
C#
public enum FontFamilies2
{
     Ariel,
     ArialNarrow,
     TimesNewRoman
};
C#
public static class ExtensionMethods
{
    public static string EnumValue(this FontFamilies2 e)
    {
        switch (e)
        {
            case FontFamilies2.Ariel:
                return "Ariel";
            case FontFamilies2.ArialNarrow:
                return "Ariel Narrow";
            case FontFamilies2.TimesNewRoman:
                return "Times New Roman";
        }
        return "Where did you find that value";
   }
}
And this is how you would access those descriptions respectively:
C#
System.Diagnostics.Debug.Print(GetDescription(FontFamilies.TimesNewRoman));
System.Diagnostics.Debug.Print(FontFamilies2.TimesNewRoman.EnumValue());


[EDIT OP now states they want this description as a list]
Assuming you have gone for the [Description...] attribute then you can do this:
C#
//Requires using System.Collections.Generic;
var fonts = new List<string>();
foreach (var x in Enum.GetValues(enumType: typeof (FontFamilies)))
    fonts.Add(GetDescription(x as Enum));
or the Linq version
C#
var fonts = (from object x in Enum.GetValues(enumType: typeof (FontFamilies)) select GetDescription(x as Enum)).ToList();
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 11-Apr-17 13:10pm    
5 for attributes
abdul subhan mohammed 12-Apr-17 2:55am    
Thanks Chili, But i want to display enum's description as a list, instead of enum's values. Please click on the links to check my code.

https://drive.google.com/open?id=0Bz2clgZZZsl4eUVWRUwzclBZalk

https://drive.google.com/open?id=0Bz2clgZZZsl4bWEwZllmanU4Yk0
CHill60 12-Apr-17 2:57am    
Then just put the description into the list instead of the value. I can't link to your code on this device.
abdul subhan mohammed 12-Apr-17 3:17am    
code
public EvaluateFontName(IQuestion question) : base(question, "Evaluate font name", new ModuleType[] { ModuleType.Word2013 })
{
DefineParameter(
"WordFile",
"The filename of the new file to be created",
false);

DefineParameter(
"Text",
"The text to be changed",
false);

DefineParameter(
"FontName",
"Font Name",
typeof(FontFamily));//FontFamily is enum, I want to display Description, instead of values.
}
CHill60 12-Apr-17 4:57am    
Why are you using typeof()? And what is DefineParameter? Which of the three options I suggested have you actually decided to use?
You are being too vague.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900