PDA

View Full Version : Macros lost when new document created from word template



jai2
09-18-2007, 12:39 PM
Hello,

I'm totally new at creating macros and using VBA. I was forced to create macro because my hyperlinks were not working in a dcoument that was proteced for forms.

I used the following guide lines by MS: support.microsoft.com/kb/913761

I created the macro in the current working document. Everything worked as per the MS-KB, but when I rename the document to .DOT to use it as a template, the new document loses the macro. Link fails to work.

What I need to do is create a macro in such way so that if I create new document from the template, it retains the macro.

Please and thanks,

Jai

TonyJollans
09-18-2007, 02:40 PM
If you base a document on a template, the macros remain in the template. When you open the document the template is automatically opened as well and the macros are thus availabale. Now, if you move the template, or open a copy of the document on another machine that does not have the template, or if the template cannot be found for any other reason, then, of course, it cannot be opened.

You would need to give some more details before anyone could identify what might be causing the problem in your situation.

fumei
09-19-2007, 08:06 AM
Yes, we need more details.

Ooooooooo, an opportunity to disagree with Tony!
When you open the document the template is automatically opened as wellTechnically, when a document is created from a template, when that document is opened, the template is NOT opened. That is, if "opened" means the template file is actually opened.

Wait a sec.......

Darn. I suppose Tony is correct, in that the template file is read which does mean "opened" technically. It is not opened as a document, but it is read. Try clicking the template which does show in the VBE Project Explorer. "Project is unviewable." It can only be viewed if the template file is actually "open".

Hmmmm. Is it "open", or not? I would say not, yet it IS read...so it has to be "opened" in some way.

Darn. I should mind my own business. OK....never mind.

TonyJollans
09-19-2007, 09:16 AM
Ooooooooo, an opportunity to disagree with Tony!
LOL!

I guess we're both wrong, really!

Templates attached to documents are opened in the same way that Normal.dot is opened - the Project is available to edit but the document isn't. Is that open? I don't know.

There are (at least) three ways to be open:

Open as a Document
Open as a Template
Open as a Global TemplateCorrect terminology? Haven't got a clue :)

fumei
09-19-2007, 10:27 AM
Darn, darn, darn.

I was thinking of globals. Silly me. Yes, of course you are correct. Template projects ARE "open" for edits from documents created from them.

Darn, darn, darn.

lucas
09-19-2007, 01:54 PM
What if we open a regular .doc file and then make a copy of it and close the original leaving the new copy open...It could even be put in the document open statement so it's run when you open the original.

It could serve as a template although it's not a .dot file extention and all the macro's, forms, etc. would be copied to the new file:
Option Explicit
Sub SaveCopyAs()
Const lCancelled_c As Long = 0
Dim sSaveAsPath As String
Dim otemplate As Document
sSaveAsPath = GetSaveAsPath
If VBA.LenB(sSaveAsPath) = lCancelled_c Then Exit Sub
Set otemplate = ActiveDocument
'the next line copies the active document
Application.Documents.Add ActiveDocument.FullName
'the next line saves the copy to your location and name
ActiveDocument.SaveAs sSaveAsPath
'next line closes the original leaving you with the copy open
otemplate.Close
End Sub
Public Function GetSaveAsPath() As String
Dim fd As Office.FileDialog
Set fd = Word.Application.FileDialog(msoFileDialogSaveAs)
fd.InitialFileName = ActiveDocument.Name
If fd.Show Then GetSaveAsPath = fd.SelectedItems(1)
End Function

TonyJollans
09-19-2007, 04:08 PM
That is just the code equivalent of "new from existing" and macros can get disconnected from formfields - so, yes, they are there, but they don't work. A simple SaveAs works better.

I tried it in 2007 and got in rather a tangle with macro security as well :(

Personally I hate the option. Word is designed to work with Templates and works best that way. But that may just be me!

fumei
09-20-2007, 08:37 PM
Personally I hate the option. Word is designed to work with Templates and works best that way. But that may just be me!You are not alone. Word IS designed to work with templates. They are the foundation.