1

I have a template.dot Word Document and if I use Windows Explorer, when I right click on it, I can see the functions "New, Open, Print, etc", with New being the default option.

If I use Process.Start("template.dot"), this function creates a new document ("document.doc"), because the default choice is "New". How can I open the "template.dot" file for editing in MS Word (like when I select the Open function from Right Click)?

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Nick_F
  • 1,103
  • 2
  • 13
  • 30
  • Set ProcessStartInfo.Arguments to "template.dot" while run Process.Start("winword.exe") and see if that works. – Prateek Shrivastava May 25 '18 at 01:25
  • 1
    See: [`ProcessStartInfo.Verb`](https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.verb%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396) – Jimi May 25 '18 at 01:54
  • Prateek, the command Process.Start("winword.exe") creates a new word document, based on the default action, "New". – Nick_F May 25 '18 at 04:53
  • Thanks Jimi, I will check later today and will answe if it's working – Nick_F May 25 '18 at 04:53
  • @Nick_F please have a look at the updated post. It's about properly creating and cleaning up objects. – wp78de May 25 '18 at 06:08

3 Answers3

3

You could use interop to start from a .dot/x template like that:

using System;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Word;

namespace EditWordDotSO
{
    class Program
    {
        static void Main(string[] args)
        {
            var applicationWord = new Microsoft.Office.Interop.Word.Application();
            applicationWord.Visible = true;

            Document doc = null;

            try
            {
                doc = applicationWord.Documents.Add(@"path\to\your\a.dotx");
                doc.Activate();
            }
            catch (COMException ex)
            {
                Console.Write(ex);
                //dispose properly as shown below
            }
        }
    }
}

Note: You need to add a COM reference to Microsoft.Office.Interop.Word tied to your installed MS Word.

Update: As mentioned by @CindyMeister use

this.Application.Documents.Add(@"C:\Test\SampleTemplate.dotx");

instead of creating a new Document(). Ref: How to: Programmatically Create New Documents

PS: The finally { ... } block is intended to close the document and dispose the COM objects properly. Why use finally?

Here is a more involved method to do so inspired by this post:

finally {
    doc.Close(null, null, null);
    applicationWord.Quit();
    if (doc != null)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
    if (applicationWord != null)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(applicationWord);
    doc = null;
    applicationWord = null;
    GC.Collect(); // final cleanup    
}

PPS: It's also possible to add or change the template like that:

doc = applicationWord.Documents.Add();
doc.set_AttachedTemplate(@"C:\Test\SampleTemplate.dotx");
doc.UpdateStyles();
wp78de
  • 18,207
  • 7
  • 43
  • 71
  • Thanks wp78de, it worked! I removed the "finally" part, because I don't know how to use it and I replaced Document dot = new Document() with Document dot = null, because I wanted to open an existent template. – Nick_F May 25 '18 at 05:08
  • NEVER EVER use `New Document`in the Word interop. It creates an object in memory that can't be accessed properly and is often orphaned, so that objects remain in memory. – Cindy Meister May 25 '18 at 05:22
  • @CindyMeister thank you, you are right. I was in a rush: `this.Application.Documents.Add(@"C:\Test\SampleTemplate.dotx");` https://msdn.microsoft.com/en-us/library/hhf98b5c.aspx – wp78de May 25 '18 at 05:42
1
Process.Start(new ProcessStartInfo(@"C:\1.dot") { Verb = "open" });

Also, full corresponding command can be found in multiple places in the registry:

Computer\HKEY_CLASSES_ROOT\Applications\WINWORD.EXE\shell\edit\command

Computer\HKEY_CLASSES_ROOT\Word.Template.8\shell\Open\command

The following works for me, but might need the full path in some cases:

Process.Start("winword.exe", @"/n C:\1.dot");
Slai
  • 22,144
  • 5
  • 45
  • 53
  • Excellent code! This is what I was hoping to obtain. I already implemented the code kindly provided by wp78de, but this code is simpler and it does not need InteropServices. Also, thanks for providing the link to registry commands. – Nick_F May 26 '18 at 09:40
0

Right click the file and choose Open. In Office 2016, a template will open in Edit mode instead of new file mode.

  • My question was not related to how to manually open the template, but from inside my own program. Thanks for your answer though. – Nick_F Oct 12 '19 at 01:10