Create new Document From Custom Template

A new word document can be made using the : Document class.
Is there a way to create a new Document from a Custom Word Template I created?

Thanks.!

Hi
Thanks for your inquiry. Yes of course, just pass path to your template into the Document constructor:

// open template
Document doc = new Document(@"Test194\in.doc");

Best regards.

Not exactly… this is what I am looking for.

We are using Custom Merge fields in our noticing templates.
While creating new templates, the user has to configure the data source (it could be a comma seperated file for all fields) and select the datasource to
enable the “Insert Mail Merge Field” option on the “Mail Merge” Toolbox. Currently when a user sets
up a document that have to select a flat file to get a listing of all
merge fields available so they can design the document. I would like to
remove the step of them having to choose the datasource and
automatically set it up for them so they can just start adding merge
fields.

For this I created a custom template and set a flat file as the data source. If I generate a document from this template manually, I can see that the “Insert Merge Field” toolbox is enabled and when the user clciks on the “Insert Merge Fields” button, the custom merge fields in the flat file are listed by Word applicaiton.

I wanted to do this programmatically.
I found that after creating a document, a template can be set to it by:
Document doc = new Document();
doc.AttachedTemplate = @“C:\Test\TestNoticingTemplate.dot”;
doc.Save(@“C:\Test\TemplateBasedNotice.doc”);

Now while opening the generated document, it asks the user permission to use the flat file data source.
But still the “Inser Merge Field” button is not enabled.
I need to manually select the data source again.

Please let me know why Aspose generated document is not picking the datasource where as the maually generated document is able to do that.

I am attaching a sample along with.
Instrctions:

  • Create a folder : C:\AsposeSample and extract the zip file contents to this folder (For the program to work, name has to be like that)

Steps to generate the document manually:

  • Double click “C:\AsposeSample\TestNoticingTemplate.dot”
  • Click YES to the dialog asking for permission to attach the data source.
    Now you can see that the “MailMerge” buttons are enabled and you can insert custom merge fields.

Steps to generate the document using Aspose:

  • From C:\AsposeSamples, run the program : CustomTemplateDocumentGeneration.exe
  • Open the document. It asks for user permission to open the data source.
    But even after that, the Merge Field buttons are not enabled.

The source code can be found in the folder inside.

Pelase let me know your feedback.

Thanks in advacnce.!

Hi
Thanks for your request. There is no way to specify Mail Merge data source using Aspose.Words, also connection to existing data source will be lost. This is the known issue #5170 in our defect database.
Best regads.

Thanks for the reply.
Do you have an idea on when this bug will be fixed?

Hi
Thanks for your request. Unfortunately, I cannot provide you any reliable estimate regarding this issue at the moment.
Best regards.

The issues you have found earlier (filed as 5170) have been fixed in this update.

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

Thanks for the fix.
I tested the fix locally and found some issues.
This is the code I used.
----------------------------------------------------------

Aspose.Words.Document newDoc = new Document();
newDoc.AttachedTemplate = @"MergeDataSource.tmp";
newDoc.Save(@"NewTemplate.doc");

----------------------------------------------------------

I couldn’t find the data source attached.
I thought it might be due to the “.tmp” file I am attaching and hence took a simple file as data source. That doesn’t give any prompts while attaching manually. I couldn’t attach even that file successfully.

I am attaching the both the merge data source files. (.tmp file is renames as .tmp.txt)
Please let me know your thoughts.

Thanks

I tested the fix locally and found some issues even with v 8.0.
This is the code I used.
----------------------------------------------------------

Aspose.Words.Document newDoc = new Document();
newDoc.AttachedTemplate = @"MergeDataSource.tmp";
newDoc.Save(@"NewTemplate.doc");

----------------------------------------------------------

I couldn’t find the data source attached.
I
thought it might be due to the “.tmp” file I am attaching and hence
took a simple file as data source. That doesn’t give any prompts while
attaching manually. I couldn’t attach even that file successfully.

I am attaching the both the merge data source files. (.tmp file is renames as .tmp.txt)
Please let me know your thoughts.

Thanks

Hi

Thanks for your request. You should use Document.MailMergeSettings property to specify data source:
https://reference.aspose.com/words/net/aspose.words/document/mailmergesettings/
Best regards.

I am sorry.
I couldn’t get it working with this code too…

Aspose.Words.Document newDoc = new Document();
newDoc.MailMergeSettings.DataSource = @"MyDataSource.txt";
newDoc.Save(@"NewTemplate.doc");

I dont see the source attached and if I load the saved document again, I see the DataSource property as empty.

Thanks

Hi

Thanks for your request. In your case, you should use the following code:

Document doc = new Document();
doc.MailMergeSettings.DataType = MailMergeDataType.Native;
doc.MailMergeSettings.MainDocumentType = MailMergeMainDocumentType.MailingLabels;
doc.MailMergeSettings.DataSource = @"C:\Temp\MyDataSource.txt";
doc.Save(@"Test001\out.doc");

Hope this helps.
Best regards.

Did you try opening the file after the data source is attached?

I am getting a dialog like this and doesn’t show the data source attached. (See the attached file)
And the document also is not bound to the data source.

I tried to clear the data source attached using the method:
newDoc.MailMergeSettings.Clear();
And I couldn’t find it working.
Is this the correct way to do that?

Thanks

Hi

Thanks for your request. Of course, I tried to open the output document and I saw the same dialog box. If you click “Yes”, MS Word will select data from your txt file.
Best regards.

Please find the detailed observations in the document attached.
I am attaching the data source file used along with.
You will get the “.tmp” file renaming the “.txt” file.

Please let me know your thoughts.

Thanks

Hi

Thank you for additional information. You should just specify Query and Field Mappings. Please see the following code:

string dataSource = @"C:\Temp\test.tmp";
Document doc = new Document();
doc.MailMergeSettings.DataType = MailMergeDataType.TextFile;
doc.MailMergeSettings.MainDocumentType = MailMergeMainDocumentType.FormLetters;
doc.MailMergeSettings.DataSource = dataSource;
doc.MailMergeSettings.Query = string.Format(@"SELECT * FROM {0}", dataSource);
Regex whiteSpaceRegex = new Regex(@"\s+");
using (StreamReader reader = new StreamReader(dataSource, Encoding.UTF8))
{
    string firstLine = reader.ReadLine();
    string[] fieldNames = firstLine.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    for (int i = 0; i < fieldNames.Length; i++)
    {
        OdsoFieldMapData mapData = new OdsoFieldMapData();
        mapData = new OdsoFieldMapData();
        mapData.Column = i;
        mapData.MappedName = fieldNames[i];
        mapData.Name = whiteSpaceRegex.Replace(fieldNames[i], "_");
        mapData.Type = OdsoFieldMappingType.Column;
        doc.MailMergeSettings.Odso.FieldMapDatas.Add(mapData);
    }
}
doc.Save(@"Test001\out.doc");

However, I found strange issue upon using your original file. If use it, MS Word does not show list of fields in the Mailings tab, but if just copy all content from your .tmp file to another file all works as expected.
Best regards.

Thanks a lot.
It works fine now.