Asked by:

VB.Net Using a Word Templte to Create a Document

Question

I'm trying to write a VB.Net app which will generate a Word Document using a set Word Template (e.g. ApprovalTemplate.dot) stored in a shared folder. The document get's created fine, the application runs without any errors. When I open the document the programme
created it appears fine just as I want, However when I go to close the document I receive a prompt asking if I want to save the changes to my template file (ApprovalTemplate.dot)

I'd love some feedback, this is the first time I've tried to Create a Word file through VB.Net.

To create a new Word document by using Automation from Visual Basic .NET, follow these steps:

Start Microsoft Visual Studio .NET. On the File menu, click
New and then click Project. Under Project types click
Visual Basic Projects, then click Windows Application under
Templates. Form1 is created by default.

Add a reference to the Microsoft Word Object Library. To do this, follow these steps:

To create a new Word document by using Automation from Visual Basic .NET, follow these steps:

Start Microsoft Visual Studio .NET. On the File menu, click
New and then click Project. Under Project types click
Visual Basic Projects, then click Windows Application under
Templates. Form1 is created by default.

Add a reference to the Microsoft Word Object Library. To do this, follow these steps:

On the Project menu, click
Add Reference.

On the COM tab, locate the
Microsoft Word Object Library and click Select.

Note Microsoft Office 2003 and later versions of Office include Primary Interop Assemblies (PIAs). Microsoft Office XP does not include PIAs, but they may be downloaded. For more information about Office XP PIAs, click the following article
number to view the article in the Microsoft Knowledge Base:

After the code completes, examine the document that is created for you. The document contains two pages of formatted paragraphs, tables, and a chart.

Use a template

If you are using Automation to build documents that are all in a common format, you can benefit from starting the process with a new document that is based on a preformatted template. Using a template with your Word Automation client has two significant advantages
over building a document from nothing:

You can have greater control over the formatting and placement of objects throughout your documents.

You can build your documents with less code.

By using a template, you can fine-tune the placement of tables, paragraphs, and other objects within the document, as well as include formatting on those objects. By using Automation, you can create a new document based on your template with code such as the
following:

oWord.Documents.Add "<Path to your template>\MyTemplate.dot"

In your template, you can define bookmarks so that your Automation client can fill in variable text at a specific location in the document, as follows:

oDoc.Bookmarks.Item("MyBookmark").Range.Text = "Some Text Here"

Another advantage to using a template is that you can create and store formatting styles that you wish to apply at run time, as follows:

Microsoft provides a special forum to deal with Word issue. For better support, I will move this thread to Word for Developers forum. It will cost a little time to involve the members in this forum. I appreciate your
patience.

<<However when I go to close the document I receive a prompt asking if I want to save the changes to my template file (ApprovalTemplate.dot)>>

Depending on what the template contains and what your code does, it can happen that something is written back to the template to "dirty" it - meaning Word thinks it needs to be saved. Usually, that is not the case, so it's enough to tell Word to ignore the
"dirtiness". You can do that using the SAVED property. Something like this:
objDoc.AttachedTemplate.Saved = True

<<However when I go to close the document I receive a prompt asking if I want to save the changes to my template file (ApprovalTemplate.dot)>>

Depending on what the template contains and what your code does, it can happen that something is written back to the template to "dirty" it - meaning Word thinks it needs to be saved. Usually, that is not the case, so it's enough to tell Word to ignore the
"dirtiness". You can do that using the SAVED property. Something like this:
objDoc.AttachedTemplate.Saved = True

The line of code you show in your recent response should generate a new document
(*.doc) file from the template (*.dot).

This new document should have an active link back to the template - a document cannot exist without a template.

So far, so good. What we cannot see, however, is the string you pass to the SaveAs method to save the new document. By default, I assume the file type is "Word Document *.doc" and that SaveAs is not saving to a template file.

If that is the case, then I'd expect objDoc.AttachedTemplate to point to ApprovalTemplate.dot. HOWEVER...

Just the other day I ran into a similar problem when trying to address files in a folder that my Windows didn't think I had access permissions to. It was System.IO.Packaging, but the error was basically the same - as far as my code was concerned, the file
wasn't "there".

As far as I can see, you haven't told us the version of Word or of Windows that's involved, here? I get the impression this is a folder on the Network?