Thanks for your inquiry. Following code example creates the new BuildingBlock and import first section a another document into the newly created BuildingBlock. Hope this helps you. I have attached the input and output documents with this post for your kind reference.
If this does not help you, please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.
Document doc = new Document();
doc.GlossaryDocument = new GlossaryDocument();
//Create new BuildingBlock
BuildingBlock bb = new BuildingBlock(doc.GlossaryDocument);
bb.Name = "Aspose.Words";
bb.Gallery = BuildingBlockGallery.AutoText;
bb.Category = "General";
bb.Description = "description";
bb.Behavior = BuildingBlockBehavior.Paragraph;
Node node = doc.GlossaryDocument.AppendChild(bb);
//import first section of in.docx into first BuildingBlock of empty document.
Document doc1 = new Document(MyDir + "in.docx");
NodeImporter imp = new NodeImporter(doc1, bb.Document, ImportFormatMode.KeepSourceFormatting);
Node impNode = imp.ImportNode(doc1.FirstSection, true);
bb.AppendChild(impNode);
doc.Save(MyDir + "Out.dotx");
//attach the template to export the document to Docx
doc.AttachedTemplate = MyDir + "Out.dotx";
doc.Save(MyDir + "Out.docx");
Hi,
thanks for the example. I ran it, but unfortunately I didn’t find the new building block in the out.dotx or the out.docx
What I need is a method to (re-)define values for given building block shortcuts. Building Blocks can get very complex, in my case it’s enough to have plain text for the replacement text.
Let me show you in pseudo code:
bb.Name = "ShortcutName";
bb.Gallery = BuildingBlockGallery.AutoText;
bb.Value = "Plain text to insert for ShortcutName";
rho:
thanks for the example. I ran it, but unfortunately I didn’t find the new building block in the out.dotx or the out.docx
Please check the attached image for new BuildingBlock. You can check BuildingBlock from Building Blocks Organizer.
rho:
What I need is a method to (re-)define values for given building block shortcuts. Building Blocks can get very complex, in my case it’s enough to have plain text for the replacement text.
You can redefine the building block values. In this case, please remove all nodes of building block and import new nodes according to your requirements. Please check my previous post for NodeImporter example.
Could you please share here your input and expected output documents? We will then provide you more information about your query along with code.
rho:
the problem was, that I had this in my code:
bb.Type = BuildingBlockType.AutoText;
which makes the building block disappear from the Building Blocks Organizer.
I have now managed to create new building blocks and edit existing ones.
It is nice to hear from you that your have found the solution of your query.
rho:
is it still true, that AUTOTEXT fields are not supported with UpdateFields() method like written here: https://docs.aspose.com/words/net/working-with-fields/
my field looks like this:
{ AUTOTEXT asposeTest * MERGEFORMAT }
Unfortunately, Aspose.Words does not support the requested feature at the moment. However, I have logged this feature request as WORDSNET-10348 in our issue tracking system. You will be notified via this forum thread once this feature is available. We apologize for your inconvenience.
rho:
Furthermore, I wonder why a MERGEFIELD is replaced with static text, while a IF field survives the call to
doc2.MailMerge.Execute(data);
the field looks like this:
{ IF “test” = “test” “yes” “no” }
Yes, this is the expected behavior of Aspose.Words. Please note that Aspose.Words mimics the same behaviour as MS Word does. If you perform mail merge process, the mail merge fields will be replaced with the field values. I suggest you, please read following documentation links for your kind reference. https://docs.aspose.com/words/net/types-of-mail-merge-operations/
tahir.manzoor:
Yes, this is the expected behavior of Aspose.Words. Please note that Aspose.Words mimics the same behaviour as MS Word does. If you perform mail merge process, the mail merge fields will be replaced with the field values. I suggest you, please read following documentation links for your kind reference. https://docs.aspose.com/words/net/types-of-mail-merge-operations/*
When I do mail merge in Word, IF fields are also replaced with static text, just like any field. So why does Aspose leave the IF field in the mail merged document when it mimics the behavior of Word?
Please use MailMerge.CleanupOptions property with MailMergeCleanupOptions.RemoveContainingFields flag to remove fields that contain merge fields (for example, IFs).
RemoveContainingFields :
Specifies whether fields that contain merge fields (for example, IFs)
should be removed from the document if the nested merge fields are
removed.
I am afraid this issue WORDSNET-10348 has now been postponed till a later date due to some other important issues and new features. We will inform you as soon as there are any further developments.
Following code example shows how to insert the AutoText field into document and update it. The input.dotx should contain the building block with name “Test”. Hope this helps you.
Document doc = new Document();
doc.AttachedTemplate = MyDir + "input.dotx";
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();
builder.Writeln("Insert field AutoText");
Field field = builder.InsertField("AutoText Test");
doc.UpdateFields();
doc.Save(MyDir + "Out.docx");
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
Enables storage, such as cookies, related to analytics.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.