Programmatically open new document based on *.dot template

G

G

I have a procedure that opens a word document (*.doc) and populates the
document with data from an *.mdbat particular bookmarks. Is there a method
to programmatically open a new document based on a *.dot? That way I do not
have to worry about modifications being made to the bookmarked file. T
Any ideas. Thanks.

Here is a snippet of code I am using now:
Dim objWord As Object
Dim objDoc As Object
Dim strFullPathAndFileName As String

'build the path to the rma form
strRMA = "RMA Request Form rev 2004-08-27"
strFullPathAndFileName = "l:\database\customer support\" & strRMA &
".doc"

'Opens word
Set objWord = CreateObject("Word.Application")
objWord.Visible = True

'opens the document
Set objDoc = objWord.Documents.Open(strFullPathAndFileName)

'write to the document at the bookmarks
With objWord.ActiveDocument.Bookmarks
.Item("MN").Range.InsertAfter (((UCase(Forms("FrmRMARequest")("MN")))))
.Item("SN").Range.InsertAfter (((Forms("FrmRMARequest")("SN"))))
.Item("PN").Range.InsertAfter (((Forms("FrmRMARequest")("PN"))))
.Item("RmaDate").Range.InsertAfter
(((Forms("FrmRMARequest")("RequestDate"))))
 
M

Malcolm Smith

May I suggest to create a new document based on a template is:

Set oNewDoc = Documents.Add sFilename, sTemplate

- Malc
 
R

Richard P

if the document is not new but you are opening an existing document try this

objDoc.ActiveDocument.AttachedTemplate = path and name of template ie.
"c:\template.dot"
 
G

G

Malc,
Thanks for the response. I tried the code suggested, but am getting and
"Object Required" Error. I dim'd and ObjDoc as an Object. What am I missing?

Code:
Public Sub tester()
Dim objWord As Object
Dim objDoc As Object

Dim strFullPathAndFileName As String
Dim strTemplatePathAndFileName As String

'build the path to the rma form
strFullPathAndFileName = "l:\database\customer support\RMA Request Form
rev 2004-08-27.doc"

'template path
strTemplatePathAndFileName = "l:\database\customer support\RMA Request
Form rev 2004-08-27.dot"

'Opens word
Set objWord = CreateObject("Word.Application")
objWord.Visible = True

'opens the document
'Set objDoc = objWord.Documents.Open(strFullPathAndFileName)
Set objDoc = Documents.Add(strFullPathAndFileName,
strTemplatePathAndFileName)

End Sub
 
C

Chad DeMeyer

G

Malc almost remembered it right. However, the Add method of the Documents
collection does not accept a filename argument, just a template name. To
name the file that's created you have to call the SaveAs method of the
document object you create. So it should look like this:

Set objDoc = objWord.Documents.Add(strTemplatePathAndFileName)
objDoc.SaveAs strFullPathAndFileName

Regards,
Chad
 

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