Hello,
I'm developing a Macro to create some ColorStyles and Apply this ColorStyles to Shapes. To Create a ColorStyle is not the problem. But how to Apply it to a shape? I doesnt find a solution four approx. four hours
corel.Shape shape = ActiveSelection;corel.Color color = CreateColor("#ff0011");corel.Color colorstyle = ActiveDocument.StyleSheet.CreateColorStyle("colorRed", color);shape.Fill.ApplyUniformFill(color); // this is only the color, not the ColorStyle - I knowforeach (corel.Color item in ActiveDocument.StyleSheet.AllColorStyles){ Debug.WriteLine("Name is {1} with Hex {0}", item.HexValue, item.ColorStyleName); // Output is: Name is colorRed with Hex #FF0011 shape.Fill.UniformColor = item; // Apply ColorStyle to Shape}
So far so good. But if I have more then one ColorStyle I want to add specific Style by Name. How can I do this?Nico
I am not aware of a way to directly access a ColorStyle by name using the API.
You could "walk through" the Colors in ActiveDocument.StyleSheet.AllColorStyles and check the Color names until you found a match.
Here's a VBA example where I do it that way in a function:
Sub test_GetColor_from_Color_Style_Name() Dim colorThis As Color Set colorThis = GetColor_from_Color_Style_Name("colorRed") If Not colorThis Is Nothing Then ActiveShape.Fill.ApplyUniformFill colorThis Else MsgBox "Color style not found." End If End Sub Function GetColor_from_Color_Style_Name(ByVal ColorStyleName As String) As Color Dim colorsThese As Colors Dim colorThis As Color Set colorsThese = ActiveDocument.StyleSheet.AllColorStyles For Each colorThis In colorsThese If colorThis.Name = ColorStyleName Then Set GetColor_from_Color_Style_Name = colorThis Exit For End If Next colorThis End Function
I looped through ActiveDocument.StyleSheet.AllColorStyles too, but I have to do this for a lot of symbols with a lot of shapes per file. I think it is not the best practices to loop through all ColorStyles for each Shape but yes .. it will work.
Your post helped me to realize that a foreach is not bad - one time
So in C# I will now use a Dictionary for a addressable list of ColorStyles.
Example:
Shape shape = ActiveSelection; // Optional Create new Colors and ColorStyles and save this in a variables Color color1 = CreateColor("#ff0011"); ActiveDocument.StyleSheet.CreateColorStyle("colorRed", color1); Color color2 = CreateColor("#11ff00"); ActiveDocument.StyleSheet.CreateColorStyle("colorGreen", color2); // Create a Directory (in C#) with Key as string and Value as Color var dictColorStyles = new Dictionary<string, corel.Color>(); // push all ColorStyles from StyleSheet into the Dictionary by Name and Color foreach (corel.Color item in DockerUI.CorelApp.ActiveDocument.StyleSheet.AllColorStyles) { dictColorStyles.Add(item.ColorStyleName, item); } // Than I can use the Styles by Name with the Dictionary shape.Fill.UniformColor = dictColorStyles["colorGreen"];