在 ThisDocument.Designer.cs 文件中,将函数调用添加到 InitializeData() 方法,如下所示:
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Tools.Office.ProgrammingModel.dll", "12.0.0.0")] [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
private void InitializeData() {
this.CreateHeader();
this.RetrieveSuppliers();
}
对于新的互操作界面,链接中的示例演练应该是这样的:
private void CreateHeader()
{
Word.Range rng;
Object editorID = new Object();
Object start = 0;
Object end = 0;
rng = this.Range(ref start, ref end);
editorID = Word.WdEditorType.wdEditorCurrent;
//Setup tab locations
Single[] tabStops = new Single[] { 4, 6 };
// this.Range(object start, object end).Delete(ref unit, ref count);
this.SelectAllEditableRanges(ref editorID);
this.Sections[1].PageSetup.
Orientation = Word.WdOrientation.wdOrientLandscape;
rng.InsertBefore("Supplier Phone List");
rng.Font.Name = "Verdana";
rng.Font.Size = 16;
rng.InsertParagraphAfter();
rng.InsertParagraphAfter();
// Create a new range at the insertion point.
rng.SetRange(rng.End, rng.End);
Word.ParagraphFormat fmt = rng.ParagraphFormat;
// Set up the tabs for the column headers.
Object alignment = Word.WdTabAlignment.wdAlignTabLeft;
Object leader = Word.WdTabLeader.wdTabLeaderSpaces;
fmt.TabStops.ClearAll();
fmt.TabStops.Add(ThisApplication.InchesToPoints(tabStops[0]),
ref alignment, ref leader);
alignment = Word.WdTabAlignment.wdAlignTabLeft;
leader = Word.WdTabLeader.wdTabLeaderSpaces;
fmt.TabStops.Add(ThisApplication.InchesToPoints(tabStops[1]),
ref alignment, ref leader);
// Insert the column header text and formatting.
rng.Text = "Company Name\tContact\tPhone Number";
rng.Font.Name = "Verdana";
rng.Font.Size = 10;
rng.Font.Bold = System.Convert.ToInt32(true);
rng.Font.Underline = Word.WdUnderline.wdUnderlineSingle;
// Create a new range at the insertion point.
rng.InsertParagraphAfter();
rng.SetRange(rng.End, rng.End);
fmt = rng.ParagraphFormat;
// Set up the tabs for the columns.
fmt.TabStops.ClearAll();
alignment = Word.WdTabAlignment.wdAlignTabLeft;
leader = Word.WdTabLeader.wdTabLeaderDots;
fmt.TabStops.Add(ThisApplication.InchesToPoints(tabStops[0]),
ref alignment, ref leader);
fmt.TabStops.Add(ThisApplication.InchesToPoints(tabStops[1]),
ref alignment, ref leader);
// Insert a bookmark to use for the inserted data.
Object range = rng;
this.Bookmarks.Add("Data", ref range);
rng.InsertParagraphAfter();
}
private void RetrieveSuppliers()
{
SqlConnection cnn;
SqlCommand cmd;
SqlDataReader dr = null;
Word.Range rng;
System.IO.StringWriter sw = new System.IO.StringWriter();
// Set up the command text:
string strSQL =
"SELECT CompanyName, ContactName, Phone " +
"FROM Suppliers ORDER BY CompanyName";
try
{
// Create the connection:
cnn = new SqlConnection(
"Data Source=(local);Database=Northwind;" +
"Integrated Security=true");
cnn.Open();
// Create the command and retrieve the data reader:
cmd = new SqlCommand(strSQL, cnn);
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
// Loop through the data, creating tab-delimited output:
while (dr.Read())
{
sw.WriteLine("{0}\t{1}\t{2}",
dr[0], dr[1], dr[2]);
}
// Work with the previously created bookmark:
Object item = "Data";
Word.Bookmark bmk =
(Word.Bookmark)this.Bookmarks.get_Item(ref item);
rng = bmk.Range;
rng.Text = sw.ToString();
rng.Font.Name = "Verdana";
rng.Font.Size = 10;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, this.Name);
}
finally
{
if (dr != null)
{
dr.Close();
}
}
}
可以将此代码添加到 ThisDocument 部分类文件中