Create a new Word document from a template

N

Newbie

Hello,

I created a new document from a template which is on a shared unit on the
network :
Documents.Add Template:="\\Network\My documents\Tracking\Template.dot".
Now, how can I refer to it via VBA, so that I can give it a name then fill
it with some data and save it ?
Thanks for some sample code

Newbie
 
S

Shauna Kelly

Hi Newbie

If you have a look in the VBA Help files you'll see that the .Add method
returns a reference to the newly-created document.

So you can do something like this:

Option Explicit

Sub CreateDocument()

Dim doc As Word.Document

Set doc = Documents.Add

doc.SaveAs FileName:="\\Network\My documents\Tracking\Template.dot"

MsgBox doc.Name

doc.Range.InsertAfter "Hello world"

doc.Close SaveChanges:=True

End Sub


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
N

Newbie

Thanks a lot Shauna!
I'll work on that!

Newbie

Shauna Kelly said:
Hi Newbie

If you have a look in the VBA Help files you'll see that the .Add method
returns a reference to the newly-created document.

So you can do something like this:

Option Explicit

Sub CreateDocument()

Dim doc As Word.Document

Set doc = Documents.Add

doc.SaveAs FileName:="\\Network\My documents\Tracking\Template.dot"

MsgBox doc.Name

doc.Range.InsertAfter "Hello world"

doc.Close SaveChanges:=True

End Sub


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top