none
how to open word with a template RRS feed

  • Question

  • Hi,

    Did any body help me how to open word with a template. if I have a word 2010 template(.dotx), how could i open an existing word with this template, rather than normal template.(c#/vb)

    I have notice that C:\Program Files (x86)\Microsoft Office\Templates\1033 have lots of .dotx file, should i replace them ? if yes, which one should i replace?

    Thx.

    Monday, December 10, 2012 8:42 AM

Answers

  • Hi "after_base"

    What kind of project is it you're working on? We need to understand why you would say "open word with a template"? Especially in the context of this, the VSTO forum, where the VSTO technology runs in-process with the Office application (so Word is already "open")...


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, December 10, 2012 2:38 PM
    Moderator
  • THe Documents.Add method creates a new document (docx) based on the template name supplied as a parameter.

    You've asked this question in the VSTO forum, but apparently you aren't using the VSTO technology, which is why Birgit's suggestion threw an error. Try Tom's suggestion.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, December 11, 2012 7:06 AM
    Moderator
  • Hi after,

    Thanks for posting in the MSDN Forum.

    Let's see whether following code can help you:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Word = Microsoft.Office.Interop.Word;
    namespace WindowsFormsApplication5
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                Word.Application wdApp = null;
                try
                {
                    wdApp = new Word.Application();
                    object template = @"C:\***\template.dotx";
                    wdApp.Documents.Add(Template: ref template);
                    wdApp.Visible = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (wdApp != null)
                    {
                        if (MessageBox.Show("Close Word?", "Close Word?", 
                            MessageBoxButtons.YesNo, MessageBoxIcon.Question) == 
                            System.Windows.Forms.DialogResult.Yes)
                            ((Word._Application)wdApp).Quit();
                    }
                }
            }
        }
    }

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 11, 2012 5:31 AM
    Moderator

All replies

  • Hi,

    you could try the following code (VB):

    globals.ThisAddIn.Application.Documents.Add("YourPathAndTemplateName.dotx")

    actually with globals.thisaddin.application you can use the complete document model of Word.

    I would not know why you would need to replace those templates in program files.

    Hope this helps.

    Cheers,

    Birgit

    Monday, December 10, 2012 1:11 PM
  • Hi "after_base"

    What kind of project is it you're working on? We need to understand why you would say "open word with a template"? Especially in the context of this, the VSTO forum, where the VSTO technology runs in-process with the Office application (so Word is already "open")...


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, December 10, 2012 2:38 PM
    Moderator
  • Thanks for you reply!

    I create a winForm projetc to open an existing word document and word template, I'd like to open the existing word doc with word template.  

    There is no need to replace  templates. In fact, I'd to open a normal word document(.docx) with custom template(.dotx).

    maybe code  is like this:

    Dim wordApp as new Microsoft.office.Interop.Word.Application()

    globals.ThisAddIn.Application.Documents.Add("MyTemplatePath.dotx")

    Dim wordDoc as Microsoft.Office.Interop.Word.Document = wordApp.Documents.Open("myFilePath.docx")

    but code: globals.ThisAddIn.Application.Documents.Add("YourPathAndTemplateName.dotx") could not be found, Athough I have import namespace "Microsoft.Office.Core" and "Microsoft.Office.Interop.Word"


    • Edited by after_base Tuesday, December 11, 2012 3:23 AM add details
    Tuesday, December 11, 2012 1:09 AM
  • Hi after,

    Thanks for posting in the MSDN Forum.

    Let's see whether following code can help you:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Word = Microsoft.Office.Interop.Word;
    namespace WindowsFormsApplication5
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                Word.Application wdApp = null;
                try
                {
                    wdApp = new Word.Application();
                    object template = @"C:\***\template.dotx";
                    wdApp.Documents.Add(Template: ref template);
                    wdApp.Visible = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (wdApp != null)
                    {
                        if (MessageBox.Show("Close Word?", "Close Word?", 
                            MessageBoxButtons.YesNo, MessageBoxIcon.Question) == 
                            System.Windows.Forms.DialogResult.Yes)
                            ((Word._Application)wdApp).Quit();
                    }
                }
            }
        }
    }

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 11, 2012 5:31 AM
    Moderator
  • Hi,

    you could try the following code (VB):

    globals.ThisAddIn.Application.Documents.Add("YourPathAndTemplateName.dotx")

    actually with globals.thisaddin.application you can use the complete document model of Word.

    I would not know why you would need to replace those templates in program files.

    Hope this helps.

    Cheers,

    Birgit


    Thanks for you reply!

    I create a winForm projetc to open an existing word document and word template, I'd like to open the existing word doc with word template.  

    There is no need to replace  templates. In fact, I'd to open a normal word document(.docx) with custom template(.dotx).

    maybe code  is like this:

    Dim wordApp as new Microsoft.office.Interop.Word.Application()

    globals.ThisAddIn.Application.Documents.Add("MyTemplatePath.dotx")

    Dim wordDoc as Microsoft.Office.Interop.Word.Document = wordApp.Documents.Open("myFilePath.docx")

    but code: globals.ThisAddIn.Application.Documents.Add("YourPathAndTemplateName.dotx") could not be found, Athough I have import namespace "Microsoft.Office.Core" and "Microsoft.Office.Interop.Word"

    Tuesday, December 11, 2012 5:33 AM
  • Hi after,

    Thanks for posting in the MSDN Forum.

    Let's see whether following code can help you:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Word = Microsoft.Office.Interop.Word;
    namespace WindowsFormsApplication5
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                Word.Application wdApp = null;
                try
                {
                    wdApp = new Word.Application();
                    object template = @"C:\***\template.dotx";
                    wdApp.Documents.Add(Template: ref template);
                    wdApp.Visible = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (wdApp != null)
                    {
                        if (MessageBox.Show("Close Word?", "Close Word?", 
                            MessageBoxButtons.YesNo, MessageBoxIcon.Question) == 
                            System.Windows.Forms.DialogResult.Yes)
                            ((Word._Application)wdApp).Quit();
                    }
                }
            }
        }
    }

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Hi Tom,

    I'd not like to open the template file(.dotx), but to open an document file (.docx) with this template file (.dotx).

    In template file i defined some coustom control, i'd like to the document file(.docx) work as the template file(.dotx).

    Thanks

    Tuesday, December 11, 2012 6:37 AM
  • THe Documents.Add method creates a new document (docx) based on the template name supplied as a parameter.

    You've asked this question in the VSTO forum, but apparently you aren't using the VSTO technology, which is why Birgit's suggestion threw an error. Try Tom's suggestion.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, December 11, 2012 7:06 AM
    Moderator