viewing/running macros in macro-saved global template crashes Word

M

MikeB77

My 'self-saving' global template works fine when it remains open (as .dot)
but crashes Word when I close it and try to run ANY macro. Feeling very stuck
here- help please!

The Goal: I want to have a single document that 'carries' various macros
between various PCs, so I don't have to 'organize' them into normal.dot on
many machines. I can't get word to run macros contained in a non-active
document (is it possible?) and don't want to mess with normal.dot nor
manually save templates or set references.

My attempt: So I've made "myTemplate.doc" that saves itself as
"myTemplate.dot" in the wdUserTemplatesPath folder (after first removing &
deleting any old instances of itself from that folder). The plan is it then
builds a menu to access its various macros...

Progress: this all works fine when (1) I record or run the 'install' macro
in normal.dot OR (2) when I run the 'install' macro from myTemplate.doc/dot
AND myTemplate.dot remains OPEN. BUT if I close myTemplate.dot (after
install) then Word crashes when I press alt-F8, even tho it shows myTemplate
as a currently loaded global template. I've tried it with Word 2003 and 2007.

I've tried unsuccessfully setting the customization context to normal.dot
prior to saving as .dot. I'm avoiding the template startup folder as I don't
want to restart Word to use the macros and I want them all to be tidy and go
away when not in use. I'd rather not need Word security settings to allow VBE
project object changes.

Any help with this GREATLY appreciated, including better ideas on how to
achieve the original goal

Thanks, MikeB77
 
B

Bear

Mike:

I haven't tried to understand what you wrote. But the correct way to
accomplish your application is to create a global add-in that contains all
your tools. This add-in must be a true template, i.e. one created as a
template, or saved as a template using Word's SaveAs command. You cannot
simply rename the extension.

For the Add-in to load automatically, it must be place in the Startup
folder. You can determine the path for this folder by examining the setting
in Tools > Options > File Locations for the item Startup.

You will not be able to edit your template using the VBE unless it IS open
in Word. You will, however, be able to run any macros it contains.

Bear
 
M

MikeB77

Thanks for your comments Bear.
I'm favouring the wdUserTemplatesPath folder so it only runs for the current
Word session. Would that make a difference? I assume the following saves my
doc as a 'true' template - it works until it crashes Word (07 and 03) when
the .dot is closed. Can you see any bugs?

Public Sub CreateTemplate()
Dim userTemplateName As String
Dim userTemplates As String

'make the file names
userTemplates = Options.DefaultFilePath(wdUserTemplatesPath)
userTemplateName = userTemplates & "\" & Left(ActiveDocument.Name,
Len(ActiveDocument.Name) - 4) & ".dot"

removeKillTemplate userTemplateName ' delete any old instance

' save .doc as .dot
ActiveDocument.SaveAs FileName:=userTemplateName, FileFormat:= _
wdGlobalTemplate, AddToRecentFiles:=False
AddIns.Add FileName:=userTemplateName, Install:=True

End Sub

Function removeKillTemplate(tName As String)
Dim oAddin As AddIn

On Error Resume Next
Set oAddin = AddIns(tName)
If Not oAddin Is Nothing Then
If oAddin.Installed Then oAddin.Installed = False
oAddin.Delete
With ActiveDocument
.UpdateStylesOnOpen = False
' .AttachedTemplate = "Normal"
End With
End If

'delete existing USER template file if it exists
If Len(Dir$(tName)) > 0 Then Kill tName

End Function

Public Sub testMe()
MsgBox "hello, I'm here still"
End Sub


thanks

Mike
 
M

MikeB77

Hi Shauna
I've tried Word 2007 and 2003 (er... could be 02 - its at work now and I'm
not) with the same problem.

the macros I want to carry do mundane things to Word docs - disable/enable
web tool bar, update and break links with .xls files, reformat text from .txt
files...

But even when I try a clean doc (->.dot) with only the code above (in reply
to Bear) I get my crash if I close the .dot (works fine if it's open & not
active: it has occured to me I could live with this, or hide it).

Any thoughts?

Thanks, Mike
 
B

Bear

Mike:

The point of these locations is to make your macros available in different
ways.

To be available, the template holding the macros has to be open. It can be
open in the Word window or open in the background (invisibly).

Document templates (those in User Templates etc.) get opened in the
background when you open a document on which they're based.

Global add-in templates (those in Startup) get opened in the background when
the session begins.

So I think you're fighting to replicate an already existing behavior. Just
put your global tools in a global add-in in the Startup folder. Put your
document-specific tools and designs and content in a document template in
User Templates.

Your global tools add-in template will get opened and closed each session
without any effort on your behalf. You can manually disable or enable your
global add-in using controls in the Templates and Add-Ins dialog box.

There is no need to create and destroy templates on the fly as you're
attempting to do. If you utterly must have some home made control over your
add-in, you can create a macro to enable or disable the add-in in VBA. All
this does is replicate manipulation of the Templates and Add-Ins controls,
but I've done it for various reasons.

I see some suspect areas in your code. First of all, it looks like you're
trying to delete the file that contains the code. Of course, this wouldn't
work, or at very least would cause a serious error.

Also, once you successfully add your document to the AddIns collection, it
becomes temporarily unavailable in the current session, unless you manually
close and open it again.

Once more, I suggest you create an ordinary template, use it to hold your
global tool macros, and simply save it in your Startup directory.

Bear
 
S

Shauna Kelly

Hi Mike

I agree with Bear.

If you want tools available to all documents, put the tools in a .dot file
and put that .dot file in the Word Startup folder.

If you want tools available to a specific set of documents, put the tools in
a .dot file, put that .dot file in the User Templates or Workgroup Templates
folder and tell users to do File > New and choose the appropriate template.

Sometimes you have to work with Word the way it was designed to work. Word
wasn't designed to work the way you are proposing.

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
M

MikeB77

Thanks for your comments Bear. they helped me to figure it's to do with
closing an attached yet open template. I want to create and destory templates
to be tidy on other people's computers. I got around it by:

Public Sub MakeTemplate2(Optional startOptions As Integer)
Dim userTName As String
Dim userTemplates As String
Dim NewTemplate As String

'make the file names
userTemplates = Options.DefaultFilePath(wdUserTemplatesPath)
userTName = userTemplates & "\" & Left(ActiveDocument.Name,
Len(ActiveDocument.Name) - 4) & ".dot"

If MsgBox("Install macro template?", vbOKCancel + vbInformation,
"Confirm:") = vbCancel Then Exit Sub

deleteOldTemplate userTName 'my Function to delete any old instance

ActiveDocument.Save
Application.Documents.Add ActiveDocument.FullName ' copy the active
document

ActiveDocument.SaveAs userTName, FileFormat:=wdFormatTemplate,
AddToRecentFiles:=False
ActiveDocument.Close
AddIns.Add FileName:=userTName, Install:=True

aa = ActiveDocument.Name ' close this doc, but not word
If Application.Documents.Count = 1 Then Application.Documents.Add
Application.Documents(aa).Close
End Sub


I note on Word 2002 I can then see the available macros via Alt-F8. but on
Word 2007 I can't see them but can still address them via a custom toolbar.
so inelegant, but problem solved.
thanks for your help
 

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