Search code examples
c#word-interop

Access X axis of the chart and Set interval between labels dynamically


I have a template document that contains a chart.enter image description here

Using this template i generate a report.

It works fine when i have certain number of values, 7 in given example. but when the sample data increases the category axis looks clustered, As shown.

enter image description here

I would like to Dynamically set interval between labels to either automatic or specify the interval using formula, but i am unable to access this property.

enter image description here

I tried googling this but didn't help. I would appreciate if anybody could help me with this.


Solution

  • i have tested a solution and seems ok:

    before test my word file:

    see Axis Properties

    enter image description here

    my code:

    using Word = Microsoft.Office.Interop.Word;
    :
    :
        private void WordWithExcel()
        {
            object missing = System.Reflection.Missing.Value;
            Word.Application application = new Word.Application();
            Word.Document wordDoc = application.Documents.Add(@"d:\+test3.docx");
    
            //i suppose there is only one inline -> always begin by 1 dunno why
            Word.InlineShape shape = wordDoc.InlineShapes[1];
            Word.Chart chart = shape.Chart;
    
            var axis = chart.Axes(Word.XlAxisType.xlCategory);
            axis.TickLabelSpacingIsAuto = false;
    
            // if you want modify the spacing value
            // axis.TickLabelSpacing = 2;
    
            object filename = @"d:\++t.docx";
            wordDoc.SaveAs2(ref filename);
            wordDoc.Close(ref missing, ref missing, ref missing);
            application.Quit(ref missing, ref missing, ref missing);
        }
    

    and the result:

    enter image description here