PDA

View Full Version : Problems with running macros



Saquiwej
10-11-2006, 05:15 AM
I have the following problem: I have been programming in VBA for a good while as a hobbyist so I am not an absolute beginner, my problem might appear a simple one anyway, if you know the answer to it. I am writing VBA code for Word at the moment. I'll use the macro for automatically formatting books downloaded from electronic libraries so as to afford a pleasant reading experience etc.. I am developing the code in a separate document called NovelForm, so that I might run it from any other documents I want to format. I considered to transfer the module into Normal.dot, but my experience is that in the phase of development it?s better to store macros in a usual workbook, because I don?t make backup copies of Normal.dot and if it gets deleted I would lose my macros. So, whenever I want to format a book, I have to open NovelForm and run the macro from there. Normally I did it so that I activated the document to be formatted and from the Visual Basic toolbar ran the formatting macro. Lately, however, I just can?t access the macros of other documents. The only macros I can reach from any documents are those stored in Normal.dot. So that the only way for me to run a macro kept in another file is to run it from the VBE editor, which is a bit annoying. I tried to establish references between the documents (VBE > Tools > References) and the file with the macros, but it won?t help. In the Run Macro dialog no other documents are visible apart from the active document and Normal. I tried also to put a command button on some of the toolbars, to run the macro from there, but it can?t be done either; in the Tools > Customize > Commands > Macros panel no macros are visible., the panel is dead. What exasperates me in particular, is that I have been running macros between documents for years without problems. Yesterday it was possible, today it isn?t, in the same files. Maybe I inadvertently changed some settings? Please, help me someone.

fumei
10-11-2006, 08:32 AM
Hmmmm.

First of all, having procedures NOT in Normal.dot is a very good idea. You may want to consider having the .DOT file with your code load as a global template. That way you will have access to all your code, independently of normal.dot.

Saquiwej
10-11-2006, 02:14 PM
I don't keep my macro in Normal, because I am still working on it, so I keep it in a usual Word document. How can I load this document as a global? I never bothered about globals before, I hardly know what they are, and all the same I could run any macros from any documents. In Excel, for example, it still works, why did it go wrong with Word?

mdmackillop
10-11-2006, 03:12 PM
Save your code in a template eg. Novel.dot. Move that template to your Startup folder eg. C:\Program Files\Microsoft Office\Office\Startup

fumei
10-11-2006, 05:15 PM
There are two locations for loading a global automatically.

The Program Files\YaddaYadda\Office\Startup folder; OR

Document and Settings\Username\Application Data\YaddaYadda\Word\Startup folder.

The first loads the global for all user sessions. The second loads it for the username session. Obviously, if you are the only user of the machine then it does not matter.

Note 1: you can also UNLOAD a global, either manually (Tools > Templates and Addins), or by code.

Note 2: globals are dynamic. You can open the global .dot file itself and add/change/remove procedures to it. These are immediately accessible. When a global template is "loaded" it is important to note that the file itself is NOT, repeat NOT, loaded - ie. opened. A global template has its procedures parsed and pointers are created for them. So, in fact, the term "loading" a global template is a bit of a misnomer. No document content is loaded - only pointers to procedures.

Saquiwej
10-12-2006, 02:47 AM
Thank you to you both, for taking the trouble to help me. I followed your advice and did the following: I opened a new document and the document in which the macros I am working on are located (named NovelForm). In VBE I copied the macros and user forms from NovelForm to the new document, which I then saved with the name Macros as a template to Document and Settings\Username\Application Data\Microsof\Word\Startup folder. Then I closed and reopened Word, and indeed, the macros saved in the template are now accessible from all Word documents, and, as much as I could test it in a few minutes, while I am writing this letter, they work. But: in VBE I can?t edit the macros saved in the template Macros, I cant edit the VBA project Macros at all. I get a message ?Project is unviewable?. Can you explain this, and instruct me how to edit my macros from the template? So, summing up: if I store my macros in a usual document, I can edit them, and work on them, but I have difficulties to run them from another document. If I store them in a template I can run them, but I can?t edit them.

mdmackillop
10-12-2006, 03:05 AM
You need to open the template as a template, not a document based on the template. The simplest way I've found for this is to use a hyperlink.
My code is

Sub Macros()
ActiveDocument.FollowHyperlink "C:\Program Files\Microsoft Office\Office\Startup\Macros.dot"
End Sub

fumei
10-13-2006, 07:54 AM
Correctly speaking, you need to open the template file (the one with the code) as a file. The fact that it is a template (or a document) is not relevant. Code in ANY file (document OR template) - other than the special case of Normal.Dot, which is always available for code - can only be edited if the file is actually open.

A hyperlink is good I suppose. I simply made a macro in normal.dot with a keyboard shortcut ( Alt-VB ) that fires:Sub GetVBATools()
Documents.Open FileName:= _
"C:\Program Files\Microsoft Office\Office10\" _
& "Startup\VBA XTools.dot"
End Subwhich opens my main global code holder. As a backup to that - because sometimes I am simply lazy and don't feel like typing Alt-VB, I also have an icon on the toolbar - OF THE TEMPLATE - that opens the template file itself.

So I have VBA XTools as global, so its toolbar is global. On the toolbar is an icon that will load the file itself.

There are many ways you could do it.

mdmackillop
10-14-2006, 02:14 AM
A hyperlink is good I suppose.
Damned by faint praise! :rotlaugh:

fumei
10-14-2006, 10:28 PM
No! No! Excuse me....using a hyperlink is FABULOUS!!!! GREAT!!!! The best! Gosh...I feel so silly not thinking about that. Gee, thanks Malcolm! You are the man!

Seriously though....a hyperlink is OK, but my point is you still need something to fire that macro that does the hyperlink. You could have text, I guess. But something has to fire that procedure. Since it IS a procedure....why not just have it open the file? Yes, the hyperlink will open the file as well.

Saquiwej
10-19-2006, 06:59 AM
Sorry, but I was away for some time, but I am back again to my macros. Thank you for your advice concerning running macros. Now I have a one-liner in my Normal.dot, which opens the file Macros.dot that I placed to Document and Settings\Username\Application data\Microsoft\Word\Startup folder. First I tried to solve the problem by placing an icon to my desktop pointing to Macros.dot, but it didn?t work, because it opened a Word document based on the template, I suppose. But the opening macro works. So the problem could be closed as solved, if there were not a few questions still unanswered:
I still don?t understand why I can?t run the macros of one document from any other Word document. Placing your macros in a global template is a good thing, perhaps, but after a time you accumulate a lot of code in the template. The fact that it takes up the memory on opening the template is not the first concern, for I don?t think I?ll write 1Mb of VBA code till the end of my life. But the template can contain a pretty great number of procedures, which will, after a time make finding the right one difficult. While, if you store the macros that you work on, in a document, there is no such difficulty. It is true, you can create a template for storing your actual code only. But in Excel it works so fine. No problems with running macros at all.

fumei
10-19-2006, 12:18 PM
Uh....you can run macros from another document.

As for having a great number of procedures...that is strictly an organizing issue. In my main global (which does nothing except hold code), I have:

modStrings - for string manipulation procedures
modBookmarks - for bookmark procedures
modFormFields - for formfield procedures
modEdits - for text editing procedures
modFields - for field procedures
modPrinting - for printing procedures

etc etc.

Saquiwej
10-20-2006, 10:45 AM
Okay. Then something must be wrong with my Word program, for I still cant run macros from another word document. Maybe I have a virus on the computer. In the running macros dialog I can't see no macro at all except what is in Normal.dot and the actual dokument. Are there any settings that I may have changed inadvertently?

fumei
10-22-2006, 04:50 PM
Well, no...I need to amend that. You can run any macro from any OPEN file. Otherwise, the only macros that are available are from Normal.dot, any global template (loaded), the current document, and the template that document was cloned from.

Saquiwej
10-23-2006, 02:59 AM
Of course! I never supposed that I could run a macro that is in an unopened document, except if it is a template. I open two documents, e.g. NovelForm, which contains the macro I want to use and Document1 that contains the text I want to format with the macro. If I open the running macros dialogue in Document1, I just can't see the macro from there, even if I set it to "all open templates and documents". I find it rather weird. I repeat: both documents are OPEN. It is now only an "academic" question, for the method you tought me works fine.

fumei
10-23-2006, 06:18 AM
Oh, sorry, I didn't understand. Yes, you are correct. Opening the macros dialogue from a document will NOT show the macros in another document.