Yes, that works, thanks a lot. I try to access the data island in the word document and it fails to find the cached data item.
statements 1 and 2 always return false and statement 3 "Object reference not set to an instance of an object"
I did open and save the document before trying any of this.
VSTO WORD Code:
[Cached()] public Expenses MyExpenses = new Expenses();
private void ThisDocument_Startup(object sender, System.EventArgs e) { if (this.NeedsFill("MyExpenses")) {
this.EmpId = 513; using (OleDbConnection cn = new OleDbConnection(ConnectionString)) { string QueryName = "TERMTOOL_MAILMERGE_Get_List"; cn.Open();
using (OleDbCommand cm = new OleDbCommand(QueryName, cn)) {
cm.CommandType = CommandType.StoredProcedure; OleDbParameter parameterItemId = new OleDbParameter("@EE__", OleDbType.Integer, 4); parameterItemId.Value = EmpId; cm.Parameters.Add(parameterItemId);
OleDbDataAdapter objAdap = new OleDbDataAdapter(cm);
objAdap.SelectCommand = cm;
objAdap.Fill(this.MyExpenses, "Expenses"); } } } Word.Section sec = this.Sections[1]; Word.Range rng = this.Sections[1].Range; // Word.Range rng = sec.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range; foreach (Word.Field fld in rng.Fields) { if (fld.Type == Microsoft.Office.Interop.Word.WdFieldType.wdFieldMergeField) { foreach(DataColumn col in this.MyExpenses.Tables[0].Columns) { if (fld.Code.Text.IndexOf(col.ToString()) > -1) { Word.Range range = fld.Result; fld.Delete(); range.Text = MyExpenses.Tables[0].Rows[0][col].ToString(); break; } } }
ASPX.PAGE CODE:
string filename = @"C:\TeamProjects\TermTool\TermTool\TermTool\CheckOutForms.doc";
FileStream file = new FileStream(filename,FileMode.Open, FileAccess.Read); byte[] template; try { template = new byte[file.Length]; file.Read(template, 0, (int)file.Length); } finally { file.Close(); }
/*1- */ bool IsCacheEnabled = ServerDocument.IsCacheEnabled(filename);
/* 2- */ bool IsCustomized = ServerDocument.IsCustomized(filename);
ServerDocument sd = new ServerDocument(template, ".DOC");
try { /*3*/ sd.CachedData.HostItems["TermTool.ThisDocument"].CachedData["MyExpenses"].SerializeDataInstance(MyExpenses); sd.Save();
// "template" still has the original bytes. Get the new bytes. // template = sd.Document; } finally { sd.Close(); } Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "application/vnd.ms-word"; Response.OutputStream.Write(template, 0, template.Length); Response.Flush(); Response.Close(); }
One if by Land, Two if by Sea |