Wednesday, July 14, 2010

Merge word document and upload in a List with attachment

private static void Merge(string filesToMerge, string outputFilename, bool insertPageBreaks, string defaultWordDocumentTemplate)
{
{
object defaultTemplate = @"Normal.dot";
object missing = System.Type.Missing;
object pageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
object outputFile = outputFilename;

// Create a new Word application
Microsoft.Office.Interop.Word._Application wordApplication = new Microsoft.Office.Interop.Word.Application();

try
{
// Create a new file based on our template
Microsoft.Office.Interop.Word._Document wordDocument = wordApplication.Documents.Add(
ref defaultTemplate
, ref missing
, ref missing
, ref missing);

// Make a Word selection object.
Microsoft.Office.Interop.Word.Selection selection = wordApplication.Selection;

// Loop thru each of the Word documents
string StrMergeDoc = filesToMerge.ToString();
string[] Delimeter = StrMergeDoc.Split(',');
for (int i = 0; i <= Delimeter.Length - 1; i++)
{
string strMDoc = "F:/punit/event handler/EventHnadlerdeployment/EventHnadlerdeployment/" + Delimeter[i].ToString().Trim() + ".doc";
// foreach (string file in filesToMerge)
//{
// Insert the files to our template
selection.InsertFile(strMDoc, ref missing, ref missing, ref missing, ref missing);

//Do we want page breaks added after each documents?
if (insertPageBreaks)
{
selection.InsertBreak(ref pageBreak);
}
}

// 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);
}
} //throw new Exception("The method or operation is not implemented.");
}


private static void uploadfile(SPListItem item)
{
SPAttachmentCollection spAttachmentCollection = item.Attachments;
FileStream fStream = File.OpenRead("C:\\Punit.doc"); //path of the file to upload
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
fStream.Dispose();
spAttachmentCollection.Add("Competency.doc", contents);
item.Update();
}
public void MergeDocument(SPListItem item)
{
string defaultWordDocumentTemplate = @"Normal.dot";
string outputFilename = @"C:\Punit.doc";
string filesToMerge = string.Empty;
SPSite site = new SPSite("http://dhts1:22222");
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
SPListItemCollection itemcoll = web.Lists["Creation of Job Description"].Items;
if (itemcoll != null && itemcoll.Count > 0)
{
{
string IndividualContributor = Convert.ToString(item["Individual_x0020_Contributor"]).Replace('#', ' ');
string newIP = IndividualContributor.Remove(0, 1);
string PPL = Convert.ToString(item["PPL"]).Replace('#', ' ');
string newPPL = PPL.Remove(0, 1);
string BFL = Convert.ToString(item["BFL"]).Replace('#', ' ');
string newBFL = BFL.Remove(0, 1);
string STL = Convert.ToString(item["STL"]).Replace('#', ' ');
string newSTL = STL.Remove(0, 1);
filesToMerge = newIP.Trim() + newPPL.Trim() + newBFL.Trim() + newSTL.Trim();
filesToMerge = filesToMerge.Replace(';', ',');
}
}
Merge(filesToMerge, outputFilename, true, defaultWordDocumentTemplate);

}

No comments:

Post a Comment