1

我正在 VS 2013 中开发一个 Word 2013 文档加载项项目,并希望在文档打开时调用一个方法。

通过 Word 2010,有一个 ThisDocument.Open() 事件,但现在似乎已替换为 在此处输入图像描述

背景:我正在寻找开发 Word 2013 加载项,由于我已经很长时间没有开发 Office 应用程序,因此我试图找到一个“入门”类型的文档。不幸的是,Office 2003 是我能找到的最新版本,似乎这不是很有帮助。

http://msdn.microsoft.com/en-us/library/aa192487(v=office.11​​).aspx

4

1 回答 1

1

在 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 部分类文件中

于 2014-08-07T17:10:29.197 回答