Excel VSTO C# - 格式化雷达图表类别和轴标签

时间:2022-03-23 20:24:23

I'm trying to change the format of the text on a radar chart's axis and category labels, my code works for the axis font size and gives the desired output but I'm also receiving an error once the code runs on the line ax.Format.TextFrame2.TextRange.Font.Size = 7;

我正在尝试更改雷达图表轴和类别标签上的文本格式,我的代码适用于轴字体大小并提供所需的输出,但是一旦代码在线轴上运行,我也会收到错误。 Format.TextFrame2.TextRange.Font.Size = 7;

Error from debug:

调试出错:

An exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll but was not handled in user code. Additional information: Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

mscorlib.dll中出现“System.Runtime.InteropServices.COMException”类型的异常,但未在用户代码中处理。附加信息:未指定的错误(来自HRESULT的异常:0x80004005(E_FAIL))

How do I change the category labels and what might be causing the error here once the action is completed?

操作完成后,如何更改类别标签以及可能导致错误的原因?

Here's the code:

这是代码:

public static void FormatRadarLabels(Excel._Application xlApp)
{
    Excel.Chart chart = null;
    xlApp.ScreenUpdating = false;

    try {

        chart = xlApp.ActiveChart as Excel.Chart;

        foreach (Excel.Axis ax in chart.Axes()) {
            ax.Format.TextFrame2.TextRange.Font.Size = 7;
        }
    }
    finally {
        if (chart != null) Marshal.ReleaseComObject(chart);
        xlApp.ScreenUpdating = true;
    }

}

The method is called by a method in another class which is called by a button on an excel toolbar but I don't think the error has any issues with anything external as if I comment or removed this piece of code the rest of the formatting works fine.

该方法由另一个类中的方法调用,该方法由excel工具栏上的按钮调用,但我不认为该错误与外部任何问题有任何问题,就像我评论或删除这段代码一样,其余的格式化工作精细。

Thank you for any help or suggestions offered. Please bear in mind I am relatively new (6 months experience) to C#, VSTO and Visual Studio so may not understand all technical jargon. I'm also happy to provide further information if required. :)

感谢您提供的任何帮助或建议。请记住,我对C#,VSTO和Visual Studio相对较新(6个月的经验),所以可能无法理解所有技术术语。如果需要,我也很乐意提供进一步的信息。 :)

1 个解决方案

#1


0  

What property exactly fires an exception?

什么属性正好引发异常?

ax.Format.TextFrame2.TextRange.Font.Size = 7;

ax.Format.TextFrame2.TextRange.Font.Size = 7;

I'd suggest starting from breaking the chain of property and method calls and declaring them on separate lines. Thus, you will find what property or method exactly fires an exception.

我建议从打破属性和方法调用链并在单独的行上声明它们开始。因此,您将找到准确触发异常的属性或方法。

#1


0  

What property exactly fires an exception?

什么属性正好引发异常?

ax.Format.TextFrame2.TextRange.Font.Size = 7;

ax.Format.TextFrame2.TextRange.Font.Size = 7;

I'd suggest starting from breaking the chain of property and method calls and declaring them on separate lines. Thus, you will find what property or method exactly fires an exception.

我建议从打破属性和方法调用链并在单独的行上声明它们开始。因此,您将找到准确触发异常的属性或方法。