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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Merging 2 Word Documents

Status
Not open for further replies.

aaronino

Programmer
Aug 6, 2001
19
US
There has to be a better method than what I'm doing to merge two word documents. I'm hoping someone can point me towards that method.

I'll show the code in a bit here, but first an explanation. I have an existing Word.Document, this code is inside the class that is responsible for creating it. I'm appending another document to the end of the current document. My basic method of accomplishing this is to physically open the "other" (passed as a parameter) word document, grab the text, and then place that text at the end of my current document.

Now, the code.

Code:
/// <summary>
/// Appends the specified document to the end of the current document
/// </summary>
/// <param name="FilePath"></param>
public void AppendDocument(string FilePath)
{
	object missing = System.Reflection.Missing.Value;
	object fileToOpen = (object) FilePath;
	object FALSE = (object) false;
	object start = 0;
	object end = toMerge.Characters.Count;

	Word.Document toMerge = _WAPP.Documents.Open(ref fileToOpen, ref missing, ref missing, 
		ref missing, ref missing, ref missing, ref missing, ref missing, 
		ref missing, ref missing, ref missing, ref missing);
	
	// Select all Text in the passed Document, inserts it into this document, 
	// then closes the document
	Word.Range allText = toMerge.Range(ref start, ref end);
	this.InsertParagraph(allText.Text);
	toMerge.Close(ref FALSE, ref missing, ref missing);
	
	toMerge = null;
}

It feels very inefficient to have to actually open the Word Document, pull the data, and then close it. When you also consider I'll be doing this a few times (at most maybe 10, but still) you can see why I'd hope to make the process as quick as possible.

Well, thanks for any help. I try to stay humble, so feel free to criticize any other errors you feel are in the code and I'll try not to be too defensive.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top