Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9272137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:47:50+00:00 2026-06-18T15:47:50+00:00

I used AltChunk object to copy data from a docx file to a rich

  • 0

I used AltChunk object to copy data from a docx file to a rich text content control in another file. The copy works fine. But now the content control cannot be cast into a SdtElement in OpenXml nor to a ContentControl in VSTO.

This is the code I used

SdtElement sdtElement = destinationdocument.MainDocumentPart.Document.Body.Descendants<SdtElement>().Where(b => b.SdtProperties.GetFirstChild<Tag>() != null).FirstOrDefault();
string altChunkId = "AltChunkId" + Guid.NewGuid().ToString();
AlternativeFormatImportPart chunk = destinationdocument.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImport    PartType.WordprocessingML, altChunkId);
chunk.FeedData(File.Open("sourceFile", FileMode.OpenOrCreate));
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
sdtElement.RemoveAllChildren();
sdtElement.Append(altChunk);

the first time the code works fine. But at the second run the first line throws an unable to cast exception. The same problem occurs while using VSTO at the client side the ContentControl object cannot hold the content control in which the AltChunk was inserted. Somehow this procedure corrupts the rich text content control.

Is there anything I am doing wrong? Or is there a better alternative?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:47:52+00:00Added an answer on June 18, 2026 at 3:47 pm

    wordDocument.MainDocumentPart.Document.Body.Descendants<SdtElement>() returns IEnumerable<SdtElement> and you are assiging it to SdtElemtnt. Try using var or the actual return type.

    Update:

    Your code is a working one. What you are doing wrong is this line sdtElement.RemoveAllChildren();

    An sdt element (Content control) contains other elements like sdtPr (content control properties), sdtContent (the actual content inside the content control) etc. as in the below eg.

     <w:sdt>
        <w:sdtPr>
            ...
        </w:sdtPr>
        <w:sdtContent>
          ....
        </w:sdtContent>
      </w:sdt>
    

    What your sdtElement.RemoveAllChildren(); doing is to delete everything inside the sdt element and replacing them as:

    <w:sdt>
       <w:altChunk r:id="AltChunkIdffebf242-30b3-4905-bf39-fc0077be9474" />
    </w:sdt>
    

    Which is making your program to throw exception on secondrun as in line destinationdocument.MainDocumentPart.Document.Body.Descendants<SdtElement>().Where(b => b.SdtProperties.GetFirstChild<Tag>() != null).FirstOrDefault(); your replaced document sdt element has no SdtProperties and also no Tag or sdtContent.

    To workaround this problem try inserting your altchunk block into the content control content element (sdtContent) instead of the sdt element directly as below:

    using (
        FileStream fileStream = File.Open("file.docx",
                                            FileMode.Open))
    {
        chunk.FeedData(fileStream);
        AltChunk altChunk = new AltChunk();
        altChunk.Id = altChunkId;
        //sdtElement.RemoveAllChildren();
        sdtElement.Elements<SdtContentBlock>().FirstOrDefault().Append(altChunk); // This is going to add to the existing content.
    }
    

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used Session[EmpName] = Convert.ToString(Request.QueryString[1]); lblEmployeeName.Text = Session[EmpName].ToString; to show the data in label
I used the AVAudioPlayer to play a 10 sec wav file and it works
Anybody used Neural Network approaches for clustering data? Particularly ART Neural Network (Adaptive Resonance
I used the phonegap website to create an IPA file. Now I want to
I used the following code for generating pdf file and its contents. Initially I
I used the following tutorial to make a parallax content slider for my webiste:
Used and modified the code below. Works exactly the way I need it too,
Used http://www.ilbcfreeware.org/software.html - I only get static from the files that ilbc_test.exe creates. Does
I used pop up in buttons using above code in asp.net. every thing works
I used Red Gates SVN Source Control to add 2 databases (one branched off

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.