Insert on database contents of word file using OpenXML and C#

Home Forums Open-Xml-Sdk Insert on database contents of word file using OpenXML and C#

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #9709
    sanjaysingh
    Participant

    Contact RKSA Packers & Movers Company for your transfer, as we provide the best and cheapest packing and moving service. We are verified and professional packers and movers companies.

    #9762
    Anonymous
    Inactive

    Looking at your document and your code, I see two places that could be the source of your problem:

    First: the xml layout for your SecondTemplate.docx containing Bookmark1 is like so:

    <Paragraph>
    <Bookmarkstart name=bookmark1/>
    <Run>
    <Text “Item 1”>
    </Run>
    </Paragraph>
    <Paragraph>
    <Run>
    <Text “Item 2”>
    </Run>
    </Paragraph>
    <Paragraph>
    <Run>
    <Text “Item 3”>
    </Run>
    </Paragraph>
    <Paragraph>
    <Run>
    <Text “Item 4”>
    </Run>
    <Bookmarkend/>
    </Paragraph>
    and your code here:

    if(bookmarkStart.Name == bookmarkKey)
    {
    foreach(Run run in bookmarkStart.Parent.Descendants<Run>())
    {
    returnVal += run.Descendants<Text>().FirstOrDefault().Text + “<br/>”;
    }
    }
    when the bookmarkstart.Parent call runs, it matches on the Paragraph that is directly above the bookmark :

    <Paragraph>
    <Bookmarkstart name=bookmark1/>
    <Run>
    <Text “Item 1”>
    </Run>
    </Paragraph>
    so when the rest of the loop executes, you only get the “Item 1” pulled into your merge process. You need to re-work your logic to correctly match the Text in the Run for all four paragraphs between the BookmarkStart and BookmarkEnd.

    Second: Another issue that often trips people up in OpenXml is when you are trying to match the Run in the Descendants call here:

    bookmarkStart.Parent.Descendants<Run>
    If you are referring to the DocumentFormat.OpenXml.Drawing.Run , not the correct ‘DocumentFormat.OpenXml.Wordprocessing.Run’, this can prevent a match – so mouse over that Run in Visual Studio and ensure you are matching the correct Run. Adjust your using statements to get the correct one. A Using statement like

    using Run = DocumentFormat.OpenXml.Wordprocessing.Run;
    is often used depending on the rest of your code in that file. Hope these clues help you.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.