I am clueless how to make Table-object in the word document to WrapText style. I have two documents. First contains a table containing some data. Second contains text. Now, when I try to merge the two files, The First document I add is - the document containing table followed by the document containing only text.
Problem is -In the output file, Table is on first page and text is on second page. But, I want the text to be wrapped next to table. So, that I can show the table and text on the same page.
If there are some other ways of dealing with this, please do tell me. Following is my code:-
public static void MergeWithColumns(string[] filesToMerge, string outputFilename, bool insertPageBreaks, string documentTemplate)
{
object defaultTemplate = documentTemplate;
object missing = System.Type.Missing;
object pageBreak = Word.WdBreakType.wdPageBreak;
object outputFile = outputFilename;
// Create a new Word application
Word._Application wordApplication = new Word.Application();
try
{
// Create a new file based on our template
Word._Document wordDocument = wordApplication.Documents.Add(
ref defaultTemplate
, ref missing
, ref missing
, ref missing);
// Make a Word selection object.
Word.Selection selection = wordApplication.Selection;
// Loop thru each of the Word documents
foreach (string file in filesToMerge)
{
// Insert the files to our template
selection.InsertFile(
file
, ref missing
, ref missing
, ref missing
, ref missing);
//Do we want page breaks added after each documents?
if (insertPageBreaks)
{
selection.InsertBreak(ref pageBreak);
}
}
foreach (Word.Table tbl in wordDocument.Tables)
{
//tbl.AllowAutoFit = false;
tbl.AllowAutoFit = true;
tbl.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
}
// Save the document to it's output file.
wordDocument.SaveAs(
ref outputFile
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing);
// Clean up!
wordDocument = null;
}
catch (Exception ex)
{
//I didn't include a default error handler so i'm just throwing the error
throw ex;
}
finally
{
// Finally, Close our Word application
wordApplication.Quit(ref missing, ref missing, ref missing);
}
}