12
I need to get the contents of a file docx as template, which contains as content a text and some identifiers to be changed with other data, and from that template create a single other docx with the same content but replicated and the appropriate values modified.
As an example, I have in my document a text containing the following content:
Hello
<NOME>
! Welcome home ... Your address<ENDERECO>
.
And then I need to create a single other docx containing something like:
Hello
Fulano 1
! Welcome home ... Your addressENDERECO 1
.
HelloFulano 2
! Welcome home ... Your addressENDERECO 2
.
HelloFulano 3
! Welcome home ... Your addressENDERECO 3
.
Where each line would be at the beginning of a new page.
That’s what I’m trying to do (TAKING A TEST) using the plugin Docx installed in the project via Nuget.
Another way to accomplish the process will be welcome!
My example for problem presentation:
public static void Main(string[] args)
{
var dictionary = new Dictionary<string, string>();
dictionary.Add("Fulano 1", "Endereco 1");
dictionary.Add("Fulano 2", "Endereco 2");
dictionary.Add("Fulano 3", "Endereco 3");
var template = new FileStream("D:\\template.docx", FileMode.Open, FileAccess.Read);
var docs = new List<DocX>();
var x = 0;
foreach (var item in dictionary)
{
x++;
var doc = DocX.Create("D:\\temp_NUM.docx".Replace("NUM", x.ToString()));
doc.ApplyTemplate(template, true);
doc.ReplaceText("<NOME>", item.Key);
doc.ReplaceText("<ENDERECO>", item.Value);
doc.Save();
docs.Add(doc);
}
var newDoc = DocX.Create("D:\\newDoc.docx");
foreach (var doc in docs)
newDoc.InsertDocument(doc);
newDoc.Save();
}
Although it is using a simple data source, built with a Dictionary
, the source of the data will be the database of the system where this should be implemented.
The archives temp_1.docx, temp_2.docx and temp_3.docx are created and the content appears correctly.
But on the line newDoc.InsertDocument(doc);
in:
foreach (var doc in docs)
newDoc.InsertDocument(doc);
I get the following error: The sequence contains no elements. And with that the "merge" is not done.
How can I solve the problem?
Um... sounds pretty simple (and you didn’t mention if the data has a specific origin besides manually informed). So why didn’t you just use the Direct Mail in Directory native to MS Word?
– Luiz Vieira
The link from my previous comment has the information but you find tutorials like this on Youtube that do step by step. I did not find a tutorial in Portuguese, but what it is doing you find in the "Correspondence" tab, "Start Direct Mail" button, "Direct Mail Wizard Step by Step".
– Luiz Vieira
Anyway, my question was more in the sense of why you’re trying to develop something for it. Is there any additional reason (like, for example, you have a large number of docx files and so want to automate the process)? If so, it might be easier to use the Office API, as suggested in a reply, or even the [tag:vba] directly from Word...
– Luiz Vieira
Sorry, but it is not clear. Which software requirement? What is actually this requirement? Because, as I said, to do this that you ask in the question (and even send by email or print) is something that MS Word ALREADY DOES without having to implement anything. See this other video here: https://www.youtube.com/watch?v=kUPmA8e549s
– Luiz Vieira
I was the one who voted against the question, because I think it is poorly worded. Although well written, the way you are asking does not make sense (at least for me), because your "system" is generating direct mail using Word, which already does it alone. I tried to ask to better understand (or make you improve the issue in this sense), but the arguments "of course I need this" and "my software has to do this" are not really useful to the community because they simply don’t describe their real need. This is why I voted no.
– Luiz Vieira
If you try to improve the question, I promise to at least take my negative vote. :)
– Luiz Vieira
Let us continue this Discussion in chat.
– Luiz Vieira