Creating Charts using OpenXML and append it to a bookmark in a word document C#

Ajay Gill 1 Reputation point
2021-09-05T02:40:10.51+00:00

I need help creating a chart append it to a bookmark in a word document using OpenXML. I have tried creating a drawing object and then appending it to the document but that doesn't work. I need help from someone who can send sample code (C#) which instructs me how to properly create a chart and append it to a bookmark in a word document. Any help would greatly be appreciated.

Developer technologies C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 90,521 Reputation points
    2021-09-05T07:04:10.733+00:00
    0 comments No comments

  2. Timon Yang-MSFT 9,606 Reputation points
    2021-09-06T07:11:56.007+00:00

    This is a piece of code generated by the Open Xml SDK 2.5 Productivity Tool, just a blank word file with a bookmark, and then add a chart to the bookmark.

    Since it is too long, I will provide it to you as an attachment, hoping to provide you with some ideas.

    By the way, if it is not for special reasons such as the code must be run on a server or a machine without an office, the code written in Microsoft.Office.Interop.Word will be much simpler.

                Application oWord = new Application();  
                Document oDoc = oWord.Documents.Open(@"C:\...\1.docx");  
      
                oWord.Visible = false;  
                InlineShape oShape;  
      
                Range wrdRng = oDoc.Bookmarks[1].Range;  
                oShape = oDoc.InlineShapes.AddChart(Office.XlChartType.xlBarClustered, wrdRng);  
      
                oDoc.Save();  
                oDoc.Close();  
                oWord.Quit();  
    

    129541-1.txt


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.