can't close Word template

S

Simon

Hi,
I use vba code to open a new doc from a Word template (dot) file , and set
some values to bookmarks, and save it to a new file. I tried to close the
template dot file at the end, but it does not work. See the code below. The
thing is, as I found out by debugging the document loop, the template file
(Print_temp.Dot) is not in the Documents collection. The file handle
~$int_temp.dot remains there until I manually close the newly created
Print_New.doc file.

Advice please. Thanks.
Simon
---------------
Dim objWord As Word.Application
Dim NewDoc As Word.Document
Dim aDoc As Document

Set objWord = New Word.Application

Set NewDoc = objWord.Documents.Add("C:\Print_Templates\" & "Print_temp.Dot")

(do bookmark inserts to NewDoc)

NewDoc.SaveAs "C:\Print_New.doc"

objWord.Visible = True

'close template, here is the problem - the dot file is not in the Documents
collection
For Each aDoc In objWord.Documents
If aDoc.FullName = "C:\Print_Templates\" & "Print_temp.Dot") Then
aDoc.Close
Exit For
End If
Next
---------------------------
 
D

Dirk Goldgar

Simon said:
Hi,
I use vba code to open a new doc from a Word template (dot) file ,
and set some values to bookmarks, and save it to a new file. I tried
to close the template dot file at the end, but it does not work. See
the code below. The thing is, as I found out by debugging the
document loop, the template file (Print_temp.Dot) is not in the
Documents collection. The file handle ~$int_temp.dot remains there
until I manually close the newly created Print_New.doc file.

Advice please. Thanks.
Simon
---------------
Dim objWord As Word.Application
Dim NewDoc As Word.Document
Dim aDoc As Document

Set objWord = New Word.Application

Set NewDoc = objWord.Documents.Add("C:\Print_Templates\" &
"Print_temp.Dot")

(do bookmark inserts to NewDoc)

NewDoc.SaveAs "C:\Print_New.doc"

objWord.Visible = True

'close template, here is the problem - the dot file is not in the
Documents collection
For Each aDoc In objWord.Documents
If aDoc.FullName = "C:\Print_Templates\" & "Print_temp.Dot")
Then aDoc.Close
Exit For
End If
Next
---------------------------

I'm not an expert on Word automation, but what makes you think you
should close the template? When you use the Documents.Add method, you
aren't so much opening the template as creating a new document based on
that template. The template itself would not be added to the Documents
collection, and AFAIK you don't have to close it. Have I missed
something?
 

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