Results 1 to 15 of 15
  1. #1
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Rename Modules (Eng/Word2K/SR-1a)

    Hi Loungers:
    In Word VBE, after I click Insert/Module, is there a fast way to rename the module? The only way I can find is to go to the Organizer.

    If I do rename a module, will all the shortcut keys, toolbar, & menu items that point to a procedure within the module cease to work?

    Thanks in advance.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Rename Modules (Eng/Word2K/SR-1a)

    Hi Phil,

    The easiest way to rename a module is in the VBE itself. Make sure that the Project Explorer is displayed (View/Project Explorer or Ctrl+R) and that the Properties window is displayed (View/Properties Window or F4).
    In the Project Explorer, select the module, then type a new name in the Properties window. Name is the only property of a module.

    If your procedure and function names are unique, you won't have a problem. If you have procedures/functions of the same name in different modules, you'll have to precede their name with that of the module: Module1.MyProc or Module2.MyProc, for example.

  3. #3
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Rename Modules (Eng/Word2K/SR-1a)

    Hi Hans:
    Thanks for the information. I'm having some problems, however. My macros are in a template called macros.dot. The project name was the default, TemplateProject, & the module name was NewMacros. I found that I could rename the project without problems (so far <img src=/S/smile.gif border=0 alt=smile width=15 height=15>), but if I renamed the module, at least one of my macros wouldn't run. I have no duplicate names, so I'm still a little puzzled.

  4. #4
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Rename Modules (Eng/Word2K/SR-1a)

    What is the macro name? It's purpose?

    How is it being started (called)? From another macro, from a command button, by pressing F[5|8] in the VBE?

    Does it not run or bomb out on a particular line?

    <img src=/S/evilgrin.gif border=0 alt=evilgrin width=15 height=15>

    Phil <img src=/S/disappointed.gif border=0 alt=disappointed width=15 height=15>, you should know better <img src=/S/sigh.gif border=0 alt=sigh width=15 height=15> than to post a question with no details <img src=/S/duck.gif border=0 alt=duck width=23 height=23>
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  5. #5
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Rename Modules (Eng/Word2K/SR-1a)

    Hi Bryan & Hans:
    Yes, I know better than not to give details <img src=/S/blush.gif border=0 alt=blush width=15 height=15> and I apologize. I was rushed & really haven't been back to the Lounge for more than a few seconds since I posted the question.

    I've examined things more closely and here are more details. I have a global template (macros.dot) with one module. The default names were used when I created it (TemplateProject.NewMacros). After putting macros in the module, I created a drop down toolbar in macros.dot. I then put shortcuts to the macros on this toolbar.

    I was wrong in my earlier post. It is not that a macro won't run. It is that all of these shortcuts that do not work after I rename NewMacros to Mac (no relation to the "other" operating system...or the hamburger <img src=/S/grin.gif border=0 alt=grin width=15 height=15>).

    I can run the macros from either the VBE or from the Alt+F8 macros dialog box, but the toolbar icons no longer work. They are unaffected by renaming the TemplateProject to Pro, but renaming the module does affect them.

    Is this the expected behavior? If so, is there are way to rename or reattach all these shortcuts to the appropriate macro other than manually recreating them?

    Thanks again.

  6. #6
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Rename Modules (Eng/Word2K/SR-1a)

    OK, here's what has happened Phil.

    The command buttons have an OnAction property, which tells the button what to do when you click it. It is formatted like:

    ModuleName.ProcedureName

    So if you have A macro assigned to a command button that is calles PhilsTest in your NewMacros Module the OnAction will look like:

    NewMacros.PhilsTest

    You need to change the first part to point to the new Mac module. Unfortunately you can only do this through code:

    <pre>Sub FixTB()

    Dim cb As CommandBar
    Dim cmdbtn As CommandBarControl

    'Get a reference to the toolbar called MyTB, Change as appropriate
    Set cb = ActiveDocument.CommandBars("MyTB")

    'Loop Through each control on the toolbar
    For Each cmdbtn In cb.Controls
    'Check to see if the OnAction points to the NewMacros Module
    If Left$(cmdbtn.OnAction, InStr(cmdbtn.OnAction, ".") - 1) = "NewMacros" Then
    'It Does so replace the first part, NewMacros, with Mac
    cmdbtn.OnAction = "Mac" & Mid$(cmdbtn.OnAction, InStr(cmdbtn.OnAction, "."))
    End If
    Next

    End Sub</pre>


    That's all there is to it.
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  7. #7
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Rename Modules (Eng/Word2K/SR-1a)

    Hi Bryan:
    Thanks for this. However, I'm getting an invalid call or procedure (Error 5) at the line<pre> If Left$(cmdbtn.OnAction, InStr(cmdbtn.OnAction, ".") - 1) = "NewMacros" Then</pre>

    I pasted your code into my macros.dot & called it FixToolbar. I had already changed TemplateProject to Pro. I changed "NewMacros" to "Mac". I changed "MyTB" to "Macros" (as that is the name of the toolbar) & ran the code. Do you know what's wrong?

    Thanks again.

  8. #8
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Rename Modules (Eng/Word2K/SR-1a)

    Um.. No I don't.

    How about trying this:

    Replace:

    <pre>If Left$(cmdbtn.OnAction, InStr(cmdbtn.OnAction, ".") - 1) = "NewMacros" Then</pre>


    With:

    <pre>lngDotPos = InStr(cmdbtn.OnAction, ".")
    strModuleName = Left$(cmdbtn.OnAction, lngDotPos - 1)
    If strModuleName = "New Macros" Then</pre>


    It may narrow down where the problem is.

    Oh, yea, don't forget to add these dims at the top:

    <pre>Dim strModuleName As String
    Dim lngDotPos As Long</pre>


    lngDotPos should give you a number, the positioin of the . in the OnAction method of the command button.
    strModuleName should give you the name of the module that the OnAction Method is pointing to.

    If that still gives an error message, you can send me your macros.dot if you want, and I'll see what I can see.

    One last thought, are you opening, through File|Open, the macros.dot template? If not, try that. THere may be some sort of locking error if it is being opened at startup.
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  9. #9
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Rename Modules (Eng/Word2K/SR-1a)

    Hi Bryan:
    Yes, it gives the same error on the line

    strModuleName = Left$(cmdbtn.OnAction, lngDotPos - 1)

    I'll send you the macros.dot zipped separately. Thanks.

  10. #10
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Rename Modules (Eng/Word2K/SR-1a)

    Phil,

    When you get the error can you examine the value of cmdbtn.OnAction, maybe it doesn't have a dot for some reason.

    StuartR

  11. #11
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Rename Modules (Eng/Word2K/SR-1a)

    Phil,

    I don't know what to say, except <img src=/S/sorry.gif border=0 alt=sorry width=15 height=15> . I didn't read your post properly and I was working on a bad assumption <img src=/S/doh.gif border=0 alt=doh width=15 height=15> <img src=/S/blush.gif border=0 alt=blush width=15 height=15>

    Here is code that WILL work. Well it worked on your macros.dot on my laptop.

    <pre>Sub fixtb()
    Dim cbc As CommandBarControl
    Dim cbcp As CommandBarControl
    Dim strModName As String
    Dim lngDotPos As Long
    Dim doc As Document

    Set doc = Documents("Macros.dot")
    For Each cbc In doc.CommandBars("Macros").Controls
    Select Case cbc.Type
    Case msoControlPopup
    For Each cbcp In cbc.CommandBar.Controls
    If cbcp.Type = msoControlButton Then
    lngDotPos = InStr(cbcp.OnAction, ".")
    strModName = Left$(cbcp.OnAction, lngDotPos - 1)
    If strModName = "NewMacros" Then
    cbcp.OnAction = "Mac" & Mid(cbcp.OnAction, lngDotPos)
    End If
    End If
    Next
    Case msoControlButton
    lngDotPos = InStr(cbc.OnAction, ".")
    strModName = Left$(cbc.OnAction, lngDotPos - 1)
    If strModName = "NewMacros" Then
    cbc.OnAction = "Mac" & Mid(cbc.OnAction, lngDotPos)
    End If
    Case Else
    MsgBox "This code doesn't handle a CommandBarControl type: " & cbc.Type
    End Select
    Next
    End Sub</pre>


    I didn't "see" the line "After putting macros in the module, I created a drop down toolbar in macros.dot. I then put shortcuts to the macros on this toolbar."

    Specifically the dropdown part. So the code I originally posted worked on buttons right on the tool bar, but none in the drop down. This code check to see if the control is a drop down. If it is, it loops through all the controls in the dropdow and fixes them. It doesn't do nested dropdowns, but since you don't have any it's not an issue. If you did have nested dropdowns, then The easiest way would be to write a recursive sub (a sub that calles itself).

    So now hopefully this should work and I can redeem myself for not reading your post properly. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  12. #12
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Rename Modules (Eng/Word2K/SR-1a)

    It didn't have the dot because I didn't read Phil's original post properly. The controls are in a drop down in the toolbar. <img src=/S/doh.gif border=0 alt=doh width=15 height=15>
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  13. #13
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Rename Modules (Eng/Word2K/SR-1a)

    Hi Bryan:
    It works!! Thank you so much. Of course, I still have my work cut out because not all my shortcuts are on one toolbar. I've scattered them on several toolbars, menus, & shortcut menus. (That's what I get for not "cleaning up" sooner.) But at least now I know I can modify the names of modules whose commands are on toolbar dropdown menus (& I'm keeping the other one for buttons directly). So it's just the menu bar (easy) & some shortcut menus (if only I had kept track of which ones <img src=/S/grin.gif border=0 alt=grin width=15 height=15>).
    Thanks again...if I can repay the favor, just ask (although "redemption" is slightly out of my mortal powers <img src=/S/rofl.gif border=0 alt=rofl width=15 height=15>)

  14. #14
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Rename Modules (Eng/Word2K/SR-1a)

    Phil,

    I'm glad it finally worked. Its amazing what one can do when one reads the problem properly <img src=/S/doh.gif border=0 alt=doh width=15 height=15>

    <hr>(& I'm keeping the other one for buttons directly).<hr>
    No need to.

    Inside the Select...Case ther are 2 cases:

    msoControlPopup - which takes care of controls on a DropDown on a controlbar
    and
    msoControlButton - which takes carre of buttons directly on the commandbar.

    What you may want to consider doing, to make using it easier in the future

    Make constants out of the CommandBar Name (Macros) and The old module Name (NewMacros) and the new module name (Mac) and use the constants in code instead, so you only need to change them in one place in the future.

    'This goes at the top with the rest of the dims
    Const strCBName as string = "Macros"
    Constr strOldModule as String = "NewMacros"
    'Used Like this
    For Each cbc In doc.CommandBars(strCBName ).Controls
    ....
    If strModName = strOldModule Then
    .....

    I almost did it that way when I wrote the working code, but decidied against it for some strange reason.

    <hr>Thanks again...if I can repay the favor, just ask<hr>
    <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15>


    is always a godd thing <img src=/S/grin.gif border=0 alt=grin width=15 height=15> <img src=/S/rofl.gif border=0 alt=rofl width=15 height=15> <img src=/S/rofl.gif border=0 alt=rofl width=15 height=15> <img src=/S/rofl.gif border=0 alt=rofl width=15 height=15>

    Just kidding of course. Glad to help Phil.
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  15. #15
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Rename Modules (Eng/Word2K/SR-1a)

    The approach I have used for a while is to create a Macro for every toolbar that I create. This macro sets the all the button attributes for that toolbar, including the ToolTip, OnAction, etc. I can then edit the macro and rerun it whenever I need to.

    StuartR

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •