Hi! I have used the interop dll to open a template dotx, substitute the merge fields with the values I want, and save it in pdf.
Now, I need to do this operation for several template files, and print only ONE file PDF with all the previous files concatenated. I've seen that there's the method InsertFile for the word application class, but it requires a file path, and so I have to
save all the files, and re-open them one by one.
Is there a way to merge all the files without doing it? I do something like this
Application wordApp = new Application();
Document wordDoc = new Document();
wordDoc = wordApp.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
and finally in wordApp.Documents I have all the document. But I can print only one document for time (wordApp.Documents[0].SaveAs(......) and so on).
I Hope I have been clear with my question, sorry for my bad english.
Thank you in advance
yes, you've right, this is what I want to do. But I've already found this. I wanted to know if it was possible to do this whitout starting from the docx file saved in a File System.
I start from more dotx files, e I wanted to create a single file in pdf, without having to save every single docx, re-open them, merge and finally save them in a pdf.
This toolkit has the capability to do Mail-Merge (populate placeholders on a template) as well as document merging/combining - but it is a commercial one.
I've just resolved the problem of merging using OpenXML. I wanted to use Interop to save the file in PDF, but I think i will use other libraries to do this...thank you all!
Marked as answer by Mark - MSFT on Dec 02, 2012 11:53 PM
dindina
Member
2 Points
11 Posts
Merging two word files in one
Nov 22, 2012 04:08 PM|LINK
Hi! I have used the interop dll to open a template dotx, substitute the merge fields with the values I want, and save it in pdf.
Now, I need to do this operation for several template files, and print only ONE file PDF with all the previous files concatenated. I've seen that there's the method InsertFile for the word application class, but it requires a file path, and so I have to save all the files, and re-open them one by one.
Is there a way to merge all the files without doing it? I do something like this
and finally in wordApp.Documents I have all the document. But I can print only one document for time (wordApp.Documents[0].SaveAs(......) and so on).
I Hope I have been clear with my question, sorry for my bad english.
Thank you in advance
Paul Linton
Star
13421 Points
2535 Posts
Re: Merging two word files in one
Nov 22, 2012 08:41 PM|LINK
Try asking your question in a forum concerned with programming Word rather than this one which is concerned with web programming.
dindina
Member
2 Points
11 Posts
Re: Merging two word files in one
Nov 23, 2012 07:13 AM|LINK
ok...thank you
Mark - MSFT
Contributor
7071 Points
435 Posts
Microsoft
Re: Merging two word files in one
Nov 29, 2012 08:40 AM|LINK
Hi,
I not sure how your project looks like, I guess it might be two
1. you have a project, user can upload files or select files then merge
2. those word files have been there, you want merge them automatically
For first one, require file path, and load them to merge, those operations are necessary
For second one, I think you can write some code to get those files automatically
Anyway, file path and file load still necessary, for better to do your project, those blogs may help you:
http://www.beansoftware.com/ASP.NET-FAQ/Get-List-Files.aspx // for auto get files
http://www.codeproject.com/Articles/2737/File-Split-Merge-Tool
http://mwalimuscorner.blogspot.com/2008/11/c-tutorial-combine-multiple-word.html
Best Regards
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
Pandalin
Member
34 Points
6 Posts
Re: Merging two word files in one
Nov 30, 2012 01:37 AM|LINK
dindina, if I am right, certainly you want to merge two word files and then, convert to PDF. as for merge task, you can use below code:
private void button1_Click(object sender, EventArgs e) { string fileName = OpenFile(); string fileMerge = OpenFile(); if ((!string.IsNullOrEmpty(fileName)) && (!string.IsNullOrEmpty(fileMerge))) { //Create word document Document document = new Document(); document.LoadFromFile(fileName,FileFormat.Doc); Document documentMerge = new Document(); documentMerge.LoadFromFile(fileMerge, FileFormat.Doc); foreach( Section sec in documentMerge.Sections) { document.Sections.Add(sec.Clone()); } //Save doc file. document.SaveToFile("Sample.doc", FileFormat.Doc); //Launching the MS Word file. WordDocViewer("Sample.doc"); } } private string OpenFile() { openFileDialog1.Filter = "Word Document (*.doc)|*.doc"; openFileDialog1.Title = "Choose a document to merage"; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { return openFileDialog1.FileName; } return string.Empty; } private void WordDocViewer(string fileName) { try { System.Diagnostics.Process.Start(fileName); } catch { } }You can see details here:Word Merge for C#, VB.NET
As for Word to PDF task, you can see this article:Convert Doc/Docx to PDF in C#
Hope I do not misunderstood you. and hope it can help you.
dindina
Member
2 Points
11 Posts
Re: Merging two word files in one
Nov 30, 2012 07:16 AM|LINK
yes, you've right, this is what I want to do. But I've already found this. I wanted to know if it was possible to do this whitout starting from the docx file saved in a File System.
I start from more dotx files, e I wanted to create a single file in pdf, without having to save every single docx, re-open them, merge and finally save them in a pdf.
RameshRajend...
Star
7983 Points
2099 Posts
Re: Merging two word files in one
Nov 30, 2012 07:26 AM|LINK
Hi,
Check the below links. There are more than one way to achive this.
http://mwalimuscorner.blogspot.com/2008/11/c-tutorial-combine-multiple-word.html
http://nishantrana.wordpress.com/2008/07/17/inserting-or-appending-document-or-file-in-a-word-document-programmatically-using-c/
http://stackoverflow.com/questions/247666/how-can-i-programatically-use-c-sharp-to-append-multiple-docx-files-together
http://stackoverflow.com/questions/444309/what-would-be-the-fastest-way-to-concatenate-three-files-in-c
http://www.codeproject.com/KB/office/mswmergecs.aspx
http://devpinoy.org/blogs/keithrull/archive/2007/05/23/how-to-merge-multiple-microsoft-word-documents-in-c.aspx
</div>Jure.Les
Member
376 Points
69 Posts
Re: Merging two word files in one
Nov 30, 2012 08:56 AM|LINK
This toolkit has the capability to do Mail-Merge (populate placeholders on a template) as well as document merging/combining - but it is a commercial one.
dindina
Member
2 Points
11 Posts
Re: Merging two word files in one
Nov 30, 2012 09:40 AM|LINK
I've just resolved the problem of merging using OpenXML. I wanted to use Interop to save the file in PDF, but I think i will use other libraries to do this...thank you all!