How to copy builtin buildingblock to another file

the microsoft built-in buildingblock file include so many buildingblock, in my situlation, i want to use some of built-in buildingblock and some custom buildingblock, so i want to make a new file and include all of them.

i use below code but will throw a exception:

private string outfile = @"C:\Users\CHN\Desktop\testDoc\Building Blocks.dotx";
private void btnCopyOut_Click(object sender, EventArgs e)
{
    MemoryStream ms = new MemoryStream();
    ADoc.Document docCopyOut = new ADoc.Document(ms);
    docCopyOut.GlossaryDocument = new ADoc.BuildingBlocks.GlossaryDocument();
    ADoc.Document builtinBuildingBlocks = new ADoc.Document(builtinBuildingBlock);
    ADoc.BuildingBlocks.GlossaryDocument glossaryDocument = builtinBuildingBlocks.GlossaryDocument;
    foreach(ADoc.BuildingBlocks.BuildingBlock buildingBlock in glossaryDocument.BuildingBlocks)
    {
        if (buildingBlock.Gallery.ToString().StartsWith("CoverPage"))
        {
            ADoc.Node node = docCopyOut.ImportNode(buildingBlock, true);
            docCopyOut.GlossaryDocument.BuildingBlocks.Add(node);
        }
    }
    docCopyOut.Save(outfile, ADoc.SaveFormat.Dotx);
}

exception picture

is any wrong in my code?

in use 14.2

thanks

some additional information:
the built-in buildingblock file is locate in :

"C:\Users\xxx\AppData\Roaming\Microsoft\Document Building Blocks\1033\15\Built-In Building Blocks.dotx"

thanks

Hi Pyntia,

Thanks for your inquiry. You cannot just add a node from one document to another because a node depends on its parent document styles, lists, etc. You can, however, import a node from one document into another. Importing a node creates a clone of the original node that belongs to the destination document. Then, you can add the cloned node to the destination document.

Please check the following code snippet for your kind reference. Hope this helps you.

foreach(BuildingBlock buildingBlock in glossaryDocument.BuildingBlocks)
{
    if (buildingBlock.Gallery.ToString().StartsWith("CoverPage"))
    {
        // clone section which you want to keep layout of
        Section sec = docCopyOut.FirstSection.Clone();
        // remove existing content of this section
        sec.RemoveAllChildren();
        // append content of buildingBlock to this section
        foreach(Node node in ((Section) buildingBlock.FirstChild).ChildNodes)
        sec.AppendChild(docCopyOut.ImportNode(node, true));
        docCopyOut.AppendChild(sec);
        break;
    }
}

hi, Tahir

there is some misunderstand about this question, let me talk about it more clearly.

you code be workable when inserting buildingblocks to a normal document, but i don’t want to inserting buildingblocks to a normal document, i just want to making myself custom buildingblock template file.

my real purpose is reading the microsoft built-in buildingblock file and extract some of them to another custom *.dotx template file.

thanks

Hi Pyntia,

Thanks for sharing the detail. Unfortunately, Aspose.Words does not support the requested feature at the moment. However, I have logged this feature request as WORDSNET-9941 in our issue tracking system. You will be notified via this forum thread once this feature is available.

We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-9941) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.

hi, Tahir

after receive the message post by Aspose Notifier, i try extract buildingblock and copy to another template file, but the exception will showing again.

is my code have mistakes?

i use the latest 14.4

then, i change strategy from copy to remove, below code can be work:

// remove unnecessary strategy
ADoc.Document builtinBuildingBlocks = new ADoc.Document(sourceTemplate);
ADoc.Document docCopy = builtinBuildingBlocks.Clone();
ADoc.BuildingBlocks.GlossaryDocument glossaryDocument = docCopy.GlossaryDocument;
for (int i = glossaryDocument.BuildingBlocks.Count; i> 0; i--)
{
    if (!blockTypes.Contains(glossaryDocument.BuildingBlocks[i - 1].Gallery.ToString()))
    {
        glossaryDocument.BuildingBlocks.RemoveAt(i - 1);
    }
}
docCopy.Save(destTemplateFile, ADoc.SaveFormat.Dotx);

Hi Pyntia,

Thanks for your inquiry. It is nice to hear from you that you have found the solution of your query by removing the Building Blocks from existing template document. However, we are in communication with our development team about your query and will get back to you as soon as possible.

hi, Tahir

thank you very much

i also want to using copy strategy, because sometimes should combine some template file.

Hi Pyntia,

Thanks
for your patience. Please use the following code example to import Building Blocks from one document to another. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document();
doc.GlossaryDocument = new GlossaryDocument();
doc.GlossaryDocument.BuildingBlocks.Add(new BuildingBlock(doc.GlossaryDocument));
var docCopyOut = new Document(MyDir + @"Built-In Building Blocks.dotx");
GlossaryDocument glossaryDocument = docCopyOut.GlossaryDocument;
foreach(BuildingBlock buildingBlock in glossaryDocument.BuildingBlocks)
{
    if (buildingBlock.Gallery.ToString().StartsWith("CoverPage") && buildingBlock.Name == "Cubicles")
    {
        Node node = doc.GlossaryDocument.ImportNode(buildingBlock.FirstChild, true);
        doc.GlossaryDocument.FirstBuildingBlock.Name = "AsposeWords";
        doc.GlossaryDocument.FirstBuildingBlock.Category = "AsposeWords";
        doc.GlossaryDocument.FirstBuildingBlock.ChildNodes.Add(node);
        break;
    }
}
doc.Save(MyDir + @"out.dotx");

hi, Tahir

your code work perfect, thank you very much.

Hi Pyntia,

Thanks
for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.