Problem setting the Text property of a Characters object in Excel Interop using Visual C#

ThreeNamesGrace

New member
Joined
Mar 20, 2023
Messages
3
Programming Experience
3-5
I'm making ChartSheets in Excel using .NET 7 and the latest update of Office 365; this is the code that I use to change the style of the charts and give them a title:

C#:
GráficasFrecuenciaGanancia[i - 1].ChartType = XlChartType.xlLineMarkers;
GráficasFrecuenciaGanancia[i-1].HasTitle = true;
Excel.Characters TítuloGráfica = null;
string dummyString = null;
dummyString = Convert.ToString(allTheCells.Item[1 + ((i - 1) * 54), 1].Value2);
TítuloGráfica = GráficasFrecuenciaGanancia[i - 1].ChartTitle.Characters;                  try
{
    TítuloGráfica.Text = dummyString;
}
catch(Exception ex)
{
    MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK);
    Debug.Write(ex.ToString());
}

In certain ChartSheets it gives the following exception (sorry, bits are in Spanish):

C#:
System.Runtime.InteropServices.COMException (0x800A03EC): No se puede asignar la propiedad Text de la clase Characters.
    at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
    at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Object[] aArgs, Boolean[] aArgsIsByRef, Int32[] aArgsWrapperTypes, Type[] aArgsTypes, Type retType)
    at Microsoft.Office.Interop.Excel.Characters.set_Text(String )
    at Graficador___v3.Form1.GraficadorFrecuenciaGanancia_Click(Object sender, EventArgs e) in C:\Users\user\OneDrive\Escritorio\Proyecto\Visual Studio\Graficadores\Graficador - v3\Form1.cs:line 247Excepción producida: 'System.Runtime.InteropServices.COMException' en System.Private.CoreLib.dll System.Runtime.InteropServices.COMException (0x800A03EC): No se puede asignar la propiedad Text de la clase Characters.
    at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
    at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Object[] aArgs, Boolean[] aArgsIsByRef, Int32[] aArgsWrapperTypes, Type[] aArgsTypes, Type retType)
    at Microsoft.Office.Interop.Excel.Characters.set_Text(String )
    at Graficador___v3.Form1.GraficadorFrecuenciaGanancia_Click(Object sender, EventArgs e) in C:\Users\user\OneDrive\Escritorio\Proyecto\Visual Studio\Graficadores\Graficador - v3\Form1.cs:line 247 El programa '[22776] Graficador - v3.exe' terminó con código 4294967295 (0xffffffff).

This line:
C#:
System.Runtime.InteropServices.COMException (0x800A03EC): No se puede asignar la propiedad Text de la clase Characters.

Says that I cannot assign the Text property from a Characters instance. I tried many things, like repairing Visual Studio (I'm using the Community version, 2022), repairing Office, downloading the latest version of Microsoft Visual C++ Redistributable (2015-2022), updating Windows 11, changing the order of the code (applying data first and title at the end), and even downloading and installing all the redistribuitables, but I couldn't fix it; I looked up the HResult of the exception: 0x800A03EC, but most of the answers were for code written for servers (I'm not working on a server, I am working on an application to make graphs for a lot of data that I'm using at work).

Here is my full and most recent code in GitHub.
 
I tried many things, like repairing Visual Studio (I'm using the Community version, 2022), repairing Office, downloading the latest version of Microsoft Visual C++ Redistributable (2015-2022), updating Windows 11, changing the order of the code (applying data first and title at the end), and even downloading and installing all the redistribuitables, but I couldn't fix it

It is unlikely that those actions would correct what is a fundamental flaw in the usage of C# here. You're trying to assign a value to a property that is read only. No amount of repairing visual studio is going to resolve it
 
It is unlikely that those actions would correct what is a fundamental flaw in the usage of C# here. You're trying to assign a value to a property that is read only. No amount of repairing visual studio is going to resolve it

I don't think it is read-only. Not at the type level, anyway. That would generate a compilation error and so I checked the documentation and that Text property has a getter and a setter. There may be something under the hood that means it can't be set in this specific case but I'm not sure exactly what that would be. The documentation does show the specific property I suggested being set though, so I think that one might do the trick.
 
This documentation shows .ChartTile.Text being set, rather than .ChartTitle.Characters.Text. Maybe try that.

With .ChartTitle.Text I had the same issue, but now I solved it, it was the character limit, I had 260 characters to assign to the text, and even the text in the cell had a lot of escape characters (a lot of tabs, returns and new lines) that I had to trim.
 
It is unlikely that those actions would correct what is a fundamental flaw in the usage of C# here. You're trying to assign a value to a property that is read only. No amount of repairing visual studio is going to resolve it

Actually, the property is not read-only, because I could assign it to other charts, the assignment failed in certain charts because the string that I was assigning was 260 characters long, and it had a lot of escape characters that I had to trim. It seems like theres is a limit to the characters that I can have, or its length in points, idk.
 
Characters property of ChartTitle is readonly, but you can get the object from Characters property and modify it. It is mostly used for formatting part of a range text.
 

Latest posts

Back
Top Bottom