Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word - VBA code to update VBA code in Normal.dot

Status
Not open for further replies.

bernie321

Programmer
Jan 7, 2004
477
GB
Hi

Is there any way to write a macro in another template to extract a sub from a VBA module in the normal.dot and then update it from another macro?

Any advice would be much apprecitated

Thanks
B
 
Yes,
The easiest way that I know is to add a reference to Microsoft Visual Basic for Applications Extensibility 5.3 (VBE6EXT.OLB) or equivilent. It contains the tools to locate and change procedures within a module.

Hope this helps,
CMP

(GMT-07:00) Mountain Time (US & Canada)
 
We can probably help more specifically if you post precisely what you are trying to do. Plus any code that you have to start it.

For example, I don't really know what you mean by "extract". Do you mean get the actual TEXT lines of code of a procedure?

And perhaps more importantly...WHY are you trying to do this?

Gerry
 
Hi

Thanks for your posts.

We have lots of precedent documents that are used and saved with various zooms.

We propose to use this macro to ensure that the user gets a default zoom each time:

Private Sub Document_Open()
ActiveDocument.ActiveWindow.View.Zoom.Percentage = 100
End sub

The problem is that many users have different resolutions so one persentage does not suit all. I can also not distibute a standard Normal.dot as all users have their own autotext setup.

So the idea is to setup a macro in one of our distibuted templates, add a macro to set the zoom that can be run by the user. The macro checks if the code is already there, if so it replaces it with a user specifed zoom, if not it creates it.

Thanks
B
 
If that is the case, then your subject is incorrect, is it not? You are NOT talking about normal.dot, you are talking about a distributed template.

BTW: mintjulep is totally correct - it IS a bad idea (generally) to have code in normal.dot. So using a distributed template is a good idea.
The problem is that many users have different resolutions so one persentage does not suit all. I can also not distibute a standard Normal.dot as all users have their own autotext setup.
RE: autotext...ANOTHER reason to not put stuff in normal.dot. Their autotext should be in an explicit template. In fact, if they did have it that way, you could use a distributed normal.dot.
So the idea is to setup a macro in one of our distibuted templates, add a macro to set the zoom that can be run by the user. The macro checks if the code is already there, if so it replaces it with a user specifed zoom, if not it creates it.
I am having a hard time following this. A macro to SET the zoom...but run by the user. Huh? Why bother when the user can just set the zoom?

But let me see if I can get at what I think you mean.

You have a code that set a zoom on document open. First of all...WHY?????? But anyway...

Because user have different resolutions, you want to be able to change that zoom. OK, that kind of makes sense. But are you talking about changing the code itself? Because if you are, then are you also talking about saving the document with the changed code? OK. Then if another user opens the document...and they have different resolution...do you change the code itself AGAIN? That is not very good design.

If this is so important - and I still can not really see why it is - then why not just test the zoom for the various resolutions? Then in Document_Open have code that checks the current resolution, and adjusts accordingly.

HOWEVER - I hate that. It feels like a serious intrusion into user preference. It feels like the zoom is being forced. If it is NOT being forced, and you are asking the user (your original idea), then you are back to WHY? The user can darn well change the view zoom if they want to...or not.

Gerry
 
Hi

Thank you for your post.

I think you may not understand fully / I may not have explained well:

1/ I want to write a macro that is set in the distributed templates to set the above macro in the normal.dot (as it only appears to run in the normal.dot).

2/ The macro can be run by the user to set their own 'default' zoom.

3/ As you probably know documents open with the zoom they were last saved by. Our precedents are created by various staff who use various resolutions/zoom percentages. The precedents are used by all staff, so will for many on a zoom that they do not want.

Our staff our fed up of changing the zoom every time they open a document, they are typists so they are inserting precedents, modifying them and closing them every couple of minutes. More importantly business wise paying large numbers of staff to constantly change the zoom every couple of minutes is expensive.

I understand your questioning but as you ask: the staff and the company both want it.

As mentioned above, we cannot get it to work from any other template apart from the normal.dot, so it must go there. Because each user is different we are not setting a standard zoom, instead the plan is to get each user to run an 'insert macro into your normal.dot' macro, they will be prompted for their zoom preference and it will insert the routine.

I would appreciate it if you know how to get this macro to work in a distributed template or can recommend another method.

Many thanks

B
 
Hi Bernie,

I can understand the requirement but, leaving aside any other considerations, in Word 2002 and Word 2003 in order to write VBA code as you request, the user must have "Trust access to VB project" set. It is very unusual for companies to set this in standard installations so the chances are that, for the code to work, you'll have to issue your users with instructions and you might find it easier just to issue them with instructions to set up a 'zoom' macro.

If you are running Word 2000, or all your users do have the option set, then post back. The code to write a small macro - to normal or elsewhere - is fairly simple.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
To further what Tony has said, doing what you propose will leave a macro in EVERY document you create, which may annoy (at the least), or cause them to get stripped out of e-mails to whatever outsiders get your documents.

I would suggest that you create a custom "Open" dialog.

I futher suggest that it live in something other than normal.dot. Put it in each users Word/Start directory.
 
Hi

Thank you for your post.

I cannot create a custom open dialoge as we use a document management system that opens the relevent documents, it does not have any features for running macros on opening the document.

Any advice on how to get such a macro to run outside of our normal.dot would be much appreciated.

Thanks
B
 
VBA Help said:
Just like other macros, auto macros can be stored in the Normal template, another template, or a document. In order for an auto macro to run, it must be either in the Normal template, in the active document, or in the template on which the active document is based. The only exception is the AutoExec macro, which will not run automatically unless it is stored in one of the following: the Normal template, a template that is loaded globally through the Templates and Add-Ins dialog box, or a global template stored in the folder specified as the Startup folder.

If your users are closing and opening Word with each doc, then AutoExec would seem to be the answer.
 

What I think you want is a global template in a shared template library, with an application document_open event handler and a check against a value stored in the user's registry for the zoom percentage - if not found, a value can be prompted for and then saved. I guess you would also need to provide a custom function for the user to change and/or override their setting once it was saved. I'll try and knock one up later.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
The other route might be to create an Add-In. You can set the Add-in to change the zoom when the [tt]DocumentOpen()[/tt] event of the [tt]Application[/tt] object fires.

Couple of benefits of an Add-in:[ul][li]The Add-in will load automatically when Word starts (no need for [tt]AutoExec[/tt]).[/li]
[li]No need to manipulate code in [tt]Normal.dot[/tt].[/li]
[li]Users can decide to use the Add-in, or not.[/li][/ul]

Just a thought,
CMP

(GMT-07:00) Mountain Time (US & Canada)
 

The promised code is:

Create a new Template (.dot File).
Insert a New Class Module containing:
Code:
[blue]Dim WithEvents App As Word.Application

Private Sub Class_Initialize()
    Set App = Word.Application
End Sub

Private Sub App_DocumentOpen(ByVal Doc As Document)
    Dirty = ActiveDocument.Saved
    Zoom = GetSetting("Word", "DocumentSettings", "Zoom")
    If Zoom = "" Then
        Zoom = InputBox("Please enter your desired zoom setting")
        [green]' Validate it here - numeric, and sensible(?)[/green]
        SaveSetting "Word", "DocumentSettings", "Zoom", Zoom
    End If
    ActiveWindow.View.Zoom = Zoom
    ActiveDocument.Saved = Dirty
End Sub[/blue]
and just let it default to the name of "Class1".
Insert a New (standard) Module containing:
Code:
[blue]Dim AppEvents As Class1

Sub AutoExec()
    Set AppEvents = New Class1
End Sub[/blue]
Save the template in your Workgroup Templates folder.

As said earlier you really need to add a reset facility of some sort but I'll leave that bit for you.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top