Search code examples
office-interopword-2013

What method would be the current equivalent of Open now that in Office 2013 Interop.Word, there is no ThisDocument.Open() method?


I am developing a Word 2013 Document add-in project in VS 2013 and want a method to be called when the document opens.

Through Word 2010, there was a ThisDocument.Open() event, but it seems this has now been replaced with enter image description here

Background: I am looking to develop a Word 2013 add-in and as it has been a long time since I have developed an Office application, I was attempting to find a "Getting Started" type document. Unfortunately, Office 2003 is the most recent one I could find and it seems as though this is not very helpful.

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


Solution

  • In the ThisDocument.Designer.cs file, add your function calls to the InitializeData() method like so:

    [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();
    }
    

    The example walk through in the link should be like this for the new Interop interface:

    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();
            }
    
        }
    
    
    }
    

    This code can be added to the ThisDocument partial class file