Setting Data Classification to word file using c#
Aditya Sharma
1
Reputation point
Team,
My requirement is to generate a word file programmatically, put some data in it and do save as and close the file followed by applying data classification to the same. In case of excel file it is happening but failing for Word since it throws data classification dialog for manual label selection during save as.
Below is the code:
using WORD=Microsoft.Office.Interop.Word;
//This method will invoke powershell command for setting classification label
public bool setClassification(string filename,string LabelID)
{
try
{
PowerShell.Create().AddCommand("Set-AIPFileLabel").AddParameter("Path", filename).AddParameter("LabelId", LabelID).Invoke();
return true;
}
catch(Exception ex)
{
return false;
}
}
public void CreateDocument()
{
try
{
//Create an instance for word app
WORD.Application winword = new Microsoft.Office.Interop.Word.Application();
winword.Visible = false;
//Create a missing variable for missing value
object missing = System.Reflection.Missing.Value;
WORD.Document document =
winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);
//adding text to document
document.Content.SetRange(0, 0);
document.Content.Text = "This is test document " + Environment.NewLine;
//Add paragraph with Heading 1 style
Microsoft.Office.Interop.Word.Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
object styleHeading1 = "Heading 1";
para1.Range.set_Style(ref styleHeading1);
para1.Range.Text = "Para 1 text";
para1.Range.InsertParagraphAfter();
//Add paragraph with Heading 2 style
Microsoft.Office.Interop.Word.Paragraph para2 = document.Content.Paragraphs.Add(ref missing);
object styleHeading2 = "Heading 2";
para2.Range.set_Style(ref styleHeading2);
para2.Range.Text = "Para 2 text";
para2.Range.InsertParagraphAfter();
//Create a 5X5 table and insert some dummy record
Microsoft.Office.Interop.Word.Table firstTable = document.Tables.Add(para1.Range, 5, 5, ref missing, ref missing);
firstTable.Borders.Enable = 1;
foreach (WORD.Row row in firstTable.Rows)
{
foreach (WORD.Cell cell in row.Cells)
{
//Header row
if (cell.RowIndex == 1)
{
cell.Range.Text = "Column " + cell.ColumnIndex.ToString();
cell.Range.Font.Bold = 1;
//other format properties goes here
cell.Range.Font.Name = "verdana";
cell.Range.Font.Size = 10;
}
//Data row
else
{
cell.Range.Text = (cell.RowIndex - 2 + cell.ColumnIndex).ToString();
}
}
}
//Save the document
object filename = (object)"C:\\Users\\Public\\test123.doc";
document.SaveAs2(ref filename, 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);
document.Close(WORD.WdSaveOptions.wdDoNotSaveChanges);
winword.Quit(false, ref missing, ref missing);
setClassification(filename.ToString(), "<label code should go here>");
MessageBox.Show("Document created successfully !");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Sign in to answer