Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Appending Word files in C#

Status
Not open for further replies.

Durandal66

Programmer
Oct 3, 2003
17
US
Here is my situation. I have a collection of word files (.doc) that include tables, pics, headers, bullets, pretty much a little of everything. I need to select a set of these files, say 2 or 3, and then combine them together in order, add a table of contents, and save them as a new unique file. I already have everything needed to select and order the files, but i am having trouble with the combining of the files. I need to preserve their contents as well, so no dumping the tables or images. I have been working with the Word Component Class, but have not been able to successfully carry this out (though I have learned a lot of stuff that might come in handy on a rainy day). Anyone have any ideas or know of a good tutorial that would have some tips?

Thanks
-Andrew R.
 
I found a way to make this work! What I do is first create a new file that I will output. I save it with the settings I want, close it, and then re-open it. (Not 100% sure that I need to do this, but it works). I then open the first file that I would like to append, select it all, and copy it to the clipboard. I bring the final document to the front, and paste to it. I repeat this process with a second file as well. When completed, I save the new output file, and do garbage collection on the currently running WINWORD.EXEs that are still hiding. Since I never make Word viewable, the user will never see it, but it never really quits, and its processes will still be running in the Task Manager (go take it look!). This final garbage collection will take care of that and free up your resources. I saw this on a different post but I forgot to save the link, so if this is your garbage collection, let me know so I can give you credit! Another good reference that pointed me toward using copy and paste can be found here:



OK, Here is my sample code for the above description:

object fileName1 = "...YourFile1.doc";
object fileName2 = "...YourFile2.doc";
object saveName = "...YourOutput.doc";
object missing = System.Reflection.Missing.Value;
object myFalse = false;
object myTrue = true;
object saveType = Word.WdSaveFormat.wdFormatDocument;

// Create the final output file (cDoc)
Word.Document cDoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);

// Save output file
cDoc.SaveAs(ref saveName, ref saveType, ref myFalse,ref missing, ref myFalse, ref missing, ref myFalse,
ref myFalse, ref myFalse, ref myFalse, ref myFalse);

// Close output file
cDoc.Close(ref myFalse, ref myFalse, ref myFalse);
cDoc = null;

// Open output file again for pasting
cDoc = WordApp.Documents.Open(ref saveName, ref myFalse,ref myFalse, ref myFalse, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref myTrue);

// Open first file (aDoc)
Word.Document aDoc = WordApp.Documents.Open(ref fileName1, ref myFalse,ref myTrue, ref myFalse, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref myTrue);

// Select all, copy
aDoc.ActiveWindow.Selection.WholeStory();
aDoc.ActiveWindow.Selection.Copy();

// Activate output file, paste values
cDoc.Activate();
cDoc.ActiveWindow.Selection.Paste();

// Close first file
aDoc.Close(ref myFalse, ref myFalse, ref myFalse);
aDoc = null;

// Open second file (bDoc)
Word.Document bDoc = WordApp.Documents.Open(ref fileName2, ref myFalse,ref myTrue, ref myFalse, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref myTrue);

// Activate second file, select all, copy
bDoc.Activate();
bDoc.ActiveWindow.Selection.WholeStory();
bDoc.ActiveWindow.Selection.Copy();

// Activate output file, paste
cDoc.Activate();
cDoc.ActiveWindow.Selection.Paste();

// Close second file
bDoc.Close(ref myFalse, ref myFalse, ref myFalse);
bDoc = null;

// Save output doc
cDoc.SaveAs(ref saveName, ref saveType, ref myFalse,ref missing, ref myFalse, ref missing, ref myFalse, ref myFalse, ref myFalse, ref myFalse, ref myFalse);

// Kill WINWORD leftovers
WordApp.Quit(ref myTrue, ref myFalse, ref myFalse);
if (WordApp!=null) System.Runtime.InteropServices.Marshal.ReleaseComObject (WordApp);

if (aDoc!=null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(aDoc);

if (bDoc!=null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(bDoc);

if (cDoc!=null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(cDoc);

WordApp = null;
aDoc = null;
bDoc = null;
cDoc = null;
GC.Collect();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top