?
Solved

How to use AutoOpen or similar in global addin to set-up toolbars etc.

Posted on 2008-11-12
25
Medium Priority
?
380 Views
Last Modified: 2012-05-05
Hi

I have 4 templates that my team uses depending on the type of document they are creating. I have several toolbars - I want the toolbars to be made visible if any 1 of the 4 templates is opened or if a document based on the templates is opened.

I have a template in the word startup folder that contains all my macros.

I tried using:
Sub FileOpen() and
Sub FileNew()

both with a msgbox to let me know the macro was firing. Unfortunately when I clicked on the file open button the message box displayed but no file was opened.... I have tried a variety of combinations but none seem to work...

I am not sure exactly where to put the FileOpen() / FileNew() / AutoOpen() / Auto_Open() code... I want the macro to be global, stored in the same template that houses all the other macros.

Cheers, Steve

Things that I have read, but perhaps not fully understood:
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Q_11804398.html?sfQueryTermInfo=1+document+macro+open
http://word.mvps.org/FAQs/MacrosVBA/DocumentEvents.htm
http://word.mvps.org/FAQs/MacrosVBA/PseudoAutoMacros.htm
0
Comment
Question by:DrTribos
  • 14
  • 6
  • 5
25 Comments
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 22950268
You could try a macro like this:
Sub FileOpen()
If ActiveDocument.AttachedTemplate = "C:\Program Files\Microsoft Office\Templates\YourTemplateName.dot" Then
    CommandBars("YourToolbarName").Visible = True
End Sub

Open in new window

0
 
LVL 15

Author Comment

by:DrTribos
ID: 22954090
Thanks for that - I am not sure exactly where to put the FileOpen() code... I want the macro to be global, stored in the same template that houses all the other macros.

Do I put it in the "ThisDocument" area? Is there a specific way to insert?

Cheers, Steve
0
 
LVL 15

Author Comment

by:DrTribos
ID: 22957640
In an attempt to learn I am doing things step-wise... so I used the code proposed and modified with a msgbox as follows:

Sub FileOpen()
MsgBox ("Hi A file Opened")
'If ActiveDocument.AttachedTemplate = "C:\Program Files\Microsoft Office\Templates\YourTemplateName.dot" Then
'    CommandBars("YourToolbarName").Visible = True
End Sub

I placed the code in Microsoft Word Objects/This Document --> Document (from PullDown)

When I click the file open button the macro runs, the message box appears but nothing else happens!

I'm going nuts!

I have also tried the following (in the same spot):
Private Sub oApp_NewDocument(ByVal  Doc  As  Document)
    'Your code here
End Sub

Private Sub oApp_DocumentOpen(ByVal Doc As Document)
    'Your code here
End Sub

Private Sub oApp_DocumentBeforeClose(ByVal Doc As Document, _
 Cancel As Boolean)
    'Your code here
End Sub

(with a msgbox in each - they don't seem to fire)
(taken from http://word.mvps.org/FAQs/MacrosVBA/PseudoAutoMacros.htm )
Again to no avail

Would love some help on this

Cheers

Steve
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 37

Assisted Solution

by:Joanne M. Orzech
Joanne M. Orzech earned 400 total points
ID: 22959712
Well - you remmed out (negated) the code that tells the toolbar to be visible.

Put the name of your template here:
If ActiveDocument.AttachedTemplate = "C:\Program Files\Microsoft Office\Templates\YourTemplateName.dot" Then

Put the name of your toolbar here:
    CommandBars("YourToolbarName").Visible = True

Remove the apostrophe from the beginning of each line and then run it
0
 
LVL 15

Author Comment

by:DrTribos
ID: 22963305
Yes - I remmed it out also (as possibly buried in my explanation above) put in msgbox - sorry I hope I did not make you slap your head too hard....

anyway, with the msgbox replacing your code, the Macro fires - shows msgbox and disables the open function... I don't see why a msgbox would disable the open function... will try your original code.

PS did I put it in the right place?
template is in ...word\startup
code is in ...thisdocument-->document

I guess your on the other side of the world from me...

Cheers

Steve
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 22963384
I wouldn't put it in ThisDocument module.... just put it in your normal.dot....

That way, it checks to see if the file you're opening is attached to whatever your template is named, and then if so, it turns on the toolbar.

Other side of the world?  Where are you Dr?  :)

0
 
LVL 15

Author Comment

by:DrTribos
ID: 22963525
Australia, you?

I thought, have read a lot, that code should be kept out of Normal.dot

I am starting to see the merit of it.. this template will be distributed to many...

but will give it a go :-)
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 22963551
It SHOULD be kept out of normal!!  You are so correct, I like to put all my macros in one global add-in template....

Australia huh?  I have friends there... just did a consulting job for a company in Darlinghurst, have friends in Northmead.... stayed at Yowie Bay in 1981 and 1994..... so I ADORE your country and its people....

I'm in Rochester, New York... upstate New York.

https://mvp.support.microsoft.com/profile=85C20F41-C1E3-4DAC-9F7F-41DAF5193F2E

0
 
LVL 15

Author Comment

by:DrTribos
ID: 22963645
Cool I was stateside in Sept - consulting too. Really enjoyed my time in the states - cousins in Newark, also made me realise what service is (was working just south of Chicago).

Can you recommend a VBA book (one with lots of examples might do)?
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 22963702
I'm looking for my VB book ..... can't put my hands on it at the moment.... You're welcome to it.  I am self taught and whenever I need to learn something, I just go online.... so I have better luck that way :)

Can't even recall the name of the book I got but I'll try to find it.... it was quite a good one I think.  What consulting do you do?

0
 
LVL 15

Author Comment

by:DrTribos
ID: 22963807
mainly in the field of efficiency, systems, error reduction, trouble shooting in industries like manufacturing, nuclear, mining, aviation...
0
 
LVL 15

Author Comment

by:DrTribos
ID: 22963914
OK, so I put the code in the Normal template (under Modules)  like this:

Sub FileOpen()
' MsgBox ("Hi A file Opened")
If ActiveDocument.AttachedTemplate = "C:\Program Files\Microsoft Office\Templates\YourTemplateName.dot" Then
    CommandBars("YourToolbarName").Visible = True
End If
End Sub

The attached template was not called YourTemplateName.dot so I expect the file dialogue to appear as normal when I press the folder button in word... but nothing happens...

Anyway - it can wait, have a great weekend.

Steve
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 22978472
What is the name of your template and what is the name of your toolbar Doc?
0
 
LVL 15

Author Comment

by:DrTribos
ID: 22989373
Template is "Training Manual (0.28).dot" & Toolbar is "YabMac_Fix"

I understand that the code should be written more in lines with the code below, however - if the template is not found (e.g. I don't have a template called "Your template name.dot" then the code should not disable the functionality of the open button...

Cheers

Steve
0
 
LVL 15

Author Comment

by:DrTribos
ID: 23062507
Hi - I still have a problem where adding code to:

FileOpen()

disables the File Open function, i.e. only the code that I have added will run! Can anyone help?

Thanks
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 23065564
You have pointed me here from another question, and there is a lot of history which I haven't gone through yet.

You should understand that any Macro that is named after a Word Command will run instead of that command. Therefore if you want to intervene, and yet you want the original purpose of the command to be executed (afterwards), you will have to reproduce that command in your own code after your own substitute code.
0
 
LVL 15

Author Comment

by:DrTribos
ID: 23065585
Indeed, and thank you for having a look.

In a nutshell I have a template called YabMacs stored in Word\Startup

This template contains toolbars. The toolbars should only be available to documents that were created using:

YabMac1.dot or YabMac2.dot

Hope that helps, Cheers

Steve
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 1600 total points
ID: 23070111
I think this is what you were aiming for.

Public Sub FileOpen()
Dim dlg As Dialog
 
Set dlg = Dialogs(wdDialogFileOpen)
If dlg.Show = -1 Then
    Select Case dlg.Name
        Case "Mydoc1", "MyDoc2"
        Call ShowMyToolbar
    End Select
    Select Case Documents(dlg.Name).AttachedTemplate
        Case "MyTempl1", "MyTempl2"
        Call ShowMyToolbar
    End Select
End If
 
End Sub

Open in new window

0
 
LVL 15

Author Comment

by:DrTribos
ID: 23074525
It works, Thank you!

Select case for "attached template" is more robust than for "file name", this seems to be due to file name being case sensitive.

I ended up changing the code a bit. Looking around I found this: http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/Q_21530384.html?sfQueryTermInfo=1+custom+document+properti+word

which led to the code below. It will be more robust because the document property should be invariant and sufficiently well hidden that people won't play with it.

If I may as a question as outside of the original scope:

How can the code be modified so that:
1. it still resides in my global macro (currently located in "word\startup" in 'ThisDocument' , and
2. if runs whenever a file opens (irrespective of how the file was opened)?
(or does this warrant a separate question?)

Many thanks

 Steve
Public Sub FileOpen()
Dim dlg As Dialog                       ' Predef. dialog box
 
Set dlg = Dialogs(wdDialogFileOpen)     ' causes dialog to run
If dlg.Show = -1 Then                   ' -1 indicates "OK" was pressed
    Select Case dlg.Name                ' File Name from the Dialog
        Case "SFB.doc", "MyDoc2"        ' SFB.doc works, sFB.doc does not
        Call ToolBarsOpenAll
        MsgBox ("Fired on: SFB")
    End Select
    Select Case Documents(dlg.Name).CustomDocumentProperties("ShowMac")     
        Case "Show"
        Call ToolBarsOpenAll
        MsgBox ("Fired on Custom Prop")
        Call FixUP
        Call Finish
        
    End Select
End If
 
End Sub

Open in new window

0
 
LVL 15

Author Closing Comment

by:DrTribos
ID: 31516285
Hi Graham, Joanne

Thanks for your help, the original question is answered. However, there is a "follow-on" question please see my comments - if the answer is complex just ignore & I'll repost as a new question.

Thanks again

Steve
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 23075930
1. It works for me if the code is in a module in a global template (in the Word StartUp folder).

2. The obvious place to put the code is in the template for the document. If it is there, in the attached template and in the Document_Open event of the ThisDocument module, the code will run whenever the document is opened. Event code get called in Global templates.

I don't believe that there is one single place that will catch opening every document.

Of course if you did plan to put the code in the documents' templates, then you might as well have a copy of the toolbar in them and you wouldn't need the code to show them.

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 23076000
VB string comparision is case-sensitive.

You can convert both sides to the same case:

if LCase$("ABC") = LCase$("abc") then


or you can use

Option Compare Text  

next to where Option Explicit is, or should be.

You can use document variables if you don't want them changed. They can only be modified in code and only seen with code or in DocVariable fields.
0
 
LVL 15

Author Comment

by:DrTribos
ID: 23076070
Thanks - I appreciate your comment.

I'm trying to move away from having macros in the documents. The macros are needed to create/convert the documents but once the documents are finished the macro should not be there (neither should the toolbars). As the documents will be used for reference only.

I have the code in start-up folder and it works great... it is slightly modified again.

I will investigate putting a macro in the document being created (NewDoc) that calls a macro from a global template (GlobDot)... this will probably be a new question. Concept as follows:

In NewDoc (built into NewDoc-template):

Sub AutoOpen()
Dim myName As DocName    ' myName is the name of this file
On Error Goto myEnd              ' So that if GlobDot is not available the macro ends in a controlled way
Call SomeMacro(myName)     ' SomeMacro is a macro In GlobDot
myEnd
End Sub

Then in GlobDot
Sub SomeMacro(FileToCheck as String)
     Select Case Documents(FileToCheck).CustomDocumentProperties("ShowMac")    
        Case "Show"
        Call ToolBarsOpenAll
     End Select
End Sub

Well that's the concept that I'm thinking of - will explore and post question if needed.

Thanks again for you comments

Kind regards

Steve
0
 
LVL 15

Author Comment

by:DrTribos
ID: 23076097
Thanks for the tips, will also look-up doc variables - that sounds like a useful thing :-)

I don't have option explicit - don't really know what it means (will google, or search on EE).

Thanks again, Steve

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 23076342
I agree that code should be in templates, not in the documents. Perhaps my words were a but confusing.

It would be possible to detach the template when it is finshed with. This is done by changing the attached template to another, e.g. the Normal template.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question