Word.ApplicationClass

S

S.Kartikeyan

Hai,
Has any one used Microsoft Office Word in C# Application.
How can i combine two word files into a single word file
using the Word.Application and Word.Document classes.

I know how to autogenerate a word doc but how to combine two
existing documents into a single doc file.

Any Ideas | code samples ?

S.Kartikeyan
 
E

Eric

I am trying to do the exact same thing. Have you had any luck with
it? I am trying to dynamically create a cover page and attach it to
the beginning of an existing document.

If I figure anything out I will post back to this thread.

Regards,
Eric
 
E

Eric

I finally figured this out. The sample code is below.

/// <summary>
/// Prepends the <code>newDocURI</code> document to the beginning
/// of the <code>originalDocURI</code> and saves the output as the
/// <code>outputFileURI</code>.
/// </summary>
/// <param name="originalDocURI">The original main document</param>
/// <param name="newDocURI">The new document, cover page, etc.
/// that you want to prepend to the beginning of the main
document</param>
/// <param name="outputFileURI">The combination output file</param>
/// <returns></returns>
public string prependDoc(string originalDocURI, string newDocURI,
string outputFileURI)
{
object oFirstDoc = originalDocURI;
object oOutputDoc = outputFileURI;
object oPageBreak = Word.WdBreakType.wdPageBreak;

try
{
Word.Application wordApp = new Word.Application();

Word.Document origDoc = wordApp.Documents.Open(ref oFirstDoc,
ref missing, ref oTrue, ref missing, ref missing, ref
missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing);
origDoc.Activate();
wordApp.Selection.InsertFile(newDocURI, ref missing, ref
oFalse,
ref oFalse, ref oFalse);
wordApp.Selection.InsertBreak(ref oPageBreak);
wordApp.ActiveDocument.SaveAs(ref oOutputDoc, ref missing, ref
missing,
ref missing, ref missing, ref missing, ref missing, ref
missing,
ref missing, ref missing, ref missing);
wordApp.ActiveDocument.Close(ref oFalse, ref missing, ref
missing);
return outputFileURI;
}
catch (Exception ex)
{
throw ex;
}
}

Regards,
Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top