Macros and Toolbar icons

D

Dayo Mitchell

Word 2001, OS 9.2.2

I decided to sort my macros into modules of related actions with helpful
names, in the ongoing quest to make using Word more efficient. (E.g.,
putting ShowStyleWidth in a LongDocMacros module).

However, I have about 6 created toolbar icons linked to macros, and now none
of them work (e.g., ShowStyleWidth used to be in NewMacros module, so now
can't find its macro). What's the best way to relink the icon and the
macro? I tried changing the name in Properties of the icon, but that didn't
work on my first test run. I wound up creating a new toolbar icon for each
macro and copying over the old image, but was there a better way?

What happens to linked toolbar icons and macros in a remove and reinstall?
That is, say I sort this out now. Next remove and reinstall, I copy my
customized toolbars and my properly named macro modules into a new Normal
template. Will I have to relink everything again, does anyone know? Any
suggestions for avoiding such problems?

And, does anyone have any tips for making use of VBA Help? I have found it
horribly frustrating and unhelpful, but am (just barely) open to the
possibility that it's me missing something, as opposed to its writer being a
complete idiot.

Thanks,
Dayo
 
J

J.E. McGimpsey

Dayo Mitchell said:
Word 2001, OS 9.2.2

I decided to sort my macros into modules of related actions with helpful
names, in the ongoing quest to make using Word more efficient. (E.g.,
putting ShowStyleWidth in a LongDocMacros module).

However, I have about 6 created toolbar icons linked to macros, and now none
of them work (e.g., ShowStyleWidth used to be in NewMacros module, so now
can't find its macro). What's the best way to relink the icon and the
macro? I tried changing the name in Properties of the icon, but that didn't
work on my first test run. I wound up creating a new toolbar icon for each
macro and copying over the old image, but was there a better way?

Going back through some old posts, I see that this hasn't gotten an
answer.

I'm not sure this is the best answer, but it'll work. I'm not sure
why Word doesn't have the "Assign macro" contextual menu item that
Excel does when CTRL-clicking on a toolbar control...

Anyhow, in the VBE you can assign macros to controls by typing
something like this in the Immediate window:

Commandbars("My Bar").Controls("My Control").OnAction =
"MyModule.MyMacro"

If you moved all of them to the same module, you could use this
macro instead:

Public Sub RelinkButtonsToMacros()
Dim ctButton As CommandBarControl
With CommandBars("My Bar")
For Each ctButton In .Controls
With ctButton
.OnAction = Replace(.OnAction, _
"NewMacros", "MyModule")
End With
Next ctButton
End With
End Sub

This uses the Replace() function which was introduced in VB6, so
you'll have to roll your own - I keep this in a global template so
that it can be used by any document/template (as long as a reference
is set):

Public Function Replace(sIn As String, sFind As String, _
sReplace As String, Optional nStart As Long = 1, _
Optional nCount As Long = -1, Optional bCompare As _
Integer = vbBinaryCompare) As String
Dim nC As Long
Dim nPos As Long
Dim nFindLen As Long
Dim nRepLen As Long

nFindLen = Len(sFind)
nRepLen = Len(sReplace)

If (sFind <> "") And (sFind <> sReplace) Then
nPos = InStr(nStart, sIn, sFind, bCompare)
Do While nPos
nC = nC + 1
sIn = Left(sIn, nPos - 1) & sReplace & _
Mid(sIn, nPos + nFindLen)
nPos = InStr(nPos + nRepLen, sIn, sFind, bCompare)
Loop
End If
Replace = sIn
End Function
What happens to linked toolbar icons and macros in a remove and reinstall?
That is, say I sort this out now. Next remove and reinstall, I copy my
customized toolbars and my properly named macro modules into a new Normal
template. Will I have to relink everything again, does anyone know? Any
suggestions for avoiding such problems?

Since you're storing both the toolbars and the macros in the same
template, you shouldn't need to relink.

However, to avoid this, I'd suggest you put both toolbars and macros
in an add-in (save as a template in the Microsoft Office
2001:Office:Start:Word folder). I build my toolbars (and I disable
all the built-in ones) in code each time, but that's probably
overkill.
And, does anyone have any tips for making use of VBA Help? I have found it
horribly frustrating and unhelpful, but am (just barely) open to the
possibility that it's me missing something, as opposed to its writer being a
complete idiot.

VBA Help wasn't written by an idiot, but it sure wasn't indexed by
anyone who had to use it. Once you find the information, it's
generally pretty well-written. The only thing I can suggest is to a)
read the Visual Basic User Interface Help topics, b) browse the
topics under Visual Basic Language Reference, and c) become
intimately familiar with the Word object model to be found in VBA
Help under "Microsoft Word Objects"

And if you plan to upgrade to v.X, help has gotten progressively
better in each version since Word98.
 
D

Dayo Mitchell

Thanks much, J.E. I have just ventured into the world of the global add-in
(based on your advice) though so far it only has keyboard shortcuts in it.
Next time I am in a reconfiguring mood, I will move the toolbars and macros
over as well, but will also save your macros in case I ever need to run
them.

Just to make sure I am clear, b/c I definitely didn't understand on first
reading:

I install the second macro Public Function Replace in a global template, and
then if ever I try to run a macro that uses Replace, it refers back to the
macro that explains what Replace is?
What do you mean by "as long as a reference is set"?

I'll agree that the problem with VB Help is the setup rather than content,
that's definitely what inspired my rant.

Thanks,
Dayo
 
J

J.E. McGimpsey

Dayo Mitchell said:
Just to make sure I am clear, b/c I definitely didn't understand on first
reading:

I install the second macro Public Function Replace in a global template, and
then if ever I try to run a macro that uses Replace, it refers back to the
macro that explains what Replace is?
What do you mean by "as long as a reference is set"?

As an example:

Put Function Replace() in a new document. In the VBE, choose
Tools/Properties and set the Project Name to MyMacros. Save the file
as a template in the Microsoft Office 2001:Office:Start:Word folder,
with the name "MyMacros.dot", say.

Close and Restart Word. If you switch to the VBE, you'll notice that
your MyMacros project is loaded, but cannot be viewed (unless you
explicitly open the template).

In a new workbook, save this macro:

Public Sub Test()
MsgBox Replace("hello","h","j")
End Sub

If you try to run it, you'll get an error saying that Word can't
find "Replace".

While your new project is still selected, choose Tools/References,
and put a checkmark next to MyMacros.

Now run the sub - you'll get a message box with the word "jello".

Of course, you could always call it explicitly:

Public Sub Test()
MsgBox MyMacros.Replace("hello","h","j")
End Sub
 
D

Dayo Mitchell

Got it! Thanks very much. Still a novice with macros, but learning bit by
bit.

Dayo
 

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