opening a word template to make changes and then save as a word document

K

KK

Hello,

I am creating a vb application to use a word template, make a quick text
change and then save as a word document.
However, the following code worked on the initial run without any error. I
used this code in a simple vb form button click.

Templates("c:\test.dot").OpenAsDocument
Selection.Range.Text = "Made a change"
ActiveDocument.SaveAs "c:\test.doc"
ActiveDocument.Close saveChanges:=wdDoNoSaveChanges


But then afterwards it gives a warning of a run-time error '5941': The
requested member of the collection does not exist. This is referring to the
line

Templates("c:\test.dot").OpenAsDocument

however, this word template already exists in this location.

It only works now if you open the word template and leave it open in word
whilst you run the vb application.


Can anyone help explain why that error message comes up and how i can
prevent that happening. I am trying to get the vb application to utilise
the template without any error and create a word document in a designated
file location. This is without the need to keep that template in word open
explicitly.


Thanks.

Wing
 
D

Doug Robbins - Word MVP

Assuming that c:\test.dot does exist, you should use

Dim newdoc As Document
Set newdoc = Documents.Add("c:\test.dot")


newdoc.SaveAs "c:\test.doc"
newdoc.Close
Set newdoc = Nothing


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
P

Peter Hewett

Hi Wing

Just to add a little to what Doug has said. The templates collection object
contains ONLY those templates currently loaded. This includes templates loaded
as Add-Ins (including Normal.dot), templates you've opened directly (File>Open)
and templates loaded because they are "attached" to a document you've opened.

But unless a template is also present in the Documents collection you can't edit
it as a document. That's what the "OpenAsDocument" method is for. It takes a
loaded template and opens it for editing, if not already open.

HTH + Cheers - Peter
 
K

KK

Hello,

I have tried what Doug Robbins has suggested but i now get Run-time error
'429'
ActiveX component can't create object.

Set newdoc = Documents.Add("c:\test.dot")

newdoc.SaveAs "c:\test.doc"
newdoc.Close
Set newdoc = Nothing

The test.dot exists but is not open in word. If i want to make a text
change for that template do i still use activeDocument?

such as
Selection.Range.Text = "Made a change"
ActiveDocument.SaveAs "c:\test.doc"
ActiveDocument.Close saveChanges:=wdDoNoSaveChanges

Thanks,

Wing
 
P

Peter Hewett

Hi KK

Post the entire procedure so we can see what's really going on.

HTH + Cheers - Peter
 

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