vba code to open doc based on template

P

pulse

Hello,
I want to write vba code to open a document. Something like this:
documents.open ("c:\test.doc") attached template ("c:\test.dot")
with a execution of the document new event. Is thispossible?
Thanks
 
J

Jay Freedman

pulse said:
Hello,
I want to write vba code to open a document. Something like this:
documents.open ("c:\test.doc") attached template ("c:\test.dot")
with a execution of the document new event. Is thispossible?
Thanks

Yes, it's only a little more complicated than what you proposed:

Private Sub Document_New()
Dim myDoc As Document
Set myDoc = Documents.Open("c:\test.doc")
myDoc.AttachedTemplate = "c:\test.dot"
End Sub

However, it does seem a bit strange to open an existing document whenever
you create a new blank document. Do you want to have them both open at the
same time? What are you trying to achieve?
 
P

pulse

Private Sub Document_New()
Dim myDoc As Document
Set myDoc = Documents.Open("c:\test.doc")
myDoc.AttachedTemplate = "c:\test.dot"
End Sub

However, it does seem a bit strange to open an existing document whenever
you create a new blank document. Do you want to have them both open at the
same time? What are you trying to achieve?

Thanks for the reaction.
Sorry, I mean the Document_open event...
 
J

Jay Freedman

pulse said:
Thanks for the reaction.
Sorry, I mean the Document_open event...

In that case, your Document_Open() procedure should not contain a
Documents.Open statement -- the document will already be opening --
and instead of a MyDoc object you can just use ActiveDocument:

ActiveDocument.AttachedTemplate = "c:\test.dot"
 
P

pulse

In that case, your Document_Open() procedure should not contain a
Documents.Open statement -- the document will already be opening --
and instead of a MyDoc object you can just use ActiveDocument:

ActiveDocument.AttachedTemplate = "c:\test.dot"

I will explain my problem. In the past I used documents with macrocode and
when the document was processed I deleted all the vba code (in a dropvbacode
subroutine) with the objects but that's only possible if the checkbox: trust
visual basic source is checked. Our information department gives us each
month another image and then the checkbox is unchecked again with lots of
error messages as a result. Now I want to use a template with code to
support a document with no vba code in it. Thats called by a third doc with
code like this:
testmacro.doc with autoopen which is unaltered:

Sub autoopen()
Documents.Open ("d:\testmacroempty.doc")
ActiveDocument.AttachedTemplate = "d:\testmacro.dot"
End Sub

(where testmacroempty.doc is the document that the users my alter, mail,
open again and again, print etc)
but my problem is that the Document_Open() macro does not fire because the
template is attached AFTER the opening of the testmacroemptydocument.

Our workaround is difficult, we cannot alter general and document templates
(no writing rights) and we use worddocuments with lots of hyperlinks to
other documents with a lot of vba-code. You cannot open a document based on
a tempate by a hyperlink in word, is this right???
Mayby there's a workaround?
Thanks a lot...
 

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